This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i,n) for(int i=0;i<n;i++)
#define REP(i,n) for(int i=1;i<n;i++)
#define rev(i,n) for(int i=n-1;i>=0;i--)
#define all(v) v.begin(),v.end()
#define P pair<int,int>
#define len(s) (int)s.size()
template<class T> inline bool chmin(T &a, T b){
if(a>b){a=b;return true;}
return false;
}
template<class T> inline bool chmax(T &a, T b){
if(a<b){a=b;return true;}
return false;
}
constexpr int mod = 1e9+7;
constexpr long long inf = 3e18;
struct UnionFind{
vector<int>par,size;
int find(int x){
return (par[x]==x?x:par[x]=find(par[x]));
}
void merge(int x,int y){
x=find(x);y=find(y);
if(x==y)return;
if(size[x]<size[y]){
par[x]=y;
size[y]+=size[x];
}else {
par[y]=x;
size[x]+=size[y];
}
}
bool same(int x,int y){
return find(x)==find(y);
}
UnionFind(int x){
rep(i,x){
par.push_back(i);size.push_back(1);
}
}
};
int N,M,Q;
struct S{
int weight,type,idx;
bool operator<(S a){
if(weight!=a.weight)return weight>a.weight;
return type<a.type;
}
};
int from[100005],to[100005];
int wh[100005];
int ans[100005];
signed main(){
cin>>N>>M;
vector<S>V;
rep(i,M){
cin>>from[i]>>to[i];
int c;cin>>c;
from[i]--;to[i]--;
V.push_back({c,0,i});
}
int Q;cin>>Q;
rep(i,Q){
cin>>wh[i]>>wh[i];int c;cin>>c;
wh[i]--;
V.push_back({c,1,i});
}
sort(all(V));
UnionFind uf(N);
for(S i:V){
if(!i.type)uf.merge(from[i.idx],to[i.idx]);
else {
ans[i.idx]=uf.size[uf.find(wh[i.idx])];
}
}
rep(i,Q)cout<<ans[i]<<endl;
}
# | 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... |