답안 #46424

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
46424 2018-04-20T15:19:33 Z leejseo 트리 (KOI16_tree) C++14
55 / 100
2000 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;
	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:34:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i=0; i<L1.size(); i++){
                ~^~~~~~~~~~
tree.cpp:37:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i=0; i<L2.size(); i++){
                ~^~~~~~~~~~
tree.cpp:64:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    for (int i=0; i<dec[v1].size(); i++){
                  ~^~~~~~~~~~~~~~~
tree.cpp:74:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    for (int i=0; i<dec[v2].size(); i++){
                  ~^~~~~~~~~~~~~~~
tree.cpp:85:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int i=0; i<L1.size(); i++){
                 ~^~~~~~~~~~
tree.cpp:91: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:104: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 12 ms 4984 KB Output is correct
2 Correct 6 ms 5236 KB Output is correct
3 Correct 6 ms 5376 KB Output is correct
4 Correct 7 ms 5376 KB Output is correct
5 Correct 7 ms 5376 KB Output is correct
6 Correct 6 ms 5452 KB Output is correct
7 Correct 6 ms 5504 KB Output is correct
8 Correct 6 ms 5576 KB Output is correct
9 Correct 6 ms 5644 KB Output is correct
10 Correct 7 ms 5660 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 4984 KB Output is correct
2 Correct 6 ms 5236 KB Output is correct
3 Correct 6 ms 5376 KB Output is correct
4 Correct 7 ms 5376 KB Output is correct
5 Correct 7 ms 5376 KB Output is correct
6 Correct 6 ms 5452 KB Output is correct
7 Correct 6 ms 5504 KB Output is correct
8 Correct 6 ms 5576 KB Output is correct
9 Correct 6 ms 5644 KB Output is correct
10 Correct 7 ms 5660 KB Output is correct
11 Correct 6 ms 5660 KB Output is correct
12 Correct 6 ms 5660 KB Output is correct
13 Correct 6 ms 5660 KB Output is correct
14 Correct 6 ms 5660 KB Output is correct
15 Correct 7 ms 5740 KB Output is correct
16 Correct 7 ms 5740 KB Output is correct
17 Correct 6 ms 5740 KB Output is correct
18 Correct 7 ms 5740 KB Output is correct
19 Correct 7 ms 5740 KB Output is correct
20 Correct 7 ms 5740 KB Output is correct
21 Correct 7 ms 5740 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 4984 KB Output is correct
2 Correct 6 ms 5236 KB Output is correct
3 Correct 6 ms 5376 KB Output is correct
4 Correct 7 ms 5376 KB Output is correct
5 Correct 7 ms 5376 KB Output is correct
6 Correct 6 ms 5452 KB Output is correct
7 Correct 6 ms 5504 KB Output is correct
8 Correct 6 ms 5576 KB Output is correct
9 Correct 6 ms 5644 KB Output is correct
10 Correct 7 ms 5660 KB Output is correct
11 Correct 6 ms 5660 KB Output is correct
12 Correct 6 ms 5660 KB Output is correct
13 Correct 6 ms 5660 KB Output is correct
14 Correct 6 ms 5660 KB Output is correct
15 Correct 7 ms 5740 KB Output is correct
16 Correct 7 ms 5740 KB Output is correct
17 Correct 6 ms 5740 KB Output is correct
18 Correct 7 ms 5740 KB Output is correct
19 Correct 7 ms 5740 KB Output is correct
20 Correct 7 ms 5740 KB Output is correct
21 Correct 7 ms 5740 KB Output is correct
22 Correct 77 ms 8700 KB Output is correct
23 Correct 88 ms 11020 KB Output is correct
24 Correct 79 ms 13120 KB Output is correct
25 Correct 100 ms 15352 KB Output is correct
26 Correct 76 ms 17568 KB Output is correct
27 Correct 76 ms 19780 KB Output is correct
28 Correct 78 ms 21980 KB Output is correct
29 Correct 76 ms 24212 KB Output is correct
30 Correct 74 ms 26568 KB Output is correct
31 Correct 87 ms 28748 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 4984 KB Output is correct
2 Correct 6 ms 5236 KB Output is correct
3 Correct 6 ms 5376 KB Output is correct
4 Correct 7 ms 5376 KB Output is correct
5 Correct 7 ms 5376 KB Output is correct
6 Correct 6 ms 5452 KB Output is correct
7 Correct 6 ms 5504 KB Output is correct
8 Correct 6 ms 5576 KB Output is correct
9 Correct 6 ms 5644 KB Output is correct
10 Correct 7 ms 5660 KB Output is correct
11 Correct 452 ms 41512 KB Output is correct
12 Correct 433 ms 46004 KB Output is correct
13 Correct 414 ms 50112 KB Output is correct
14 Correct 430 ms 54404 KB Output is correct
15 Correct 412 ms 59048 KB Output is correct
16 Correct 426 ms 63048 KB Output is correct
17 Correct 265 ms 65536 KB Output is correct
18 Correct 267 ms 65536 KB Output is correct
19 Correct 262 ms 65536 KB Output is correct
20 Correct 260 ms 65536 KB Output is correct
21 Correct 404 ms 65536 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 4984 KB Output is correct
2 Correct 6 ms 5236 KB Output is correct
3 Correct 6 ms 5376 KB Output is correct
4 Correct 7 ms 5376 KB Output is correct
5 Correct 7 ms 5376 KB Output is correct
6 Correct 6 ms 5452 KB Output is correct
7 Correct 6 ms 5504 KB Output is correct
8 Correct 6 ms 5576 KB Output is correct
9 Correct 6 ms 5644 KB Output is correct
10 Correct 7 ms 5660 KB Output is correct
11 Correct 6 ms 5660 KB Output is correct
12 Correct 6 ms 5660 KB Output is correct
13 Correct 6 ms 5660 KB Output is correct
14 Correct 6 ms 5660 KB Output is correct
15 Correct 7 ms 5740 KB Output is correct
16 Correct 7 ms 5740 KB Output is correct
17 Correct 6 ms 5740 KB Output is correct
18 Correct 7 ms 5740 KB Output is correct
19 Correct 7 ms 5740 KB Output is correct
20 Correct 7 ms 5740 KB Output is correct
21 Correct 7 ms 5740 KB Output is correct
22 Correct 77 ms 8700 KB Output is correct
23 Correct 88 ms 11020 KB Output is correct
24 Correct 79 ms 13120 KB Output is correct
25 Correct 100 ms 15352 KB Output is correct
26 Correct 76 ms 17568 KB Output is correct
27 Correct 76 ms 19780 KB Output is correct
28 Correct 78 ms 21980 KB Output is correct
29 Correct 76 ms 24212 KB Output is correct
30 Correct 74 ms 26568 KB Output is correct
31 Correct 87 ms 28748 KB Output is correct
32 Correct 452 ms 41512 KB Output is correct
33 Correct 433 ms 46004 KB Output is correct
34 Correct 414 ms 50112 KB Output is correct
35 Correct 430 ms 54404 KB Output is correct
36 Correct 412 ms 59048 KB Output is correct
37 Correct 426 ms 63048 KB Output is correct
38 Correct 265 ms 65536 KB Output is correct
39 Correct 267 ms 65536 KB Output is correct
40 Correct 262 ms 65536 KB Output is correct
41 Correct 260 ms 65536 KB Output is correct
42 Correct 404 ms 65536 KB Output is correct
43 Correct 226 ms 65536 KB Output is correct
44 Correct 186 ms 65536 KB Output is correct
45 Correct 195 ms 65536 KB Output is correct
46 Correct 205 ms 65536 KB Output is correct
47 Correct 226 ms 65536 KB Output is correct
48 Execution timed out 2060 ms 65536 KB Time limit exceeded
49 Halted 0 ms 0 KB -