답안 #97486

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
97486 2019-02-16T11:46:13 Z Retro3014 트리 (KOI16_tree) C++11
36 / 100
2000 ms 25592 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;
	//cout<<x<<' '<<gtop[group[x]]<<' '<<group[x]<<endl;
	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] = N; gtop[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:110: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:112:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &p[i]);
   ~~~~~^~~~~~~~~~~~~
tree.cpp:120: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);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 5120 KB Output is correct
2 Correct 8 ms 5120 KB Output is correct
3 Correct 7 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 7 ms 5120 KB Output is correct
7 Correct 7 ms 5120 KB Output is correct
8 Correct 6 ms 5120 KB Output is correct
9 Correct 8 ms 5248 KB Output is correct
10 Correct 7 ms 5120 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 5120 KB Output is correct
2 Correct 8 ms 5120 KB Output is correct
3 Correct 7 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 7 ms 5120 KB Output is correct
7 Correct 7 ms 5120 KB Output is correct
8 Correct 6 ms 5120 KB Output is correct
9 Correct 8 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 9 ms 5120 KB Output is correct
13 Correct 7 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 9 ms 5120 KB Output is correct
17 Correct 8 ms 5248 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
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 5120 KB Output is correct
2 Correct 8 ms 5120 KB Output is correct
3 Correct 7 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 7 ms 5120 KB Output is correct
7 Correct 7 ms 5120 KB Output is correct
8 Correct 6 ms 5120 KB Output is correct
9 Correct 8 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 9 ms 5120 KB Output is correct
13 Correct 7 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 9 ms 5120 KB Output is correct
17 Correct 8 ms 5248 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 90 ms 5724 KB Output is correct
23 Correct 95 ms 5820 KB Output is correct
24 Correct 84 ms 5752 KB Output is correct
25 Correct 85 ms 5752 KB Output is correct
26 Correct 79 ms 5752 KB Output is correct
27 Correct 87 ms 5880 KB Output is correct
28 Correct 95 ms 5880 KB Output is correct
29 Correct 90 ms 5752 KB Output is correct
30 Correct 72 ms 5884 KB Output is correct
31 Correct 86 ms 5844 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 5120 KB Output is correct
2 Correct 8 ms 5120 KB Output is correct
3 Correct 7 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 7 ms 5120 KB Output is correct
7 Correct 7 ms 5120 KB Output is correct
8 Correct 6 ms 5120 KB Output is correct
9 Correct 8 ms 5248 KB Output is correct
10 Correct 7 ms 5120 KB Output is correct
11 Correct 832 ms 25588 KB Output is correct
12 Correct 894 ms 25584 KB Output is correct
13 Correct 975 ms 25464 KB Output is correct
14 Correct 917 ms 25592 KB Output is correct
15 Correct 924 ms 25592 KB Output is correct
16 Correct 780 ms 25404 KB Output is correct
17 Execution timed out 2073 ms 24572 KB Time limit exceeded
18 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 5120 KB Output is correct
2 Correct 8 ms 5120 KB Output is correct
3 Correct 7 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 7 ms 5120 KB Output is correct
7 Correct 7 ms 5120 KB Output is correct
8 Correct 6 ms 5120 KB Output is correct
9 Correct 8 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 9 ms 5120 KB Output is correct
13 Correct 7 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 9 ms 5120 KB Output is correct
17 Correct 8 ms 5248 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 90 ms 5724 KB Output is correct
23 Correct 95 ms 5820 KB Output is correct
24 Correct 84 ms 5752 KB Output is correct
25 Correct 85 ms 5752 KB Output is correct
26 Correct 79 ms 5752 KB Output is correct
27 Correct 87 ms 5880 KB Output is correct
28 Correct 95 ms 5880 KB Output is correct
29 Correct 90 ms 5752 KB Output is correct
30 Correct 72 ms 5884 KB Output is correct
31 Correct 86 ms 5844 KB Output is correct
32 Correct 832 ms 25588 KB Output is correct
33 Correct 894 ms 25584 KB Output is correct
34 Correct 975 ms 25464 KB Output is correct
35 Correct 917 ms 25592 KB Output is correct
36 Correct 924 ms 25592 KB Output is correct
37 Correct 780 ms 25404 KB Output is correct
38 Execution timed out 2073 ms 24572 KB Time limit exceeded
39 Halted 0 ms 0 KB -