제출 #601700

#제출 시각아이디문제언어결과실행 시간메모리
601700WongChun1234다리 (APIO19_bridges)C++14
13 / 100
3085 ms11496 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,pair<int,int>>> srt; vector<int> adj[N],del; 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>>n>>m; for (int i=1;i<=m;i++){ cin>>u[i]>>v[i]>>w[i]; } cin>>q; for (int i=1;i<=q;i++){ cin>>t[i]>>a[i]>>b[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; srt.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"; } else srt.push_back({-b[i],{INT_MAX,i}}); } for (int i=1;i<=m;i++) if (!ch[i]) srt.push_back({-w[i],{u[i],v[i]}}); for (int i=1;i<=n;i++) par[i]=i,sz[i]=1; sort(srt.begin(),srt.end()); 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 (auto i:srt){ if (i.second.first==INT_MAX){ ret=0; for (int j:del) vis[j]=0; dfs(find(a[i.second.second]),b[i.second.second],i.second.second-l); ans[i.second.second]=ret; }else{ onion(i.second.first,i.second.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"; }
#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...