답안 #20643

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
20643 2017-02-13T06:32:55 Z model_code 트리 (KOI16_tree) C++11
100 / 100
849 ms 31200 KB
#include<stdio.h>
#include<vector>
#include<deque>

#define MAX 400100
#pragma warning(disable:4996)

using namespace std;

struct xy {
int x, y, z;
};

int labelw;
int label[MAX];
vector<xy>edge[MAX];

deque<int>st1;
bool is_gone[MAX];

int cut1, cut2;
int parent[MAX];
int ep[2][MAX];

void un_check(int w) {
int i;
is_gone[w] = 0;
for (i = 0; i < edge[w].size(); i++) {
if (is_gone[edge[w][i].x] == 0) continue;
un_check(edge[w][i].x);
}
}

void labeling(int w) {
int i;
is_gone[w] = 1;
label[w] = labelw;
for (i = 0; i < edge[w].size(); i++) {
if (is_gone[edge[w][i].x]) continue;
labeling(edge[w][i].x);
}
}
vector<int>stw[2];
vector<int>sti[2];
vector<int>done;


int main() {
int n, q;
int i, j;
int a, b;
int P;
int flag;
int noww, nowi;

scanf("%d%d", &n, &q);
labelw = 1;
for (i = 1; i <= n; i++) label[i] = labelw;
labelw++;

parent[1] = -1;

for (i = 2; i <= n; i++) {
scanf("%d", &parent[i]);
ep[0][i] = edge[i].size();
ep[1][i] = edge[parent[i]].size();
edge[i].push_back({ parent[i], i ,0});
edge[parent[i]].push_back({ i, i ,1 });
}

for (i = 0; i < q; i++) {
scanf("%d%d%d", &a, &b, &flag);
if (flag == 0) {
if (label[a] == label[b]) printf("YES\n");
else printf("NO\n");
continue;
}
if (label[a] == label[b]) {
printf("YES\n");
cut1 = a;
cut2 = parent[a];

}
else {
printf("NO\n");
cut1 = b;
cut2 = parent[b];
}
if (cut2 == -1) continue;
if (ep[0][cut1] == -1) continue;

if (ep[0][cut1] == edge[cut1].size() - 1) edge[cut1].pop_back();
else {
edge[cut1][ep[0][cut1]] = edge[cut1].back();
edge[cut1].pop_back();
ep[edge[cut1][ep[0][cut1]].z][edge[cut1][ep[0][cut1]].y] = ep[0][cut1];
}

if (ep[1][cut1] == edge[cut2].size() - 1) edge[cut2].pop_back();
else {
edge[cut2][ep[1][cut1]] = edge[cut2].back();
edge[cut2].pop_back();
ep[edge[cut2][ep[1][cut1]].z][edge[cut2][ep[1][cut1]].y] = ep[1][cut1];
}
ep[0][cut1] = -1;
ep[1][cut1] = -1;

stw[0].push_back(cut1); sti[0].push_back(0);
stw[1].push_back(cut2); sti[1].push_back(0);
flag = 0;
while (1) {
if (stw[flag].empty())
break;
noww = stw[flag].back();
nowi = sti[flag].back();
if(nowi==0)
done.push_back(noww);
stw[flag].pop_back();
sti[flag].pop_back();
is_gone[noww] = 1;
while (1) {
if (nowi == edge[noww].size()) break;
if (is_gone[edge[noww][nowi].x]) {
nowi++;
continue;
}
break;
}
if (nowi == edge[noww].size()) continue;
stw[flag].push_back(noww);
sti[flag].push_back(nowi + 1);
stw[flag].push_back(edge[noww][nowi].x);
sti[flag].push_back(0);
flag = !flag;
}
for (j = 0; j < done.size(); j++) is_gone[done[j]] = 0;
done.clear();
stw[0].clear(); sti[0].clear();
stw[1].clear(); sti[1].clear();
if (flag == 0) {
labeling(cut1);
labelw++;
un_check(cut1);
}
else {
labeling(cut2);
labelw++;
un_check(cut2);
}
}

return 0;
}

Compilation message

tree.cpp:6:0: warning: ignoring #pragma warning  [-Wunknown-pragmas]
 #pragma warning(disable:4996)
 ^
tree.cpp: In function 'void un_check(int)':
tree.cpp:28:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 for (i = 0; i < edge[w].size(); i++) {
               ^
tree.cpp: In function 'void labeling(int)':
tree.cpp:38:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 for (i = 0; i < edge[w].size(); i++) {
               ^
tree.cpp: In function 'int main()':
tree.cpp:92:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 if (ep[0][cut1] == edge[cut1].size() - 1) edge[cut1].pop_back();
                 ^
tree.cpp:99:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 if (ep[1][cut1] == edge[cut2].size() - 1) edge[cut2].pop_back();
                 ^
tree.cpp:122:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 if (nowi == edge[noww].size()) break;
          ^
tree.cpp:129:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 if (nowi == edge[noww].size()) continue;
          ^
tree.cpp:136:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 for (j = 0; j < done.size(); j++) is_gone[done[j]] = 0;
               ^
tree.cpp:52:5: warning: unused variable 'P' [-Wunused-variable]
 int P;
     ^
tree.cpp:56:22: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
 scanf("%d%d", &n, &q);
                      ^
tree.cpp:64:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
 scanf("%d", &parent[i]);
                        ^
tree.cpp:72:31: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
 scanf("%d%d%d", &a, &b, &flag);
                               ^
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 17196 KB Output is correct
2 Correct 3 ms 17196 KB Output is correct
3 Correct 3 ms 17196 KB Output is correct
4 Correct 3 ms 17196 KB Output is correct
5 Correct 3 ms 17196 KB Output is correct
6 Correct 3 ms 17196 KB Output is correct
7 Correct 3 ms 17196 KB Output is correct
8 Correct 3 ms 17196 KB Output is correct
9 Correct 3 ms 17196 KB Output is correct
10 Correct 0 ms 17196 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 17196 KB Output is correct
2 Correct 3 ms 17196 KB Output is correct
3 Correct 3 ms 17196 KB Output is correct
4 Correct 3 ms 17196 KB Output is correct
5 Correct 3 ms 17196 KB Output is correct
6 Correct 3 ms 17196 KB Output is correct
7 Correct 3 ms 17196 KB Output is correct
8 Correct 3 ms 17196 KB Output is correct
9 Correct 3 ms 17196 KB Output is correct
10 Correct 0 ms 17196 KB Output is correct
11 Correct 0 ms 17196 KB Output is correct
12 Correct 3 ms 17196 KB Output is correct
13 Correct 6 ms 17196 KB Output is correct
14 Correct 0 ms 17196 KB Output is correct
15 Correct 3 ms 17196 KB Output is correct
16 Correct 3 ms 17196 KB Output is correct
17 Correct 6 ms 17196 KB Output is correct
18 Correct 0 ms 17196 KB Output is correct
19 Correct 0 ms 17196 KB Output is correct
20 Correct 0 ms 17196 KB Output is correct
21 Correct 3 ms 17196 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 17196 KB Output is correct
2 Correct 3 ms 17196 KB Output is correct
3 Correct 3 ms 17196 KB Output is correct
4 Correct 3 ms 17196 KB Output is correct
5 Correct 3 ms 17196 KB Output is correct
6 Correct 3 ms 17196 KB Output is correct
7 Correct 3 ms 17196 KB Output is correct
8 Correct 3 ms 17196 KB Output is correct
9 Correct 3 ms 17196 KB Output is correct
10 Correct 0 ms 17196 KB Output is correct
11 Correct 0 ms 17196 KB Output is correct
12 Correct 3 ms 17196 KB Output is correct
13 Correct 6 ms 17196 KB Output is correct
14 Correct 0 ms 17196 KB Output is correct
15 Correct 3 ms 17196 KB Output is correct
16 Correct 3 ms 17196 KB Output is correct
17 Correct 6 ms 17196 KB Output is correct
18 Correct 0 ms 17196 KB Output is correct
19 Correct 0 ms 17196 KB Output is correct
20 Correct 0 ms 17196 KB Output is correct
21 Correct 3 ms 17196 KB Output is correct
22 Correct 73 ms 17328 KB Output is correct
23 Correct 59 ms 17328 KB Output is correct
24 Correct 73 ms 17328 KB Output is correct
25 Correct 73 ms 17328 KB Output is correct
26 Correct 79 ms 17328 KB Output is correct
27 Correct 66 ms 17328 KB Output is correct
28 Correct 76 ms 17328 KB Output is correct
29 Correct 73 ms 17328 KB Output is correct
30 Correct 63 ms 17328 KB Output is correct
31 Correct 69 ms 17328 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 17196 KB Output is correct
2 Correct 3 ms 17196 KB Output is correct
3 Correct 3 ms 17196 KB Output is correct
4 Correct 3 ms 17196 KB Output is correct
5 Correct 3 ms 17196 KB Output is correct
6 Correct 3 ms 17196 KB Output is correct
7 Correct 3 ms 17196 KB Output is correct
8 Correct 3 ms 17196 KB Output is correct
9 Correct 3 ms 17196 KB Output is correct
10 Correct 0 ms 17196 KB Output is correct
11 Correct 699 ms 27552 KB Output is correct
12 Correct 683 ms 30948 KB Output is correct
13 Correct 849 ms 27636 KB Output is correct
14 Correct 806 ms 27740 KB Output is correct
15 Correct 796 ms 31200 KB Output is correct
16 Correct 836 ms 30324 KB Output is correct
17 Correct 303 ms 23400 KB Output is correct
18 Correct 316 ms 23584 KB Output is correct
19 Correct 303 ms 23400 KB Output is correct
20 Correct 323 ms 23400 KB Output is correct
21 Correct 673 ms 26896 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 17196 KB Output is correct
2 Correct 3 ms 17196 KB Output is correct
3 Correct 3 ms 17196 KB Output is correct
4 Correct 3 ms 17196 KB Output is correct
5 Correct 3 ms 17196 KB Output is correct
6 Correct 3 ms 17196 KB Output is correct
7 Correct 3 ms 17196 KB Output is correct
8 Correct 3 ms 17196 KB Output is correct
9 Correct 3 ms 17196 KB Output is correct
10 Correct 0 ms 17196 KB Output is correct
11 Correct 0 ms 17196 KB Output is correct
12 Correct 3 ms 17196 KB Output is correct
13 Correct 6 ms 17196 KB Output is correct
14 Correct 0 ms 17196 KB Output is correct
15 Correct 3 ms 17196 KB Output is correct
16 Correct 3 ms 17196 KB Output is correct
17 Correct 6 ms 17196 KB Output is correct
18 Correct 0 ms 17196 KB Output is correct
19 Correct 0 ms 17196 KB Output is correct
20 Correct 0 ms 17196 KB Output is correct
21 Correct 3 ms 17196 KB Output is correct
22 Correct 73 ms 17328 KB Output is correct
23 Correct 59 ms 17328 KB Output is correct
24 Correct 73 ms 17328 KB Output is correct
25 Correct 73 ms 17328 KB Output is correct
26 Correct 79 ms 17328 KB Output is correct
27 Correct 66 ms 17328 KB Output is correct
28 Correct 76 ms 17328 KB Output is correct
29 Correct 73 ms 17328 KB Output is correct
30 Correct 63 ms 17328 KB Output is correct
31 Correct 69 ms 17328 KB Output is correct
32 Correct 699 ms 27552 KB Output is correct
33 Correct 683 ms 30948 KB Output is correct
34 Correct 849 ms 27636 KB Output is correct
35 Correct 806 ms 27740 KB Output is correct
36 Correct 796 ms 31200 KB Output is correct
37 Correct 836 ms 30324 KB Output is correct
38 Correct 303 ms 23400 KB Output is correct
39 Correct 316 ms 23584 KB Output is correct
40 Correct 303 ms 23400 KB Output is correct
41 Correct 323 ms 23400 KB Output is correct
42 Correct 673 ms 26896 KB Output is correct
43 Correct 269 ms 26832 KB Output is correct
44 Correct 273 ms 26836 KB Output is correct
45 Correct 286 ms 27092 KB Output is correct
46 Correct 313 ms 27488 KB Output is correct
47 Correct 343 ms 26836 KB Output is correct
48 Correct 136 ms 26560 KB Output is correct
49 Correct 126 ms 27344 KB Output is correct
50 Correct 126 ms 27924 KB Output is correct
51 Correct 423 ms 27296 KB Output is correct