답안 #107546

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
107546 2019-04-25T01:57:02 Z aer0park 트리 (KOI16_tree) C++14
100 / 100
271 ms 27920 KB
#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+=n+1-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;
}

Compilation message

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++)
              ~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 5248 KB Output is correct
2 Correct 7 ms 5248 KB Output is correct
3 Correct 6 ms 5120 KB Output is correct
4 Correct 6 ms 5248 KB Output is correct
5 Correct 7 ms 5120 KB Output is correct
6 Correct 7 ms 5120 KB Output is correct
7 Correct 6 ms 5120 KB Output is correct
8 Correct 7 ms 5248 KB Output is correct
9 Correct 8 ms 5248 KB Output is correct
10 Correct 7 ms 5248 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 5248 KB Output is correct
2 Correct 7 ms 5248 KB Output is correct
3 Correct 6 ms 5120 KB Output is correct
4 Correct 6 ms 5248 KB Output is correct
5 Correct 7 ms 5120 KB Output is correct
6 Correct 7 ms 5120 KB Output is correct
7 Correct 6 ms 5120 KB Output is correct
8 Correct 7 ms 5248 KB Output is correct
9 Correct 8 ms 5248 KB Output is correct
10 Correct 7 ms 5248 KB Output is correct
11 Correct 8 ms 5120 KB Output is correct
12 Correct 7 ms 5120 KB Output is correct
13 Correct 7 ms 5120 KB Output is correct
14 Correct 7 ms 5120 KB Output is correct
15 Correct 8 ms 5172 KB Output is correct
16 Correct 7 ms 5120 KB Output is correct
17 Correct 7 ms 5120 KB Output is correct
18 Correct 7 ms 5120 KB Output is correct
19 Correct 7 ms 5120 KB Output is correct
20 Correct 8 ms 5120 KB Output is correct
21 Correct 10 ms 5120 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 5248 KB Output is correct
2 Correct 7 ms 5248 KB Output is correct
3 Correct 6 ms 5120 KB Output is correct
4 Correct 6 ms 5248 KB Output is correct
5 Correct 7 ms 5120 KB Output is correct
6 Correct 7 ms 5120 KB Output is correct
7 Correct 6 ms 5120 KB Output is correct
8 Correct 7 ms 5248 KB Output is correct
9 Correct 8 ms 5248 KB Output is correct
10 Correct 7 ms 5248 KB Output is correct
11 Correct 8 ms 5120 KB Output is correct
12 Correct 7 ms 5120 KB Output is correct
13 Correct 7 ms 5120 KB Output is correct
14 Correct 7 ms 5120 KB Output is correct
15 Correct 8 ms 5172 KB Output is correct
16 Correct 7 ms 5120 KB Output is correct
17 Correct 7 ms 5120 KB Output is correct
18 Correct 7 ms 5120 KB Output is correct
19 Correct 7 ms 5120 KB Output is correct
20 Correct 8 ms 5120 KB Output is correct
21 Correct 10 ms 5120 KB Output is correct
22 Correct 94 ms 5900 KB Output is correct
23 Correct 79 ms 5840 KB Output is correct
24 Correct 75 ms 5880 KB Output is correct
25 Correct 69 ms 5852 KB Output is correct
26 Correct 66 ms 5752 KB Output is correct
27 Correct 75 ms 6008 KB Output is correct
28 Correct 77 ms 5812 KB Output is correct
29 Correct 72 ms 5880 KB Output is correct
30 Correct 81 ms 5980 KB Output is correct
31 Correct 63 ms 6052 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 5248 KB Output is correct
2 Correct 7 ms 5248 KB Output is correct
3 Correct 6 ms 5120 KB Output is correct
4 Correct 6 ms 5248 KB Output is correct
5 Correct 7 ms 5120 KB Output is correct
6 Correct 7 ms 5120 KB Output is correct
7 Correct 6 ms 5120 KB Output is correct
8 Correct 7 ms 5248 KB Output is correct
9 Correct 8 ms 5248 KB Output is correct
10 Correct 7 ms 5248 KB Output is correct
11 Correct 271 ms 27480 KB Output is correct
12 Correct 256 ms 27600 KB Output is correct
13 Correct 241 ms 27640 KB Output is correct
14 Correct 253 ms 27512 KB Output is correct
15 Correct 235 ms 27484 KB Output is correct
16 Correct 248 ms 27656 KB Output is correct
17 Correct 250 ms 27768 KB Output is correct
18 Correct 247 ms 27640 KB Output is correct
19 Correct 258 ms 27784 KB Output is correct
20 Correct 260 ms 27920 KB Output is correct
21 Correct 236 ms 27620 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 5248 KB Output is correct
2 Correct 7 ms 5248 KB Output is correct
3 Correct 6 ms 5120 KB Output is correct
4 Correct 6 ms 5248 KB Output is correct
5 Correct 7 ms 5120 KB Output is correct
6 Correct 7 ms 5120 KB Output is correct
7 Correct 6 ms 5120 KB Output is correct
8 Correct 7 ms 5248 KB Output is correct
9 Correct 8 ms 5248 KB Output is correct
10 Correct 7 ms 5248 KB Output is correct
11 Correct 8 ms 5120 KB Output is correct
12 Correct 7 ms 5120 KB Output is correct
13 Correct 7 ms 5120 KB Output is correct
14 Correct 7 ms 5120 KB Output is correct
15 Correct 8 ms 5172 KB Output is correct
16 Correct 7 ms 5120 KB Output is correct
17 Correct 7 ms 5120 KB Output is correct
18 Correct 7 ms 5120 KB Output is correct
19 Correct 7 ms 5120 KB Output is correct
20 Correct 8 ms 5120 KB Output is correct
21 Correct 10 ms 5120 KB Output is correct
22 Correct 94 ms 5900 KB Output is correct
23 Correct 79 ms 5840 KB Output is correct
24 Correct 75 ms 5880 KB Output is correct
25 Correct 69 ms 5852 KB Output is correct
26 Correct 66 ms 5752 KB Output is correct
27 Correct 75 ms 6008 KB Output is correct
28 Correct 77 ms 5812 KB Output is correct
29 Correct 72 ms 5880 KB Output is correct
30 Correct 81 ms 5980 KB Output is correct
31 Correct 63 ms 6052 KB Output is correct
32 Correct 271 ms 27480 KB Output is correct
33 Correct 256 ms 27600 KB Output is correct
34 Correct 241 ms 27640 KB Output is correct
35 Correct 253 ms 27512 KB Output is correct
36 Correct 235 ms 27484 KB Output is correct
37 Correct 248 ms 27656 KB Output is correct
38 Correct 250 ms 27768 KB Output is correct
39 Correct 247 ms 27640 KB Output is correct
40 Correct 258 ms 27784 KB Output is correct
41 Correct 260 ms 27920 KB Output is correct
42 Correct 236 ms 27620 KB Output is correct
43 Correct 200 ms 15072 KB Output is correct
44 Correct 200 ms 14964 KB Output is correct
45 Correct 196 ms 14968 KB Output is correct
46 Correct 183 ms 15120 KB Output is correct
47 Correct 178 ms 14968 KB Output is correct
48 Correct 125 ms 12520 KB Output is correct
49 Correct 121 ms 13212 KB Output is correct
50 Correct 132 ms 13288 KB Output is correct
51 Correct 267 ms 20984 KB Output is correct