Submission #20826

# Submission time Handle Problem Language Result Execution time Memory
20826 2017-02-20T13:56:08 Z model_code None (KOI16_treeM) C++11
100 / 100
1169 ms 20280 KB
#include<stdio.h>
#include<vector>
#include<deque>

#define MAX 200100

using namespace std;

struct xy {
int x, y;
};

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

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

int cut1, cut2;
int parent[MAX];
int ep1[MAX];
int ep2[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 (edge[w][i].y == 1)
continue;
if (is_gone[edge[w][i].x])
continue;
labeling(edge[w][i].x);
}
}
vector<int>stw[2];
vector<int>sti[2];


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]);
ep1[i] = edge[i].size();
ep2[i] = edge[parent[i]].size();
edge[parent[i]].push_back({ i, 0 });
edge[i].push_back({ parent[i], 0 });
}

for (i = 0; i < n + q - 1; i++) {
scanf("%d", &flag);
if (flag == 1) {
scanf("%d%d", &a, &b);
if (label[a] == label[b]) {
printf("YES\n");
}
else {
printf("NO\n");
}
continue;
}
scanf("%d", &cut1);
cut2 = parent[cut1];
if (cut2 == -1)
continue;
edge[cut1][ep1[cut1]].y = 1;
edge[cut2][ep2[cut1]].y = 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();
stw[flag].pop_back();
sti[flag].pop_back();
is_gone[noww] = 1;
while (1) {
if (nowi == edge[noww].size())
break;
if (edge[noww][nowi].y) {
nowi++;
continue;
}
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;
}
stw[0].clear(); sti[0].clear();
stw[1].clear(); sti[1].clear();
un_check(cut1);
if (flag == 0) {
labeling(cut1);
labelw++;
un_check(cut1);
}
else {
labeling(cut2);
labelw++;
un_check(cut2);
}
}

return 0;
}

Compilation message

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:39: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:106:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 if (nowi == edge[noww].size())
          ^
tree.cpp:118:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 if (nowi == edge[noww].size()) {
          ^
tree.cpp:53:8: warning: unused variable 'j' [-Wunused-variable]
 int i, j;
        ^
tree.cpp:55:5: warning: unused variable 'P' [-Wunused-variable]
 int P;
     ^
tree.cpp:59: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:69:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
 scanf("%d", &parent[i]);
                        ^
tree.cpp:77:19: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
 scanf("%d", &flag);
                   ^
tree.cpp:79:22: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
 scanf("%d%d", &a, &b);
                      ^
tree.cpp:88:19: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
 scanf("%d", &cut1);
                   ^
# Verdict Execution time Memory Grader output
1 Correct 3 ms 9188 KB Output is correct
2 Correct 0 ms 9188 KB Output is correct
3 Correct 3 ms 9188 KB Output is correct
4 Correct 0 ms 9188 KB Output is correct
5 Correct 0 ms 9188 KB Output is correct
6 Correct 3 ms 9188 KB Output is correct
7 Correct 0 ms 9188 KB Output is correct
8 Correct 0 ms 9188 KB Output is correct
9 Correct 0 ms 9188 KB Output is correct
10 Correct 0 ms 9188 KB Output is correct
11 Correct 0 ms 9188 KB Output is correct
12 Correct 0 ms 9188 KB Output is correct
13 Correct 0 ms 9188 KB Output is correct
14 Correct 0 ms 9188 KB Output is correct
15 Correct 0 ms 9188 KB Output is correct
16 Correct 0 ms 9188 KB Output is correct
17 Correct 0 ms 9188 KB Output is correct
18 Correct 0 ms 9188 KB Output is correct
19 Correct 0 ms 9188 KB Output is correct
20 Correct 0 ms 9188 KB Output is correct
21 Correct 0 ms 9188 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 9188 KB Output is correct
2 Correct 0 ms 9188 KB Output is correct
3 Correct 3 ms 9188 KB Output is correct
4 Correct 0 ms 9188 KB Output is correct
5 Correct 0 ms 9188 KB Output is correct
6 Correct 3 ms 9188 KB Output is correct
7 Correct 0 ms 9188 KB Output is correct
8 Correct 0 ms 9188 KB Output is correct
9 Correct 0 ms 9188 KB Output is correct
10 Correct 0 ms 9188 KB Output is correct
11 Correct 0 ms 9188 KB Output is correct
12 Correct 0 ms 9188 KB Output is correct
13 Correct 0 ms 9188 KB Output is correct
14 Correct 0 ms 9188 KB Output is correct
15 Correct 0 ms 9188 KB Output is correct
16 Correct 0 ms 9188 KB Output is correct
17 Correct 0 ms 9188 KB Output is correct
18 Correct 0 ms 9188 KB Output is correct
19 Correct 0 ms 9188 KB Output is correct
20 Correct 0 ms 9188 KB Output is correct
21 Correct 0 ms 9188 KB Output is correct
22 Correct 83 ms 9320 KB Output is correct
23 Correct 76 ms 9320 KB Output is correct
24 Correct 96 ms 9320 KB Output is correct
25 Correct 73 ms 9320 KB Output is correct
26 Correct 83 ms 9320 KB Output is correct
27 Correct 76 ms 9320 KB Output is correct
28 Correct 83 ms 9320 KB Output is correct
29 Correct 76 ms 9320 KB Output is correct
30 Correct 83 ms 9320 KB Output is correct
31 Correct 99 ms 9320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1169 ms 20256 KB Output is correct
2 Correct 949 ms 19532 KB Output is correct
3 Correct 1043 ms 18204 KB Output is correct
4 Correct 963 ms 18064 KB Output is correct
5 Correct 1012 ms 18028 KB Output is correct
6 Correct 996 ms 18156 KB Output is correct
7 Correct 1113 ms 20280 KB Output is correct
8 Correct 333 ms 15392 KB Output is correct
9 Correct 326 ms 15392 KB Output is correct
10 Correct 356 ms 15392 KB Output is correct
11 Correct 1006 ms 20024 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 9188 KB Output is correct
2 Correct 0 ms 9188 KB Output is correct
3 Correct 3 ms 9188 KB Output is correct
4 Correct 0 ms 9188 KB Output is correct
5 Correct 0 ms 9188 KB Output is correct
6 Correct 3 ms 9188 KB Output is correct
7 Correct 0 ms 9188 KB Output is correct
8 Correct 0 ms 9188 KB Output is correct
9 Correct 0 ms 9188 KB Output is correct
10 Correct 0 ms 9188 KB Output is correct
11 Correct 0 ms 9188 KB Output is correct
12 Correct 0 ms 9188 KB Output is correct
13 Correct 0 ms 9188 KB Output is correct
14 Correct 0 ms 9188 KB Output is correct
15 Correct 0 ms 9188 KB Output is correct
16 Correct 0 ms 9188 KB Output is correct
17 Correct 0 ms 9188 KB Output is correct
18 Correct 0 ms 9188 KB Output is correct
19 Correct 0 ms 9188 KB Output is correct
20 Correct 0 ms 9188 KB Output is correct
21 Correct 0 ms 9188 KB Output is correct
22 Correct 83 ms 9320 KB Output is correct
23 Correct 76 ms 9320 KB Output is correct
24 Correct 96 ms 9320 KB Output is correct
25 Correct 73 ms 9320 KB Output is correct
26 Correct 83 ms 9320 KB Output is correct
27 Correct 76 ms 9320 KB Output is correct
28 Correct 83 ms 9320 KB Output is correct
29 Correct 76 ms 9320 KB Output is correct
30 Correct 83 ms 9320 KB Output is correct
31 Correct 99 ms 9320 KB Output is correct
32 Correct 1169 ms 20256 KB Output is correct
33 Correct 949 ms 19532 KB Output is correct
34 Correct 1043 ms 18204 KB Output is correct
35 Correct 963 ms 18064 KB Output is correct
36 Correct 1012 ms 18028 KB Output is correct
37 Correct 996 ms 18156 KB Output is correct
38 Correct 1113 ms 20280 KB Output is correct
39 Correct 333 ms 15392 KB Output is correct
40 Correct 326 ms 15392 KB Output is correct
41 Correct 356 ms 15392 KB Output is correct
42 Correct 1006 ms 20024 KB Output is correct
43 Correct 579 ms 17504 KB Output is correct
44 Correct 566 ms 17504 KB Output is correct
45 Correct 516 ms 17504 KB Output is correct
46 Correct 396 ms 17504 KB Output is correct
47 Correct 433 ms 17504 KB Output is correct
48 Correct 179 ms 17480 KB Output is correct
49 Correct 223 ms 17996 KB Output is correct
50 Correct 213 ms 18096 KB Output is correct
51 Correct 216 ms 17992 KB Output is correct