이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
const int N=150050;
int n,m,q,u[N],v[N],w[N],ans,t,a,b,lift[20][N],ch[2][N],sz[N],par[N],val[N],rt,rpar[20][N];
bool vis[N];
vector<int> adj[N];
vector<pair<int,pair<int,int>>> srt;
int find(int u){
return (par[u]==u)?u:par[u]=find(par[u]);
}
void dfs(int nde,int par){
lift[0][nde]=val[par];
for (int i=1;i<20;i++) rpar[i][nde]=rpar[i-1][rpar[i-1][nde]],lift[i][nde]=val[rpar[i][nde]];
//cerr<<nde<<":"<<val[nde]<<"::"<<ch[0][nde]<<","<<ch[1][nde]<<"\n";
if (nde>n){
dfs(ch[0][nde],nde);
dfs(ch[1][nde],nde);
}
}
int main(){
cin>>n>>m;
for (int i=1;i<=m;i++){
cin>>u[i]>>v[i]>>w[i];
srt.push_back({-w[i],{u[i],v[i]}});
}
for (int i=1;i<=n;i++) sz[i]=1;
sort(srt.begin(),srt.end());
int cnt=n;
for (int i=1;i<2*n;i++) par[i]=i;
for (auto i:srt){
if (find(i.second.first)==find(i.second.second)) continue;
ch[0][++cnt]=find(i.second.first);
ch[1][cnt]=find(i.second.second);
sz[cnt]=sz[find(i.second.first)]+sz[find(i.second.second)];
val[cnt]=-i.first;
rpar[0][find(i.second.first)]=rpar[0][find(i.second.second)]=cnt;
par[find(i.second.first)]=par[find(i.second.second)]=cnt;
rt=cnt;
}
dfs(rt,rt);
cin>>q;
while (q--){
cin>>t>>a>>b;
if (t==1) continue;
for (int i=19;i>=0;i--){
if (val[rpar[i][a]]>=b) a=rpar[i][a];
}
cout<<sz[a]<<"\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... |