Submission #973102

#TimeUsernameProblemLanguageResultExecution timeMemory
973102UnforgettableplBridges (APIO19_bridges)C++17
14 / 100
78 ms12992 KiB
#include <bits/stdc++.h> using namespace std; #define int long long struct UFDS{ vector<int> siz,p; UFDS(int n):siz(n+1,1),p(n+1){iota(p.begin(),p.end(),0);} int findRoot(int x){ return x==p[x] ? x : p[x]= findRoot(p[x]); } void unite(int a,int b){ a = findRoot(a); b = findRoot(b); if(a==b)return; if(siz[b]>siz[a])swap(a,b); siz[a]+=siz[b]; p[b] = a; } }; int ans[100001]; UFDS dsu(50000); int32_t main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); vector<pair<int,pair<int,int>>> edges; vector<tuple<int,int,int>> queries; int n,m; cin >> n >> m; for(int i=1;i<=m;i++){ int a,b,c;cin>>a>>b>>c; edges.emplace_back(c,make_pair(a,b)); } int q; cin >> q; for(int i=1;i<=q;i++){ int type,a,b;cin>>type>>a>>b; queries.emplace_back(b,a,i); } sort(edges.rbegin(), edges.rend()); sort(queries.rbegin(), queries.rend()); auto iter = edges.begin(); for(auto[wt,x,idx]:queries){ while(iter!=edges.end() and iter->first>=wt){ dsu.unite(iter->second.first,iter->second.second); iter++; } ans[idx] = dsu.siz[dsu.findRoot(x)]; } for(int i=1;i<=q;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...