# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
97479 |
2019-02-16T11:26:49 Z |
Retro3014 |
None (KOI16_tree) |
C++11 |
|
2000 ms |
29664 KB |
#include <bits/stdc++.h>
#define pb push_back
#define all(v) ((v).begin(), (v).end())
#define sortv(v) sort(all(v))
#define sz(v) ((int)(v).size())
#define uniqv(v) (v).erase(unique(all(v)), (v).end())
#define umax(a, b) (a)=max((a), (b))
#define umin(a, b) (a)=min((a), (b))
#define FOR(i,a,b) for(int i = (a); i <= (b); i++)
#define rep(i,n) FOR(i,1,n)
#define rep0(i,n) FOR(i,0,(int)(n)-1)
#define FI first
#define SE second
#define INF 2000000000
#define INFLL 1000000000000000000LL
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAX_N = 200000;
const int MAX_Q = 200000;
int N, Q;
vector<int> gp[MAX_N+1];
int p[MAX_N+1];
int num[MAX_N+1][2];
int cnt = 0;
int size[MAX_N+1];
struct SEG{
SEG(int l, int r, int sum) : l(l), r(r), sum(sum) {}
int l, r, sum;
};
vector<SEG> seg;
void init(){
seg.push_back({-1, -1, 0});
}
void update(int idx, int s, int e, int x, int y){
seg[idx].sum+=y;
if(s==e) return;
if(x<=(s+e)/2){
if(seg[idx].l==-1){
seg[idx].l = seg.size();
}update(seg[idx].l, s, (s+e)/2, x, y);
}else{
if(seg[idx].r==-1){
seg[idx].r = seg.size();
}update(seg[idx].r, (s+e)/2+1, e, x, y);
}
}
int sum(int idx, int s, int e, int x, int y){
if(idx==-1) return 0;
if(x<=s && e<=y) return seg[idx].sum;
if(x>e || y<s) return 0;
return sum(seg[idx].l, s, (s+e)/2, x, y) + sum(seg[idx].r, (s+e)/2+1, e, x, y);
}
void dfs(int x){
size[x] = 1;
num[x][0] = cnt++;
for(auto i : gp[x]){
dfs(i);
size[x] += size[i];
}
num[x][1] = cnt;
}
int group[MAX_N+1];
int gsz[MAX_N+1];
int gtop[MAX_N+1];
int cntg = 2;
void dfs2(int x){
group[x] = cntg;
for(auto i : gp[x]){
if(p[i]!=x) continue;
dfs2(i);
}
}
void cut(int x){
if(p[x]==0) return;/*
if(gsz[group[x]] < 2 * (size[x] - sum(0, 0, N-1, num[x][0]+1, num[x][1]))){
gsz[group[x]] = (size[x] - sum(0, 0, N-1, num[x][0]+1, num[x][1]));
p[x] = 0; gtop[cntg] = gtop[group[x]]; dfs2(gtop[group[x]]);
gtop[group[x]] = x; update(0, 0, N-1, num[x][0], gsz[group[x]]);
}else{
gsz[group[x]] -= (size[x] - sum(0, 0, N-1, num[x][0]+1, num[x][1]));
p[x] = 0; gtop[cntg] = x; p[x] = 0; dfs2(x);
gsz[group[x]] = (size[x] - sum(0, 0, N-1, num[x][0]+1, num[x][1]));
update(0, 0, N-1, num[x][0], gsz[group[x]]);
}*/
p[x] = 0; dfs2(x);
cntg++;
}
int main(){
init();
scanf("%d%d", &N, &Q);
for(int i=2; i<=N; i++){
scanf("%d", &p[i]);
gp[p[i]].pb(i);
}
dfs(1);
for(int i=1; i<=N; i++) group[i] = 1;
gsz[1] = 1;
rep(q, Q){
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
if(group[a]==group[b]) printf("YES\n");
else printf("NO\n");
if(c==1){
if(group[a]==group[b]) cut(a);
else cut(b);
}
}
return 0;
}
Compilation message
tree.cpp: In function 'int main()':
tree.cpp:109:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &N, &Q);
~~~~~^~~~~~~~~~~~~~~~
tree.cpp:111:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &p[i]);
~~~~~^~~~~~~~~~~~~
tree.cpp:119:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d", &a, &b, &c);
~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
5092 KB |
Output is correct |
2 |
Correct |
6 ms |
5120 KB |
Output is correct |
3 |
Correct |
6 ms |
5120 KB |
Output is correct |
4 |
Correct |
6 ms |
5120 KB |
Output is correct |
5 |
Correct |
7 ms |
5120 KB |
Output is correct |
6 |
Correct |
8 ms |
5120 KB |
Output is correct |
7 |
Correct |
7 ms |
5120 KB |
Output is correct |
8 |
Correct |
9 ms |
5120 KB |
Output is correct |
9 |
Correct |
7 ms |
5120 KB |
Output is correct |
10 |
Correct |
9 ms |
5120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
5092 KB |
Output is correct |
2 |
Correct |
6 ms |
5120 KB |
Output is correct |
3 |
Correct |
6 ms |
5120 KB |
Output is correct |
4 |
Correct |
6 ms |
5120 KB |
Output is correct |
5 |
Correct |
7 ms |
5120 KB |
Output is correct |
6 |
Correct |
8 ms |
5120 KB |
Output is correct |
7 |
Correct |
7 ms |
5120 KB |
Output is correct |
8 |
Correct |
9 ms |
5120 KB |
Output is correct |
9 |
Correct |
7 ms |
5120 KB |
Output is correct |
10 |
Correct |
9 ms |
5120 KB |
Output is correct |
11 |
Correct |
8 ms |
5120 KB |
Output is correct |
12 |
Correct |
9 ms |
5120 KB |
Output is correct |
13 |
Correct |
6 ms |
5120 KB |
Output is correct |
14 |
Correct |
8 ms |
5120 KB |
Output is correct |
15 |
Correct |
8 ms |
5120 KB |
Output is correct |
16 |
Correct |
8 ms |
5120 KB |
Output is correct |
17 |
Correct |
9 ms |
5120 KB |
Output is correct |
18 |
Correct |
9 ms |
5120 KB |
Output is correct |
19 |
Correct |
8 ms |
5120 KB |
Output is correct |
20 |
Correct |
5 ms |
5120 KB |
Output is correct |
21 |
Correct |
9 ms |
5120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
5092 KB |
Output is correct |
2 |
Correct |
6 ms |
5120 KB |
Output is correct |
3 |
Correct |
6 ms |
5120 KB |
Output is correct |
4 |
Correct |
6 ms |
5120 KB |
Output is correct |
5 |
Correct |
7 ms |
5120 KB |
Output is correct |
6 |
Correct |
8 ms |
5120 KB |
Output is correct |
7 |
Correct |
7 ms |
5120 KB |
Output is correct |
8 |
Correct |
9 ms |
5120 KB |
Output is correct |
9 |
Correct |
7 ms |
5120 KB |
Output is correct |
10 |
Correct |
9 ms |
5120 KB |
Output is correct |
11 |
Correct |
8 ms |
5120 KB |
Output is correct |
12 |
Correct |
9 ms |
5120 KB |
Output is correct |
13 |
Correct |
6 ms |
5120 KB |
Output is correct |
14 |
Correct |
8 ms |
5120 KB |
Output is correct |
15 |
Correct |
8 ms |
5120 KB |
Output is correct |
16 |
Correct |
8 ms |
5120 KB |
Output is correct |
17 |
Correct |
9 ms |
5120 KB |
Output is correct |
18 |
Correct |
9 ms |
5120 KB |
Output is correct |
19 |
Correct |
8 ms |
5120 KB |
Output is correct |
20 |
Correct |
5 ms |
5120 KB |
Output is correct |
21 |
Correct |
9 ms |
5120 KB |
Output is correct |
22 |
Correct |
73 ms |
8056 KB |
Output is correct |
23 |
Correct |
87 ms |
7928 KB |
Output is correct |
24 |
Correct |
87 ms |
7928 KB |
Output is correct |
25 |
Correct |
102 ms |
7912 KB |
Output is correct |
26 |
Correct |
91 ms |
7948 KB |
Output is correct |
27 |
Correct |
91 ms |
8052 KB |
Output is correct |
28 |
Correct |
89 ms |
8064 KB |
Output is correct |
29 |
Correct |
68 ms |
8056 KB |
Output is correct |
30 |
Correct |
102 ms |
8184 KB |
Output is correct |
31 |
Correct |
88 ms |
8200 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
5092 KB |
Output is correct |
2 |
Correct |
6 ms |
5120 KB |
Output is correct |
3 |
Correct |
6 ms |
5120 KB |
Output is correct |
4 |
Correct |
6 ms |
5120 KB |
Output is correct |
5 |
Correct |
7 ms |
5120 KB |
Output is correct |
6 |
Correct |
8 ms |
5120 KB |
Output is correct |
7 |
Correct |
7 ms |
5120 KB |
Output is correct |
8 |
Correct |
9 ms |
5120 KB |
Output is correct |
9 |
Correct |
7 ms |
5120 KB |
Output is correct |
10 |
Correct |
9 ms |
5120 KB |
Output is correct |
11 |
Correct |
990 ms |
29616 KB |
Output is correct |
12 |
Correct |
915 ms |
29508 KB |
Output is correct |
13 |
Correct |
883 ms |
29432 KB |
Output is correct |
14 |
Correct |
824 ms |
29432 KB |
Output is correct |
15 |
Correct |
882 ms |
29432 KB |
Output is correct |
16 |
Correct |
719 ms |
29664 KB |
Output is correct |
17 |
Execution timed out |
2081 ms |
26276 KB |
Time limit exceeded |
18 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
5092 KB |
Output is correct |
2 |
Correct |
6 ms |
5120 KB |
Output is correct |
3 |
Correct |
6 ms |
5120 KB |
Output is correct |
4 |
Correct |
6 ms |
5120 KB |
Output is correct |
5 |
Correct |
7 ms |
5120 KB |
Output is correct |
6 |
Correct |
8 ms |
5120 KB |
Output is correct |
7 |
Correct |
7 ms |
5120 KB |
Output is correct |
8 |
Correct |
9 ms |
5120 KB |
Output is correct |
9 |
Correct |
7 ms |
5120 KB |
Output is correct |
10 |
Correct |
9 ms |
5120 KB |
Output is correct |
11 |
Correct |
8 ms |
5120 KB |
Output is correct |
12 |
Correct |
9 ms |
5120 KB |
Output is correct |
13 |
Correct |
6 ms |
5120 KB |
Output is correct |
14 |
Correct |
8 ms |
5120 KB |
Output is correct |
15 |
Correct |
8 ms |
5120 KB |
Output is correct |
16 |
Correct |
8 ms |
5120 KB |
Output is correct |
17 |
Correct |
9 ms |
5120 KB |
Output is correct |
18 |
Correct |
9 ms |
5120 KB |
Output is correct |
19 |
Correct |
8 ms |
5120 KB |
Output is correct |
20 |
Correct |
5 ms |
5120 KB |
Output is correct |
21 |
Correct |
9 ms |
5120 KB |
Output is correct |
22 |
Correct |
73 ms |
8056 KB |
Output is correct |
23 |
Correct |
87 ms |
7928 KB |
Output is correct |
24 |
Correct |
87 ms |
7928 KB |
Output is correct |
25 |
Correct |
102 ms |
7912 KB |
Output is correct |
26 |
Correct |
91 ms |
7948 KB |
Output is correct |
27 |
Correct |
91 ms |
8052 KB |
Output is correct |
28 |
Correct |
89 ms |
8064 KB |
Output is correct |
29 |
Correct |
68 ms |
8056 KB |
Output is correct |
30 |
Correct |
102 ms |
8184 KB |
Output is correct |
31 |
Correct |
88 ms |
8200 KB |
Output is correct |
32 |
Correct |
990 ms |
29616 KB |
Output is correct |
33 |
Correct |
915 ms |
29508 KB |
Output is correct |
34 |
Correct |
883 ms |
29432 KB |
Output is correct |
35 |
Correct |
824 ms |
29432 KB |
Output is correct |
36 |
Correct |
882 ms |
29432 KB |
Output is correct |
37 |
Correct |
719 ms |
29664 KB |
Output is correct |
38 |
Execution timed out |
2081 ms |
26276 KB |
Time limit exceeded |
39 |
Halted |
0 ms |
0 KB |
- |