답안 #46425

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
46425 2018-04-20T15:26:28 Z leejseo 트리 (KOI16_tree) C++17
55 / 100
2000 ms 51088 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;
		//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:65:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    for (int i=0; i<dec[v1].size(); i++){
                  ~^~~~~~~~~~~~~~~
tree.cpp:75:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    for (int i=0; i<dec[v2].size(); i++){
                  ~^~~~~~~~~~~~~~~
tree.cpp:86:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int i=0; i<L1.size(); i++){
                 ~^~~~~~~~~~
tree.cpp:92: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:105: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 7 ms 5116 KB Output is correct
3 Correct 6 ms 5196 KB Output is correct
4 Correct 6 ms 5264 KB Output is correct
5 Correct 6 ms 5312 KB Output is correct
6 Correct 7 ms 5328 KB Output is correct
7 Correct 7 ms 5336 KB Output is correct
8 Correct 7 ms 5340 KB Output is correct
9 Correct 6 ms 5376 KB Output is correct
10 Correct 6 ms 5392 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 7 ms 5116 KB Output is correct
3 Correct 6 ms 5196 KB Output is correct
4 Correct 6 ms 5264 KB Output is correct
5 Correct 6 ms 5312 KB Output is correct
6 Correct 7 ms 5328 KB Output is correct
7 Correct 7 ms 5336 KB Output is correct
8 Correct 7 ms 5340 KB Output is correct
9 Correct 6 ms 5376 KB Output is correct
10 Correct 6 ms 5392 KB Output is correct
11 Correct 6 ms 5392 KB Output is correct
12 Correct 6 ms 5392 KB Output is correct
13 Correct 8 ms 5392 KB Output is correct
14 Correct 6 ms 5464 KB Output is correct
15 Correct 8 ms 5464 KB Output is correct
16 Correct 7 ms 5512 KB Output is correct
17 Correct 6 ms 5544 KB Output is correct
18 Correct 7 ms 5544 KB Output is correct
19 Correct 6 ms 5576 KB Output is correct
20 Correct 6 ms 5576 KB Output is correct
21 Correct 6 ms 5624 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 7 ms 5116 KB Output is correct
3 Correct 6 ms 5196 KB Output is correct
4 Correct 6 ms 5264 KB Output is correct
5 Correct 6 ms 5312 KB Output is correct
6 Correct 7 ms 5328 KB Output is correct
7 Correct 7 ms 5336 KB Output is correct
8 Correct 7 ms 5340 KB Output is correct
9 Correct 6 ms 5376 KB Output is correct
10 Correct 6 ms 5392 KB Output is correct
11 Correct 6 ms 5392 KB Output is correct
12 Correct 6 ms 5392 KB Output is correct
13 Correct 8 ms 5392 KB Output is correct
14 Correct 6 ms 5464 KB Output is correct
15 Correct 8 ms 5464 KB Output is correct
16 Correct 7 ms 5512 KB Output is correct
17 Correct 6 ms 5544 KB Output is correct
18 Correct 7 ms 5544 KB Output is correct
19 Correct 6 ms 5576 KB Output is correct
20 Correct 6 ms 5576 KB Output is correct
21 Correct 6 ms 5624 KB Output is correct
22 Correct 79 ms 8484 KB Output is correct
23 Correct 94 ms 10740 KB Output is correct
24 Correct 78 ms 12896 KB Output is correct
25 Correct 83 ms 15204 KB Output is correct
26 Correct 117 ms 17388 KB Output is correct
27 Correct 78 ms 19700 KB Output is correct
28 Correct 85 ms 22088 KB Output is correct
29 Correct 84 ms 24176 KB Output is correct
30 Correct 82 ms 26420 KB Output is correct
31 Correct 76 ms 28632 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 7 ms 5116 KB Output is correct
3 Correct 6 ms 5196 KB Output is correct
4 Correct 6 ms 5264 KB Output is correct
5 Correct 6 ms 5312 KB Output is correct
6 Correct 7 ms 5328 KB Output is correct
7 Correct 7 ms 5336 KB Output is correct
8 Correct 7 ms 5340 KB Output is correct
9 Correct 6 ms 5376 KB Output is correct
10 Correct 6 ms 5392 KB Output is correct
11 Correct 441 ms 41404 KB Output is correct
12 Correct 434 ms 45996 KB Output is correct
13 Correct 435 ms 49844 KB Output is correct
14 Correct 469 ms 50968 KB Output is correct
15 Correct 436 ms 51088 KB Output is correct
16 Correct 417 ms 51088 KB Output is correct
17 Correct 238 ms 51088 KB Output is correct
18 Correct 279 ms 51088 KB Output is correct
19 Correct 273 ms 51088 KB Output is correct
20 Correct 222 ms 51088 KB Output is correct
21 Correct 443 ms 51088 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 7 ms 5116 KB Output is correct
3 Correct 6 ms 5196 KB Output is correct
4 Correct 6 ms 5264 KB Output is correct
5 Correct 6 ms 5312 KB Output is correct
6 Correct 7 ms 5328 KB Output is correct
7 Correct 7 ms 5336 KB Output is correct
8 Correct 7 ms 5340 KB Output is correct
9 Correct 6 ms 5376 KB Output is correct
10 Correct 6 ms 5392 KB Output is correct
11 Correct 6 ms 5392 KB Output is correct
12 Correct 6 ms 5392 KB Output is correct
13 Correct 8 ms 5392 KB Output is correct
14 Correct 6 ms 5464 KB Output is correct
15 Correct 8 ms 5464 KB Output is correct
16 Correct 7 ms 5512 KB Output is correct
17 Correct 6 ms 5544 KB Output is correct
18 Correct 7 ms 5544 KB Output is correct
19 Correct 6 ms 5576 KB Output is correct
20 Correct 6 ms 5576 KB Output is correct
21 Correct 6 ms 5624 KB Output is correct
22 Correct 79 ms 8484 KB Output is correct
23 Correct 94 ms 10740 KB Output is correct
24 Correct 78 ms 12896 KB Output is correct
25 Correct 83 ms 15204 KB Output is correct
26 Correct 117 ms 17388 KB Output is correct
27 Correct 78 ms 19700 KB Output is correct
28 Correct 85 ms 22088 KB Output is correct
29 Correct 84 ms 24176 KB Output is correct
30 Correct 82 ms 26420 KB Output is correct
31 Correct 76 ms 28632 KB Output is correct
32 Correct 441 ms 41404 KB Output is correct
33 Correct 434 ms 45996 KB Output is correct
34 Correct 435 ms 49844 KB Output is correct
35 Correct 469 ms 50968 KB Output is correct
36 Correct 436 ms 51088 KB Output is correct
37 Correct 417 ms 51088 KB Output is correct
38 Correct 238 ms 51088 KB Output is correct
39 Correct 279 ms 51088 KB Output is correct
40 Correct 273 ms 51088 KB Output is correct
41 Correct 222 ms 51088 KB Output is correct
42 Correct 443 ms 51088 KB Output is correct
43 Correct 185 ms 51088 KB Output is correct
44 Correct 267 ms 51088 KB Output is correct
45 Correct 217 ms 51088 KB Output is correct
46 Correct 207 ms 51088 KB Output is correct
47 Correct 194 ms 51088 KB Output is correct
48 Execution timed out 2053 ms 51088 KB Time limit exceeded
49 Halted 0 ms 0 KB -