# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
36235 | moonrabbit2 | 트리 (KOI16_tree) | C++11 | 2000 ms | 23564 KiB |
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>
#define N 200005
using namespace std;
vector<int>g[N];
int p[N],clr[N],c_clr=1;
int n,q;
bool visit[N];
int dfs_sz(int curr)
{
if(curr==0||curr==-1)
return 0;
visit[curr]=true;
int res=1;
if(g[curr].size()==0){
return 1;
}
for(int i=0;i<g[curr].size();i++)
if(p[g[curr][i]]==curr)
res+=dfs_sz(g[curr][i]);
if(p[curr]!=-1&&!visit[p[curr]])
res+=dfs_sz(p[curr]);
visit[curr]=false;
return res;
}
void dfs_clr(int curr)
{
if(curr==0||curr==-1)
return;
visit[curr]=true;
clr[curr]=c_clr;
for(int i=0;i<g[curr].size();i++)
if(p[g[curr][i]]==curr)
dfs_clr(g[curr][i]);
if(p[curr]!=-1&&!visit[p[curr]])
dfs_clr(p[curr]);
visit[curr]=false;
return;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin>>n>>q;
for(int i=2;i<=n;i++){
int parent;
cin>>parent;
p[i]=parent;
g[parent].push_back(i);
}
while(q--){
int b,c,d;
cin>>b>>c>>d;
if(!d){
if(clr[b]==clr[c])
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
continue;
}
if(clr[b]==clr[c]){
cout<<"YES"<<endl;
int tmp=p[b];
if(p[b]==-1)
continue;
p[b]=-1;
int szb=dfs_sz(b);
int szp=dfs_sz(tmp);
if(szb<szp){
dfs_clr(b);
c_clr++;
}
else{
dfs_clr(tmp);
c_clr++;
}
}
else{
cout<<"NO"<<endl;
int tmp=p[c];
if(p[c]==-1)
continue;
p[c]=-1;
int szc=dfs_sz(c),szp=dfs_sz(tmp);
if(szc<szp){
dfs_clr(c);
c_clr++;
}
else{
dfs_clr(tmp);
c_clr++;
}
}
}
return 0;
}
Compilation message (stderr)
# | 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... |