이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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 time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |