# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
38869 | 2018-01-07T12:34:16 Z | moonrabbit2 | None (KOI16_tree) | C++14 | 456 ms | 38356 KB |
#include <bits/stdc++.h> #define N 200005 #define pb push_back using namespace std; typedef long long ll; int n,q,par[N]; vector<int> child[N]; int sub[N],lev[N]; vector<int> chain[N]; int where[N],place[N],c_chain; vector<ll>tree[N]; bool linked[N]; void dfs(int curr) { sub[curr]=1; for(auto &i : child[curr]){ lev[i]=lev[curr]+1; dfs(i); sub[curr]+=sub[i]; } } void hld(int curr) { where[curr]=c_chain; chain[c_chain].pb(curr); int idx=-1; for(auto &i : child[curr]){ if(idx==-1||sub[i]>sub[idx]) idx=i; } if(idx!=-1) hld(idx); for(auto &i : child[curr]){ if(i!=idx){ c_chain++; hld(i); } } } ll sum(int num,int pos) { pos++; ll res=0; while(pos){ res+=tree[num][pos]; pos&=(pos-1); } return res; } void add(int num,int pos,ll val) { pos++; while(pos<tree[num].size()){ tree[num][pos]+=val; pos+=(pos&-pos); } } ll query_dist(int u,int v) { ll res=0; while(where[u]!=where[v]){ int uu=chain[where[u]][0]; int vv=chain[where[v]][0]; if(lev[uu]>lev[vv]){ if(linked[uu]) return -1; res+=sum(where[u],place[u])+1; u=par[uu]; } else{ if(linked[vv]) return -1; res+=sum(where[v],place[v])+1; v=par[vv]; } } if(place[v]>=place[u]) res+=sum(where[v],place[v])-sum(where[u],place[u]); else res+=sum(where[u],place[u])-sum(where[v],place[v]); return res; } int main() { scanf("%d %d",&n,&q); for(int i=2;i<=n;i++){ int x; scanf("%d",&x); par[i]=x; child[x].pb(i); } dfs(1); hld(1); for(int i=0;i<=c_chain;i++){ tree[i].resize(chain[i].size()+1); for(int j=1;j<chain[i].size();j++){ place[chain[i][j]]=j; add(i,j,1); } } while(q--){ int x,y,z; scanf("%d %d %d",&x,&y,&z); if(z==0){ if(query_dist(x,y)>=0) puts("YES"); else puts("NO"); } else{ if(query_dist(x,y)>=0){ puts("YES"); linked[x]=true; add(where[x],place[x],-N); } else{ puts("NO"); linked[y]=true; add(where[y],place[y],-N); } } } return 0; }
Compilation message
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 6 ms | 20184 KB | Output is correct |
2 | Correct | 3 ms | 20184 KB | Output is correct |
3 | Correct | 16 ms | 20184 KB | Output is correct |
4 | Correct | 6 ms | 20184 KB | Output is correct |
5 | Correct | 3 ms | 20184 KB | Output is correct |
6 | Correct | 6 ms | 20184 KB | Output is correct |
7 | Correct | 0 ms | 20184 KB | Output is correct |
8 | Correct | 9 ms | 20184 KB | Output is correct |
9 | Correct | 3 ms | 20184 KB | Output is correct |
10 | Correct | 6 ms | 20184 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 6 ms | 20184 KB | Output is correct |
2 | Correct | 3 ms | 20184 KB | Output is correct |
3 | Correct | 16 ms | 20184 KB | Output is correct |
4 | Correct | 6 ms | 20184 KB | Output is correct |
5 | Correct | 3 ms | 20184 KB | Output is correct |
6 | Correct | 6 ms | 20184 KB | Output is correct |
7 | Correct | 0 ms | 20184 KB | Output is correct |
8 | Correct | 9 ms | 20184 KB | Output is correct |
9 | Correct | 3 ms | 20184 KB | Output is correct |
10 | Correct | 6 ms | 20184 KB | Output is correct |
11 | Correct | 3 ms | 20316 KB | Output is correct |
12 | Correct | 9 ms | 20316 KB | Output is correct |
13 | Correct | 9 ms | 20316 KB | Output is correct |
14 | Correct | 3 ms | 20316 KB | Output is correct |
15 | Correct | 6 ms | 20316 KB | Output is correct |
16 | Correct | 16 ms | 20316 KB | Output is correct |
17 | Correct | 3 ms | 20316 KB | Output is correct |
18 | Correct | 3 ms | 20316 KB | Output is correct |
19 | Correct | 9 ms | 20316 KB | Output is correct |
20 | Correct | 3 ms | 20316 KB | Output is correct |
21 | Correct | 3 ms | 20316 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 6 ms | 20184 KB | Output is correct |
2 | Correct | 3 ms | 20184 KB | Output is correct |
3 | Correct | 16 ms | 20184 KB | Output is correct |
4 | Correct | 6 ms | 20184 KB | Output is correct |
5 | Correct | 3 ms | 20184 KB | Output is correct |
6 | Correct | 6 ms | 20184 KB | Output is correct |
7 | Correct | 0 ms | 20184 KB | Output is correct |
8 | Correct | 9 ms | 20184 KB | Output is correct |
9 | Correct | 3 ms | 20184 KB | Output is correct |
10 | Correct | 6 ms | 20184 KB | Output is correct |
11 | Correct | 3 ms | 20316 KB | Output is correct |
12 | Correct | 9 ms | 20316 KB | Output is correct |
13 | Correct | 9 ms | 20316 KB | Output is correct |
14 | Correct | 3 ms | 20316 KB | Output is correct |
15 | Correct | 6 ms | 20316 KB | Output is correct |
16 | Correct | 16 ms | 20316 KB | Output is correct |
17 | Correct | 3 ms | 20316 KB | Output is correct |
18 | Correct | 3 ms | 20316 KB | Output is correct |
19 | Correct | 9 ms | 20316 KB | Output is correct |
20 | Correct | 3 ms | 20316 KB | Output is correct |
21 | Correct | 3 ms | 20316 KB | Output is correct |
22 | Correct | 99 ms | 20316 KB | Output is correct |
23 | Correct | 106 ms | 20316 KB | Output is correct |
24 | Correct | 96 ms | 20316 KB | Output is correct |
25 | Correct | 109 ms | 20316 KB | Output is correct |
26 | Correct | 109 ms | 20316 KB | Output is correct |
27 | Correct | 109 ms | 20316 KB | Output is correct |
28 | Correct | 146 ms | 20316 KB | Output is correct |
29 | Correct | 99 ms | 20316 KB | Output is correct |
30 | Correct | 136 ms | 20316 KB | Output is correct |
31 | Correct | 143 ms | 20316 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 6 ms | 20184 KB | Output is correct |
2 | Correct | 3 ms | 20184 KB | Output is correct |
3 | Correct | 16 ms | 20184 KB | Output is correct |
4 | Correct | 6 ms | 20184 KB | Output is correct |
5 | Correct | 3 ms | 20184 KB | Output is correct |
6 | Correct | 6 ms | 20184 KB | Output is correct |
7 | Correct | 0 ms | 20184 KB | Output is correct |
8 | Correct | 9 ms | 20184 KB | Output is correct |
9 | Correct | 3 ms | 20184 KB | Output is correct |
10 | Correct | 6 ms | 20184 KB | Output is correct |
11 | Correct | 369 ms | 38356 KB | Output is correct |
12 | Correct | 393 ms | 38356 KB | Output is correct |
13 | Correct | 383 ms | 38352 KB | Output is correct |
14 | Correct | 349 ms | 38356 KB | Output is correct |
15 | Correct | 456 ms | 38356 KB | Output is correct |
16 | Correct | 429 ms | 38352 KB | Output is correct |
17 | Correct | 393 ms | 38352 KB | Output is correct |
18 | Correct | 349 ms | 38352 KB | Output is correct |
19 | Correct | 323 ms | 38352 KB | Output is correct |
20 | Correct | 383 ms | 38348 KB | Output is correct |
21 | Correct | 366 ms | 38352 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 6 ms | 20184 KB | Output is correct |
2 | Correct | 3 ms | 20184 KB | Output is correct |
3 | Correct | 16 ms | 20184 KB | Output is correct |
4 | Correct | 6 ms | 20184 KB | Output is correct |
5 | Correct | 3 ms | 20184 KB | Output is correct |
6 | Correct | 6 ms | 20184 KB | Output is correct |
7 | Correct | 0 ms | 20184 KB | Output is correct |
8 | Correct | 9 ms | 20184 KB | Output is correct |
9 | Correct | 3 ms | 20184 KB | Output is correct |
10 | Correct | 6 ms | 20184 KB | Output is correct |
11 | Correct | 3 ms | 20316 KB | Output is correct |
12 | Correct | 9 ms | 20316 KB | Output is correct |
13 | Correct | 9 ms | 20316 KB | Output is correct |
14 | Correct | 3 ms | 20316 KB | Output is correct |
15 | Correct | 6 ms | 20316 KB | Output is correct |
16 | Correct | 16 ms | 20316 KB | Output is correct |
17 | Correct | 3 ms | 20316 KB | Output is correct |
18 | Correct | 3 ms | 20316 KB | Output is correct |
19 | Correct | 9 ms | 20316 KB | Output is correct |
20 | Correct | 3 ms | 20316 KB | Output is correct |
21 | Correct | 3 ms | 20316 KB | Output is correct |
22 | Correct | 99 ms | 20316 KB | Output is correct |
23 | Correct | 106 ms | 20316 KB | Output is correct |
24 | Correct | 96 ms | 20316 KB | Output is correct |
25 | Correct | 109 ms | 20316 KB | Output is correct |
26 | Correct | 109 ms | 20316 KB | Output is correct |
27 | Correct | 109 ms | 20316 KB | Output is correct |
28 | Correct | 146 ms | 20316 KB | Output is correct |
29 | Correct | 99 ms | 20316 KB | Output is correct |
30 | Correct | 136 ms | 20316 KB | Output is correct |
31 | Correct | 143 ms | 20316 KB | Output is correct |
32 | Correct | 369 ms | 38356 KB | Output is correct |
33 | Correct | 393 ms | 38356 KB | Output is correct |
34 | Correct | 383 ms | 38352 KB | Output is correct |
35 | Correct | 349 ms | 38356 KB | Output is correct |
36 | Correct | 456 ms | 38356 KB | Output is correct |
37 | Correct | 429 ms | 38352 KB | Output is correct |
38 | Correct | 393 ms | 38352 KB | Output is correct |
39 | Correct | 349 ms | 38352 KB | Output is correct |
40 | Correct | 323 ms | 38352 KB | Output is correct |
41 | Correct | 383 ms | 38348 KB | Output is correct |
42 | Correct | 366 ms | 38352 KB | Output is correct |
43 | Correct | 409 ms | 32460 KB | Output is correct |
44 | Correct | 433 ms | 32460 KB | Output is correct |
45 | Correct | 423 ms | 32460 KB | Output is correct |
46 | Correct | 399 ms | 32460 KB | Output is correct |
47 | Correct | 436 ms | 32460 KB | Output is correct |
48 | Correct | 223 ms | 33696 KB | Output is correct |
49 | Correct | 246 ms | 33968 KB | Output is correct |
50 | Correct | 243 ms | 34028 KB | Output is correct |
51 | Correct | 349 ms | 31468 KB | Output is correct |