Submission #27172

# Submission time Handle Problem Language Result Execution time Memory
27172 2017-07-09T19:20:44 Z wwiiiii None (KOI16_tree) C++14
100 / 100
1186 ms 32540 KB
#include <cstdio>
#include <vector>
#include <queue>
#include <set>
using namespace std;
const int N = 200100;
set<int> adj[N];
int parent[N];
int treeidx[N];
int n, q, maxidx, visitmax;
int visited[N];

void reindex(int node) {
	treeidx[node] = maxidx;
	queue<int> q; q.push(node);
	while (!q.empty())
	{
		int now = q.front(); q.pop();
		for (auto next : adj[now]) {
			if (treeidx[next] != maxidx) {
				treeidx[next] = maxidx;
				q.push(next);
			}
		}
	}
}

void cut(int node)
{
	visitmax++;
	if (node == parent[node]) return;
	adj[node].erase(parent[node]);
	adj[parent[node]].erase(node);
	queue<int> q, pq;
	q.push(node); pq.push(parent[node]);
	int qnow = -1, pqnow = -1;
	set<int>::iterator qit, pqit;
	while ((!q.empty()) && (!pq.empty()))
	{
		while (qnow == -1 || qit == adj[qnow].end()) {
			if (q.empty()) break;
			qnow = q.front(); q.pop();
			qit = adj[qnow].begin();
			while (qit != adj[qnow].end() && visited[*qit] == visitmax) qit++;
		}
		//if (!q.empty()) {
		if(qnow != -1 && qit != adj[qnow].end()){
			visited[*qit] = visitmax;
			q.push(*qit); qit++;
		}
		while (pqnow == -1 || pqit == adj[pqnow].end()) {
			if (pq.empty()) break;
			pqnow = pq.front(); pq.pop();
			pqit = adj[pqnow].begin();
			while (pqit != adj[pqnow].end() && visited[*pqit] == visitmax) pqit++;
		}
		//if (!pq.empty()) {
		if (pqnow != -1 && pqit != adj[pqnow].end()) {
			visited[*pqit] = visitmax;
			pq.push(*pqit); pqit++;
		}
	}

	maxidx++;
	if (q.empty()) reindex(node);
	else reindex(parent[node]);

	parent[node] = node;

}

int main()
{
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	scanf("%d %d", &n, &q);
	for (int i = 2; i <= n; i++) {
		scanf("%d", &parent[i]);
		adj[parent[i]].insert(i);
		adj[i].insert(parent[i]);
	}
	while (q--) {
		int b, c, d; scanf("%d %d %d", &b, &c, &d);
		switch (d)
		{
		case 0:
			puts(treeidx[b] == treeidx[c] ? "YES" : "NO"); break;
		case 1:
			if (treeidx[b] == treeidx[c])
			{
				puts("YES"); cut(b);
			}
			else
			{
				puts("NO"); cut(c);
			}
		}
	}
}

Compilation message

tree.cpp: In function 'int main()':
tree.cpp:76:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d", &n, &q);
                        ^
tree.cpp:78:26: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &parent[i]);
                          ^
tree.cpp:83:45: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   int b, c, d; scanf("%d %d %d", &b, &c, &d);
                                             ^
# Verdict Execution time Memory Grader output
1 Correct 0 ms 13796 KB Output is correct
2 Correct 0 ms 13796 KB Output is correct
3 Correct 3 ms 13796 KB Output is correct
4 Correct 0 ms 13796 KB Output is correct
5 Correct 0 ms 13796 KB Output is correct
6 Correct 6 ms 13796 KB Output is correct
7 Correct 0 ms 13796 KB Output is correct
8 Correct 3 ms 13796 KB Output is correct
9 Correct 6 ms 13796 KB Output is correct
10 Correct 3 ms 13796 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 13796 KB Output is correct
2 Correct 0 ms 13796 KB Output is correct
3 Correct 3 ms 13796 KB Output is correct
4 Correct 0 ms 13796 KB Output is correct
5 Correct 0 ms 13796 KB Output is correct
6 Correct 6 ms 13796 KB Output is correct
7 Correct 0 ms 13796 KB Output is correct
8 Correct 3 ms 13796 KB Output is correct
9 Correct 6 ms 13796 KB Output is correct
10 Correct 3 ms 13796 KB Output is correct
11 Correct 6 ms 13796 KB Output is correct
12 Correct 0 ms 13796 KB Output is correct
13 Correct 3 ms 13796 KB Output is correct
14 Correct 3 ms 13796 KB Output is correct
15 Correct 3 ms 13796 KB Output is correct
16 Correct 0 ms 13796 KB Output is correct
17 Correct 0 ms 13796 KB Output is correct
18 Correct 0 ms 13796 KB Output is correct
19 Correct 3 ms 13796 KB Output is correct
20 Correct 3 ms 13796 KB Output is correct
21 Correct 3 ms 13796 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 13796 KB Output is correct
2 Correct 0 ms 13796 KB Output is correct
3 Correct 3 ms 13796 KB Output is correct
4 Correct 0 ms 13796 KB Output is correct
5 Correct 0 ms 13796 KB Output is correct
6 Correct 6 ms 13796 KB Output is correct
7 Correct 0 ms 13796 KB Output is correct
8 Correct 3 ms 13796 KB Output is correct
9 Correct 6 ms 13796 KB Output is correct
10 Correct 3 ms 13796 KB Output is correct
11 Correct 6 ms 13796 KB Output is correct
12 Correct 0 ms 13796 KB Output is correct
13 Correct 3 ms 13796 KB Output is correct
14 Correct 3 ms 13796 KB Output is correct
15 Correct 3 ms 13796 KB Output is correct
16 Correct 0 ms 13796 KB Output is correct
17 Correct 0 ms 13796 KB Output is correct
18 Correct 0 ms 13796 KB Output is correct
19 Correct 3 ms 13796 KB Output is correct
20 Correct 3 ms 13796 KB Output is correct
21 Correct 3 ms 13796 KB Output is correct
22 Correct 86 ms 13928 KB Output is correct
23 Correct 73 ms 13928 KB Output is correct
24 Correct 86 ms 13928 KB Output is correct
25 Correct 66 ms 13928 KB Output is correct
26 Correct 69 ms 13928 KB Output is correct
27 Correct 76 ms 13928 KB Output is correct
28 Correct 79 ms 13928 KB Output is correct
29 Correct 79 ms 13928 KB Output is correct
30 Correct 63 ms 13928 KB Output is correct
31 Correct 59 ms 13928 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 13796 KB Output is correct
2 Correct 0 ms 13796 KB Output is correct
3 Correct 3 ms 13796 KB Output is correct
4 Correct 0 ms 13796 KB Output is correct
5 Correct 0 ms 13796 KB Output is correct
6 Correct 6 ms 13796 KB Output is correct
7 Correct 0 ms 13796 KB Output is correct
8 Correct 3 ms 13796 KB Output is correct
9 Correct 6 ms 13796 KB Output is correct
10 Correct 3 ms 13796 KB Output is correct
11 Correct 1169 ms 32408 KB Output is correct
12 Correct 1123 ms 32408 KB Output is correct
13 Correct 1106 ms 32408 KB Output is correct
14 Correct 1186 ms 32408 KB Output is correct
15 Correct 1153 ms 32408 KB Output is correct
16 Correct 1063 ms 32408 KB Output is correct
17 Correct 506 ms 32408 KB Output is correct
18 Correct 419 ms 32408 KB Output is correct
19 Correct 509 ms 32408 KB Output is correct
20 Correct 516 ms 32408 KB Output is correct
21 Correct 1022 ms 32408 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 13796 KB Output is correct
2 Correct 0 ms 13796 KB Output is correct
3 Correct 3 ms 13796 KB Output is correct
4 Correct 0 ms 13796 KB Output is correct
5 Correct 0 ms 13796 KB Output is correct
6 Correct 6 ms 13796 KB Output is correct
7 Correct 0 ms 13796 KB Output is correct
8 Correct 3 ms 13796 KB Output is correct
9 Correct 6 ms 13796 KB Output is correct
10 Correct 3 ms 13796 KB Output is correct
11 Correct 6 ms 13796 KB Output is correct
12 Correct 0 ms 13796 KB Output is correct
13 Correct 3 ms 13796 KB Output is correct
14 Correct 3 ms 13796 KB Output is correct
15 Correct 3 ms 13796 KB Output is correct
16 Correct 0 ms 13796 KB Output is correct
17 Correct 0 ms 13796 KB Output is correct
18 Correct 0 ms 13796 KB Output is correct
19 Correct 3 ms 13796 KB Output is correct
20 Correct 3 ms 13796 KB Output is correct
21 Correct 3 ms 13796 KB Output is correct
22 Correct 86 ms 13928 KB Output is correct
23 Correct 73 ms 13928 KB Output is correct
24 Correct 86 ms 13928 KB Output is correct
25 Correct 66 ms 13928 KB Output is correct
26 Correct 69 ms 13928 KB Output is correct
27 Correct 76 ms 13928 KB Output is correct
28 Correct 79 ms 13928 KB Output is correct
29 Correct 79 ms 13928 KB Output is correct
30 Correct 63 ms 13928 KB Output is correct
31 Correct 59 ms 13928 KB Output is correct
32 Correct 1169 ms 32408 KB Output is correct
33 Correct 1123 ms 32408 KB Output is correct
34 Correct 1106 ms 32408 KB Output is correct
35 Correct 1186 ms 32408 KB Output is correct
36 Correct 1153 ms 32408 KB Output is correct
37 Correct 1063 ms 32408 KB Output is correct
38 Correct 506 ms 32408 KB Output is correct
39 Correct 419 ms 32408 KB Output is correct
40 Correct 509 ms 32408 KB Output is correct
41 Correct 516 ms 32408 KB Output is correct
42 Correct 1022 ms 32408 KB Output is correct
43 Correct 426 ms 32408 KB Output is correct
44 Correct 396 ms 32408 KB Output is correct
45 Correct 406 ms 32540 KB Output is correct
46 Correct 559 ms 32540 KB Output is correct
47 Correct 433 ms 32408 KB Output is correct
48 Correct 266 ms 32408 KB Output is correct
49 Correct 253 ms 32408 KB Output is correct
50 Correct 273 ms 32540 KB Output is correct
51 Correct 619 ms 32408 KB Output is correct