답안 #47869

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
47869 2018-05-08T10:11:25 Z leejseo 트리 (KOI16_tree) C++17
100 / 100
397 ms 65536 KB
#include <stdio.h>
#include <vector>

using namespace std;

#define MAXN 200000

int N, Q;
int par[MAXN+5];
int color[MAXN+5];
vector<int> stack1;
vector<int> stack2;
vector<int> L1;
vector<int> L2;
vector<int> dec[MAXN+5];
bool vis[MAXN+5];
int cc = 1;

void input(){
	scanf("%d%d", &N, &Q);
	par[1] = -1;
	par[0] = -1;
	color[1] = 1;
	for (int i=2; i<=N; i++){
		color[i] = 1;
		scanf("%d", &par[i]);
		dec[par[i]].push_back(i);
	}
}

void delete_edge(int u){
	if (u == 1) return;
	if (par[u] == -1) return;
	if (color[u] != color[par[u]]) return;
	for (int i=0; i<L1.size(); i++){
		vis[L1[i]] = false;
	}
	for (int i=0; i<L2.size(); i++){
		vis[L2[i]] = false;
	}
	L1.clear();
	L2.clear();
	stack1.clear();
	stack2.clear();
	L1.push_back(par[u]);
	stack1.push_back(par[u]);
	L2.push_back(u);
	stack2.push_back(u);
	par[u] = -1;
	while ((stack1.size() >= 1) && (stack2.size() >= 1)){
		int v1 = stack1.back();
		int v2 = stack2.back();
		stack1.pop_back();
		stack2.pop_back();
		vis[v1] = true;
		vis[v2] = true;
		if ((par[v1] == -1) && (dec[v1].empty()) && (stack1.empty())){
			break;
		}
		if ((dec[v2].empty()) && (stack2.empty())) {
			stack1.push_back(1);
			break;		
		}
		//for stack1
		if (par[v1] != -1) {
			if ((color[par[v1]] == color[v1]) && (!vis[par[v1]])){
				stack1.push_back(par[v1]);
				L1.push_back(par[v1]);
			}
		}
		if (dec[v1].size() > 0){
			for (int i=0; i<dec[v1].size(); i++){
				int w = dec[v1][i];
				if ((color[w] == color[v1]) && (!vis[w])){
					L1.push_back(w);
					stack1.push_back(w);
				}
			}
		}
		// for stack2
		if (dec[v2].size() > 0){
			for (int i=0; i<dec[v2].size(); i++){
				int w = dec[v2][i];
				if ((color[w] == color[v2]) && (!vis[w])){
					L2.push_back(w);
					stack2.push_back(w);
				}
			}
		}
	}
	if (stack1.empty()){
		// connected component w/ parent is smaller
		for (int i=0; i<L1.size(); i++){
			color[L1[i]] = cc + 1;
		}
	}
	else{
		// connected component w/ u is smaller
		for (int i=0; i<L2.size(); i++){
			color[L2[i]] = cc + 1;
		}
	}
	cc += 1;
}

//Deal with query

int main(void){
	input();
	for (int query=0; query<Q; query++){
		int u, v, q;
		scanf("%d%d%d", &u, &v, &q);
		if (color[u] == color[v]){
			printf("YES\n");
			if (q) delete_edge(u);
		}
		else{
			printf("NO\n");
			if (q) delete_edge(v);
		}
	}
}

Compilation message

tree.cpp: In function 'void delete_edge(int)':
tree.cpp:35:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i=0; i<L1.size(); i++){
                ~^~~~~~~~~~
tree.cpp:38:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i=0; i<L2.size(); i++){
                ~^~~~~~~~~~
tree.cpp:72:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    for (int i=0; i<dec[v1].size(); i++){
                  ~^~~~~~~~~~~~~~~
tree.cpp:82:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    for (int i=0; i<dec[v2].size(); i++){
                  ~^~~~~~~~~~~~~~~
tree.cpp:93:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int i=0; i<L1.size(); i++){
                 ~^~~~~~~~~~
tree.cpp:99:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int i=0; i<L2.size(); i++){
                 ~^~~~~~~~~~
tree.cpp: In function 'void input()':
tree.cpp:20: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:26:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &par[i]);
   ~~~~~^~~~~~~~~~~~~~~
tree.cpp: In function 'int main()':
tree.cpp:112:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d%d", &u, &v, &q);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 6 ms 5220 KB Output is correct
3 Correct 6 ms 5384 KB Output is correct
4 Correct 6 ms 5404 KB Output is correct
5 Correct 6 ms 5404 KB Output is correct
6 Correct 6 ms 5404 KB Output is correct
7 Correct 6 ms 5404 KB Output is correct
8 Correct 6 ms 5404 KB Output is correct
9 Correct 6 ms 5480 KB Output is correct
10 Correct 6 ms 5536 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 6 ms 5220 KB Output is correct
3 Correct 6 ms 5384 KB Output is correct
4 Correct 6 ms 5404 KB Output is correct
5 Correct 6 ms 5404 KB Output is correct
6 Correct 6 ms 5404 KB Output is correct
7 Correct 6 ms 5404 KB Output is correct
8 Correct 6 ms 5404 KB Output is correct
9 Correct 6 ms 5480 KB Output is correct
10 Correct 6 ms 5536 KB Output is correct
11 Correct 6 ms 5536 KB Output is correct
12 Correct 6 ms 5656 KB Output is correct
13 Correct 6 ms 5656 KB Output is correct
14 Correct 6 ms 5708 KB Output is correct
15 Correct 6 ms 5708 KB Output is correct
16 Correct 6 ms 5708 KB Output is correct
17 Correct 6 ms 5708 KB Output is correct
18 Correct 6 ms 5708 KB Output is correct
19 Correct 6 ms 5708 KB Output is correct
20 Correct 6 ms 5708 KB Output is correct
21 Correct 6 ms 5716 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 6 ms 5220 KB Output is correct
3 Correct 6 ms 5384 KB Output is correct
4 Correct 6 ms 5404 KB Output is correct
5 Correct 6 ms 5404 KB Output is correct
6 Correct 6 ms 5404 KB Output is correct
7 Correct 6 ms 5404 KB Output is correct
8 Correct 6 ms 5404 KB Output is correct
9 Correct 6 ms 5480 KB Output is correct
10 Correct 6 ms 5536 KB Output is correct
11 Correct 6 ms 5536 KB Output is correct
12 Correct 6 ms 5656 KB Output is correct
13 Correct 6 ms 5656 KB Output is correct
14 Correct 6 ms 5708 KB Output is correct
15 Correct 6 ms 5708 KB Output is correct
16 Correct 6 ms 5708 KB Output is correct
17 Correct 6 ms 5708 KB Output is correct
18 Correct 6 ms 5708 KB Output is correct
19 Correct 6 ms 5708 KB Output is correct
20 Correct 6 ms 5708 KB Output is correct
21 Correct 6 ms 5716 KB Output is correct
22 Correct 81 ms 8916 KB Output is correct
23 Correct 77 ms 10980 KB Output is correct
24 Correct 79 ms 13308 KB Output is correct
25 Correct 78 ms 15608 KB Output is correct
26 Correct 78 ms 17688 KB Output is correct
27 Correct 78 ms 19912 KB Output is correct
28 Correct 74 ms 22140 KB Output is correct
29 Correct 74 ms 24356 KB Output is correct
30 Correct 72 ms 26716 KB Output is correct
31 Correct 80 ms 28928 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 6 ms 5220 KB Output is correct
3 Correct 6 ms 5384 KB Output is correct
4 Correct 6 ms 5404 KB Output is correct
5 Correct 6 ms 5404 KB Output is correct
6 Correct 6 ms 5404 KB Output is correct
7 Correct 6 ms 5404 KB Output is correct
8 Correct 6 ms 5404 KB Output is correct
9 Correct 6 ms 5480 KB Output is correct
10 Correct 6 ms 5536 KB Output is correct
11 Correct 361 ms 41676 KB Output is correct
12 Correct 387 ms 46320 KB Output is correct
13 Correct 371 ms 50204 KB Output is correct
14 Correct 397 ms 54528 KB Output is correct
15 Correct 343 ms 59068 KB Output is correct
16 Correct 357 ms 63096 KB Output is correct
17 Correct 222 ms 65536 KB Output is correct
18 Correct 238 ms 65536 KB Output is correct
19 Correct 233 ms 65536 KB Output is correct
20 Correct 217 ms 65536 KB Output is correct
21 Correct 362 ms 65536 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 6 ms 5220 KB Output is correct
3 Correct 6 ms 5384 KB Output is correct
4 Correct 6 ms 5404 KB Output is correct
5 Correct 6 ms 5404 KB Output is correct
6 Correct 6 ms 5404 KB Output is correct
7 Correct 6 ms 5404 KB Output is correct
8 Correct 6 ms 5404 KB Output is correct
9 Correct 6 ms 5480 KB Output is correct
10 Correct 6 ms 5536 KB Output is correct
11 Correct 6 ms 5536 KB Output is correct
12 Correct 6 ms 5656 KB Output is correct
13 Correct 6 ms 5656 KB Output is correct
14 Correct 6 ms 5708 KB Output is correct
15 Correct 6 ms 5708 KB Output is correct
16 Correct 6 ms 5708 KB Output is correct
17 Correct 6 ms 5708 KB Output is correct
18 Correct 6 ms 5708 KB Output is correct
19 Correct 6 ms 5708 KB Output is correct
20 Correct 6 ms 5708 KB Output is correct
21 Correct 6 ms 5716 KB Output is correct
22 Correct 81 ms 8916 KB Output is correct
23 Correct 77 ms 10980 KB Output is correct
24 Correct 79 ms 13308 KB Output is correct
25 Correct 78 ms 15608 KB Output is correct
26 Correct 78 ms 17688 KB Output is correct
27 Correct 78 ms 19912 KB Output is correct
28 Correct 74 ms 22140 KB Output is correct
29 Correct 74 ms 24356 KB Output is correct
30 Correct 72 ms 26716 KB Output is correct
31 Correct 80 ms 28928 KB Output is correct
32 Correct 361 ms 41676 KB Output is correct
33 Correct 387 ms 46320 KB Output is correct
34 Correct 371 ms 50204 KB Output is correct
35 Correct 397 ms 54528 KB Output is correct
36 Correct 343 ms 59068 KB Output is correct
37 Correct 357 ms 63096 KB Output is correct
38 Correct 222 ms 65536 KB Output is correct
39 Correct 238 ms 65536 KB Output is correct
40 Correct 233 ms 65536 KB Output is correct
41 Correct 217 ms 65536 KB Output is correct
42 Correct 362 ms 65536 KB Output is correct
43 Correct 174 ms 65536 KB Output is correct
44 Correct 174 ms 65536 KB Output is correct
45 Correct 171 ms 65536 KB Output is correct
46 Correct 181 ms 65536 KB Output is correct
47 Correct 213 ms 65536 KB Output is correct
48 Correct 118 ms 65536 KB Output is correct
49 Correct 117 ms 65536 KB Output is correct
50 Correct 120 ms 65536 KB Output is correct
51 Correct 283 ms 65536 KB Output is correct