이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,q,cnt=1,qy=1;
ll ind[200004],pw[200004],chd[200004],par[200004];
vector<ll> g[200004]; //원래 번호
ll val(ll a)
{
ll ret=0;
while(a)
{
ret+=pw[a];
a-=(a&-a);
}
return ret;
}
void upd(ll a,ll x)
{
while(a<=n)
{
pw[a]+=x;
a+=(a&-a);
}
}
void updrange(ll a,ll sz)
{
upd(a,qy);
upd(a+sz,-qy);
qy=200000-a;
}
ll dfs(ll a,ll p)
{
ll ret=1;
ind[a]=cnt++,par[ind[a]]=ind[p];
for(int i=0;i<g[a].size();i++)
ret+=dfs(g[a][i],a);
chd[ind[a]]=ret;
return ret;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cin>>n>>q;
for(int i=2;i<=n;i++)
{
ll a;cin>>a;
g[a].push_back(i);
}
dfs(1,0);
for(int i=0;i<q;i++)
{
ll b,c,d;cin>>b>>c>>d;
if(d==0)
{
if(val(ind[b])==val(ind[c]))
cout<<"YES"<<"\n";
else
cout<<"NO"<<"\n";
}
else
{
if(val(ind[b])==val(ind[c]))
{
cout<<"YES"<<"\n";
ll f=ind[b];
if(val(f)==val(par[f]))
updrange(f,chd[f]);
}
else
{
cout<<"NO"<<"\n";
ll f=ind[c];
if(val(f)==val(par[f]))
updrange(f,chd[f]);
}
}
}
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
tree.cpp: In function 'll dfs(ll, ll)':
tree.cpp:42:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<g[a].size();i++)
~^~~~~~~~~~~~
# | 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... |