Submission #97483

# Submission time Handle Problem Language Result Execution time Memory
97483 2019-02-16T11:39:51 Z Retro3014 None (KOI16_tree) C++11
36 / 100
2000 ms 25636 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[cntg] = gsz[group[x]] - (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[cntg]);
		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]));
		gsz[cntg] = (size[x] - sum(0, 0, N-1, num[x][0]+1, num[x][1]));
		p[x] = 0; gtop[cntg] = x; dfs2(x);
		update(0, 0, N-1, num[x][0], gsz[cntg]);
	//}
	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 7 ms 5120 KB Output is correct
2 Correct 10 ms 5248 KB Output is correct
3 Correct 8 ms 5120 KB Output is correct
4 Correct 8 ms 5120 KB Output is correct
5 Correct 8 ms 5120 KB Output is correct
6 Correct 8 ms 5120 KB Output is correct
7 Correct 7 ms 4992 KB Output is correct
8 Correct 6 ms 5120 KB Output is correct
9 Correct 7 ms 5248 KB Output is correct
10 Correct 7 ms 5120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5120 KB Output is correct
2 Correct 10 ms 5248 KB Output is correct
3 Correct 8 ms 5120 KB Output is correct
4 Correct 8 ms 5120 KB Output is correct
5 Correct 8 ms 5120 KB Output is correct
6 Correct 8 ms 5120 KB Output is correct
7 Correct 7 ms 4992 KB Output is correct
8 Correct 6 ms 5120 KB Output is correct
9 Correct 7 ms 5248 KB Output is correct
10 Correct 7 ms 5120 KB Output is correct
11 Correct 7 ms 5120 KB Output is correct
12 Correct 7 ms 5092 KB Output is correct
13 Correct 8 ms 5092 KB Output is correct
14 Correct 8 ms 5120 KB Output is correct
15 Correct 7 ms 5120 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 8 ms 5120 KB Output is correct
20 Correct 8 ms 5120 KB Output is correct
21 Correct 8 ms 5120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5120 KB Output is correct
2 Correct 10 ms 5248 KB Output is correct
3 Correct 8 ms 5120 KB Output is correct
4 Correct 8 ms 5120 KB Output is correct
5 Correct 8 ms 5120 KB Output is correct
6 Correct 8 ms 5120 KB Output is correct
7 Correct 7 ms 4992 KB Output is correct
8 Correct 6 ms 5120 KB Output is correct
9 Correct 7 ms 5248 KB Output is correct
10 Correct 7 ms 5120 KB Output is correct
11 Correct 7 ms 5120 KB Output is correct
12 Correct 7 ms 5092 KB Output is correct
13 Correct 8 ms 5092 KB Output is correct
14 Correct 8 ms 5120 KB Output is correct
15 Correct 7 ms 5120 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 8 ms 5120 KB Output is correct
20 Correct 8 ms 5120 KB Output is correct
21 Correct 8 ms 5120 KB Output is correct
22 Correct 73 ms 5752 KB Output is correct
23 Correct 83 ms 5808 KB Output is correct
24 Correct 77 ms 5752 KB Output is correct
25 Correct 79 ms 5728 KB Output is correct
26 Correct 104 ms 5804 KB Output is correct
27 Correct 91 ms 5880 KB Output is correct
28 Correct 83 ms 5824 KB Output is correct
29 Correct 112 ms 5880 KB Output is correct
30 Correct 65 ms 6008 KB Output is correct
31 Correct 110 ms 5880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5120 KB Output is correct
2 Correct 10 ms 5248 KB Output is correct
3 Correct 8 ms 5120 KB Output is correct
4 Correct 8 ms 5120 KB Output is correct
5 Correct 8 ms 5120 KB Output is correct
6 Correct 8 ms 5120 KB Output is correct
7 Correct 7 ms 4992 KB Output is correct
8 Correct 6 ms 5120 KB Output is correct
9 Correct 7 ms 5248 KB Output is correct
10 Correct 7 ms 5120 KB Output is correct
11 Correct 855 ms 25496 KB Output is correct
12 Correct 815 ms 25592 KB Output is correct
13 Correct 984 ms 25636 KB Output is correct
14 Correct 887 ms 25468 KB Output is correct
15 Correct 964 ms 25508 KB Output is correct
16 Correct 771 ms 25592 KB Output is correct
17 Execution timed out 2078 ms 24568 KB Time limit exceeded
18 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5120 KB Output is correct
2 Correct 10 ms 5248 KB Output is correct
3 Correct 8 ms 5120 KB Output is correct
4 Correct 8 ms 5120 KB Output is correct
5 Correct 8 ms 5120 KB Output is correct
6 Correct 8 ms 5120 KB Output is correct
7 Correct 7 ms 4992 KB Output is correct
8 Correct 6 ms 5120 KB Output is correct
9 Correct 7 ms 5248 KB Output is correct
10 Correct 7 ms 5120 KB Output is correct
11 Correct 7 ms 5120 KB Output is correct
12 Correct 7 ms 5092 KB Output is correct
13 Correct 8 ms 5092 KB Output is correct
14 Correct 8 ms 5120 KB Output is correct
15 Correct 7 ms 5120 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 8 ms 5120 KB Output is correct
20 Correct 8 ms 5120 KB Output is correct
21 Correct 8 ms 5120 KB Output is correct
22 Correct 73 ms 5752 KB Output is correct
23 Correct 83 ms 5808 KB Output is correct
24 Correct 77 ms 5752 KB Output is correct
25 Correct 79 ms 5728 KB Output is correct
26 Correct 104 ms 5804 KB Output is correct
27 Correct 91 ms 5880 KB Output is correct
28 Correct 83 ms 5824 KB Output is correct
29 Correct 112 ms 5880 KB Output is correct
30 Correct 65 ms 6008 KB Output is correct
31 Correct 110 ms 5880 KB Output is correct
32 Correct 855 ms 25496 KB Output is correct
33 Correct 815 ms 25592 KB Output is correct
34 Correct 984 ms 25636 KB Output is correct
35 Correct 887 ms 25468 KB Output is correct
36 Correct 964 ms 25508 KB Output is correct
37 Correct 771 ms 25592 KB Output is correct
38 Execution timed out 2078 ms 24568 KB Time limit exceeded
39 Halted 0 ms 0 KB -