Submission #601753

#TimeUsernameProblemLanguageResultExecution timeMemory
601753WongChun1234Bridges (APIO19_bridges)C++14
13 / 100
3065 ms28276 KiB
#include<bits/stdc++.h> using namespace std; const int N=100050,B=500; int n,m,q,t[N],a[N],b[N],u[N],v[N],w[N],par[N],sz[N],ans[N],rw[B+10][B+10],irw[N],rrl[B+10],pt,ret; bool ch[N],vis[N]; vector<pair<int,int>> srt[N<<1]; vector<int> adj[N],del,dis; unordered_map<int,int> dd; int find(int u){ return par[u]==u?u:par[u]=find(par[u]); } void onion(int u,int v){ if (find(u)==find(v)) return; if (adj[find(u)].size()<adj[find(v)].size()) swap(u,v); //cout<<"onion"<<v<<" to "<<u<<"\n"; for (int i:adj[find(v)]) adj[find(u)].push_back(i); sz[find(u)]+=sz[find(v)]; sz[find(v)]=0; par[find(v)]=u; } void dfs(int nde,int lim,int curr){ //cout<<curr<<": "<<nde<<"::"<<sz[nde]<<"\n"; vis[nde]=1; del.push_back(nde); ret+=sz[nde]; for (int i:adj[nde]){ int des,cst; if (i<0) des=find(u[rrl[-i]]); else des=find(v[rrl[i]]); cst=rw[curr][abs(i)]; //cout<<i<<":"<<cst<<"-->"<<des<<"\n"; if (cst<lim||vis[des]) continue; dfs(des,lim,curr); } } int main(){ cin.tie(0)->sync_with_stdio(0); cin>>n>>m; for (int i=1;i<=m;i++){ cin>>u[i]>>v[i]>>w[i]; dis.push_back(w[i]); } cin>>q; for (int i=1;i<=q;i++){ cin>>t[i]>>a[i]>>b[i]; dis.push_back(b[i]); } sort(dis.begin(),dis.end()); dis.erase(unique(dis.begin(),dis.end()),dis.end()); for (int i=0;i<dis.size();i++) dd[dis[i]]=i; for (int l=1;l<=q;l+=B){ int r=min(q,l+B-1); for (int i=1;i<=m;i++) ch[i]=0,irw[i]=0; for (int i=0;i<dis.size();i++) srt[i].clear(); pt=0; for (int i=l;i<=r;i++){ if (t[i]==1){ ch[a[i]]=1; if (!irw[a[i]]) irw[a[i]]=++pt; rrl[irw[a[i]]]=a[i]; //cout<<pt<<":::"<<a[i]<<"\n"; } } for (int i=1;i<=m;i++) if (!ch[i]) srt[dd[w[i]]].push_back({u[i],v[i]}); for (int i=l;i<=r;i++) if (t[i]==2) srt[dd[b[i]]].push_back({INT_MAX,i}); for (int i=1;i<=n;i++) par[i]=i,sz[i]=1; for (int i=1;i<=pt;i++) rw[0][i]=w[rrl[i]]; for (int i=0;i<=r-l;i++){ if (i) for (int j=1;j<=pt;j++) rw[i][j]=rw[i-1][j]; if (t[l+i]==1) rw[i][irw[a[l+i]]]=b[l+i]; } for (int i=1;i<=n;i++) adj[i].clear(); for (int i=1;i<=m;i++) if (ch[i]){ adj[find(u[i])].push_back(irw[i]); adj[find(v[i])].push_back(-irw[i]); } for (int j=dis.size()-1;j>=0;j--) for (auto i:srt[j]){ if (i.first==INT_MAX){ ret=0; for (int k:del) vis[k]=0; dfs(find(a[i.second]),b[i.second],i.second-l); ans[i.second]=ret; }else{ onion(i.first,i.second); } } for (int i=l;i<=r;i++) if (t[i]==1) w[a[i]]=b[i]; } for (int i=1;i<=q;i++) if (ans[i]) cout<<ans[i]<<"\n"; }

Compilation message (stderr)

bridges.cpp: In function 'int main()':
bridges.cpp:50:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   50 |  for (int i=0;i<dis.size();i++) dd[dis[i]]=i;
      |               ~^~~~~~~~~~~
bridges.cpp:54:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   54 |   for (int i=0;i<dis.size();i++) srt[i].clear();
      |                ~^~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...