NC50884. Go on Strike!
描述
输入描述
The first line of input contains two space-separated integers N and Q, where N is the number of cities and Q is the number of events.
Following Q lines each contains four space-separated integers . If , there is a new flight coming out between city and city with cost . If , the flight attendants of the flight connecting city and with cost has gone on strike and the flight is shut down.
It's guaranteed that if , the required flight exists.
输出描述
Output Q lines each contains an integer representing the most flights needed to connect two cities in the chosen combination. If there exists two cities which can not be connected through the current flights, output .
It's guaranteed that the chosen combination will be unique.
示例1
输入:
3 6 1 1 2 1 1 1 3 2 1 2 3 4 1 1 2 8 0 1 2 1 0 2 3 4
输出:
-1 2 2 2 2 2
示例2
输入:
4 4 1 1 2 1 1 1 3 2 1 1 4 8 1 2 3 4
输出:
-1 -1 2 2
说明:
First two flights are not enough to make each pair of cities connected.C++11(clang++ 3.9) 解法, 执行用时: 6601ms, 内存消耗: 3328K, 提交时间: 2019-07-23 15:50:43
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int MAXN=10005; const int MAXM=20005; const int INF=0x3f3f3f3f; struct Edge { int u,v,w; Edge(){} Edge(int _u,int _v,int _w):u(min(_u,_v)),v(max(_u,_v)),w(_w){} bool operator < (const Edge &t)const { return u==t.u ? (v==t.v ? w<t.w : v<t.v) : u<t.u; } }e[MAXM]; map<Edge,int>mp; inline int get_eid(const Edge &u) { if(!mp[u])e[mp[u]=mp.size()]=u; return mp[u]; } vector<pair<int,int>> g[MAXN]; bool dfs_add(int u,int f,int p,int &rep) { if(u==p)return 1; for(auto &t:g[u]) { int v=t.first,id=t.second; if(v==f)continue; if(dfs_add(v,u,p,rep)) { if(e[id].w>e[rep].w) rep=id; return 1; } } return 0; } int pid[MAXN]; void dfs_del(int u,int f,int id) { pid[u]=id; for(auto &t:g[u]) { int v=t.first; if(v==f)continue; dfs_del(v,u,id); } } ll dp[MAXN]; void dfs_dia(int u,int f,ll &res) { dp[u]=0; for(auto &t:g[u]) { int v=t.first; if(v==f)continue; dfs_dia(v,u,res); res=max(res,dp[u]+dp[v]+1); dp[u]=max(dp[u],dp[v]+1); } } vector<int> bak; int main() { int n,q,e_cnt=0; scanf("%d%d",&n,&q); ll las=0; for(int i=1;i<=q;i++) { int q,u,v,c; scanf("%d%d%d%d",&q,&u,&v,&c); int eid=get_eid({u,v,c}),keep=0; if(q==1) { int rep=eid; if(dfs_add(u,0,v,rep)) { if(rep==eid) { bak.push_back(eid); keep=1; } else { g[e[rep].u].erase(find(g[e[rep].u].begin(),g[e[rep].u].end(),make_pair(e[rep].v,rep))); g[e[rep].v].erase(find(g[e[rep].v].begin(),g[e[rep].v].end(),make_pair(e[rep].u,rep))); g[u].push_back({v,eid}); g[v].push_back({u,eid}); } } else { g[u].push_back({v,eid}); g[v].push_back({u,eid}); e_cnt++; } } else { auto itr=find(bak.begin(),bak.end(),eid); if(itr!=bak.end()) { bak.erase(itr); keep=1; } else { g[u].erase(find(g[u].begin(),g[u].end(),make_pair(v,eid))); g[v].erase(find(g[v].begin(),g[v].end(),make_pair(u,eid))); dfs_del(u,0,2*i),dfs_del(v,0,2*i+1),e_cnt--; int mi=INF,loc=0; for(auto &t:bak) if(pid[e[t].u]>=2*i && (pid[e[t].u]^pid[e[t].v])==1 && e[t].w<mi) mi=e[t].w,loc=t; if(loc) { g[e[loc].u].push_back({e[loc].v,loc}); g[e[loc].v].push_back({e[loc].u,loc}); e_cnt++; } } } if(!keep) { if(e_cnt<n-1)printf("%lld\n",las=-1LL); else { ll res=0; if(keep)res=las; else dfs_dia(1,0,res); printf("%lld\n",las=res); } } else printf("%lld\n",las); } return 0; }
C++14(g++5.4) 解法, 执行用时: 6341ms, 内存消耗: 3340K, 提交时间: 2019-07-22 00:09:57
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int MAXN=10005; const int MAXM=20005; const int INF=0x3f3f3f3f; struct Edge { int u,v,w; Edge(){} Edge(int _u,int _v,int _w):u(min(_u,_v)),v(max(_u,_v)),w(_w){} bool operator < (const Edge &t)const { return u==t.u ? (v==t.v ? w<t.w : v<t.v) : u<t.u; } }e[MAXM]; map<Edge,int>mp; inline int get_eid(const Edge &u) { if(!mp[u])e[mp[u]=mp.size()]=u; return mp[u]; } vector<pair<int,int>> g[MAXN]; bool dfs_add(int u,int f,int p,int &rep) { if(u==p)return 1; for(auto &t:g[u]) { int v=t.first,id=t.second; if(v==f)continue; if(dfs_add(v,u,p,rep)) { if(e[id].w>e[rep].w) rep=id; return 1; } } return 0; } int pid[MAXN]; void dfs_del(int u,int f,int id) { pid[u]=id; for(auto &t:g[u]) { int v=t.first; if(v==f)continue; dfs_del(v,u,id); } } ll dp[MAXN]; void dfs_dia(int u,int f,ll &res) { dp[u]=0; for(auto &t:g[u]) { int v=t.first; if(v==f)continue; dfs_dia(v,u,res); res=max(res,dp[u]+dp[v]+1); dp[u]=max(dp[u],dp[v]+1); } } vector<int> bak; int main() { int n,q,e_cnt=0; scanf("%d%d",&n,&q); ll las=0; for(int i=1;i<=q;i++) { int q,u,v,c; scanf("%d%d%d%d",&q,&u,&v,&c); int eid=get_eid({u,v,c}),keep=0; if(q==1) { int rep=eid; if(dfs_add(u,0,v,rep)) { if(rep==eid) { bak.push_back(eid); keep=1; } else { g[e[rep].u].erase(find(g[e[rep].u].begin(),g[e[rep].u].end(),make_pair(e[rep].v,rep))); g[e[rep].v].erase(find(g[e[rep].v].begin(),g[e[rep].v].end(),make_pair(e[rep].u,rep))); g[u].push_back({v,eid}); g[v].push_back({u,eid}); } } else { g[u].push_back({v,eid}); g[v].push_back({u,eid}); e_cnt++; } } else { auto itr=find(bak.begin(),bak.end(),eid); if(itr!=bak.end()) { bak.erase(itr); keep=1; } else { g[u].erase(find(g[u].begin(),g[u].end(),make_pair(v,eid))); g[v].erase(find(g[v].begin(),g[v].end(),make_pair(u,eid))); dfs_del(u,0,2*i),dfs_del(v,0,2*i+1),e_cnt--; int mi=INF,loc=0; for(auto &t:bak) if(pid[e[t].u]>=2*i && (pid[e[t].u]^pid[e[t].v])==1 && e[t].w<mi) mi=e[t].w,loc=t; if(loc) { g[e[loc].u].push_back({e[loc].v,loc}); g[e[loc].v].push_back({e[loc].u,loc}); e_cnt++; } } } if(!keep) { if(e_cnt<n-1)printf("%lld\n",las=-1LL); else { ll res=0; if(keep)res=las; else dfs_dia(1,0,res); printf("%lld\n",las=res); } } else printf("%lld\n",las); } return 0; }