Submission #333164

# Submission time Handle Problem Language Result Execution time Memory
333164 2020-12-04T21:12:40 Z ly20 Mergers (JOI19_mergers) C++17
70 / 100
2870 ms 262148 KB
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 500010;

int n, k, qtt[MAXN], g[MAXN], in[2][MAXN];

int par[MAXN], depth[MAXN], need[MAXN], have[MAXN];
set<int> s[MAXN];
set<int> :: iterator it;
int marc[MAXN];
vector<int> graph[MAXN];
map<pair<int, int>, bool> specialEdge;

void Special(int u, int v)
{
	specialEdge[make_pair(u, v)] = true;
	specialEdge[make_pair(v, u)] = true;
}

void dfs(int v, int p)
{
	for(int i = 0; i < (int) graph[v].size(); i++)
	{
		int u = graph[v][i];
		if(u != p)	dfs(u, v);
	}
}

int find(int v)
{
	return (v == par[v]) ? v : par[v] = find(par[v]);
}

void join(int a, int b)
{
	a = find(a), b = find(b);
	if(a == b)	return;
	if(depth[a] > depth[b])	swap(a, b);

	par[a] = b;
	have[b] += have[a];

	for(it = s[a].begin(); it != s[a].end(); it++)
		if(s[b].find(*it) == s[b].end()) need[b] += qtt[*it], s[b].insert(*it);

	if(depth[a] == depth[b])	depth[b]++;
}

int main ()
{
	scanf("%d %d", &n, &k);

	for(int i = 0, u, v; i < n - 1; i++)
	{
		scanf("%d %d", &u, &v);
		graph[u].push_back(v);
		graph[v].push_back(u);
		in[0][u]++, in[0][v]++;
	}

	queue<int> q;
	for(int i = 1; i <= n; i++)
	{
		scanf("%d", &g[i]);	qtt[g[i]]++;
		in[1][i] = in[0][i];
		if(in[0][i] == 1)	q.push(i);
	}

	for(int i = 1; i <= n; i++)
	{
		par[i] = i;
		need[i] = qtt[g[i]];
		have[i] = 1;
		s[i].insert(g[i]);
	}

	while(!q.empty())
	{
		int v = q.front(); q.pop();
        marc[v] = 1;
		for(int i = 0; i < (int) graph[v].size(); i++)
		{
			int u = graph[v][i];
            if(marc[u] == 1) continue;
            in[0][u]--;
			if(have[find(v)] == need[find(v)])	Special(u, v);
			join(v, u);
			if(in[0][u] <= 1)	q.push(u);
		}
	}

	for(int i = 1; i <= n; i++)
		if(in[1][i] == 1)	q.push(i);

	int ans = 0;
	while(!q.empty())
	{
		int v = q.front(); q.pop();

		for(int i = 0; i < (int) graph[v].size(); i++)
		{
			int u = graph[v][i];
			in[1][u]--;

			if(specialEdge[make_pair(u, v)]){	ans++; in[1][u] = 0; continue;	}
			if(in[1][u] == 1)	in[1][u] = 0, q.push(u);
		}
	}

	printf("%d\n", (ans + 1) >> 1);
}

/*

5 4
1 2
2 3
3 4
3 5
1
2
1
3
4

*/

Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:51:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   51 |  scanf("%d %d", &n, &k);
      |  ~~~~~^~~~~~~~~~~~~~~~~
mergers.cpp:55:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   55 |   scanf("%d %d", &u, &v);
      |   ~~~~~^~~~~~~~~~~~~~~~~
mergers.cpp:64:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   64 |   scanf("%d", &g[i]); qtt[g[i]]++;
      |   ~~~~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 23 ms 35564 KB Output is correct
2 Correct 23 ms 35692 KB Output is correct
3 Correct 23 ms 35692 KB Output is correct
4 Correct 23 ms 35564 KB Output is correct
5 Correct 23 ms 35564 KB Output is correct
6 Correct 23 ms 35692 KB Output is correct
7 Correct 22 ms 35564 KB Output is correct
8 Correct 23 ms 35692 KB Output is correct
9 Correct 22 ms 35564 KB Output is correct
10 Correct 23 ms 35564 KB Output is correct
11 Correct 22 ms 35692 KB Output is correct
12 Correct 23 ms 35692 KB Output is correct
13 Correct 23 ms 35564 KB Output is correct
14 Correct 23 ms 35692 KB Output is correct
15 Correct 23 ms 35692 KB Output is correct
16 Correct 23 ms 35564 KB Output is correct
17 Correct 23 ms 35564 KB Output is correct
18 Correct 23 ms 35692 KB Output is correct
19 Correct 23 ms 35692 KB Output is correct
20 Correct 24 ms 35564 KB Output is correct
21 Correct 23 ms 35692 KB Output is correct
22 Correct 23 ms 35572 KB Output is correct
23 Correct 23 ms 35584 KB Output is correct
24 Correct 23 ms 35564 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 35564 KB Output is correct
2 Correct 23 ms 35692 KB Output is correct
3 Correct 23 ms 35692 KB Output is correct
4 Correct 23 ms 35564 KB Output is correct
5 Correct 23 ms 35564 KB Output is correct
6 Correct 23 ms 35692 KB Output is correct
7 Correct 22 ms 35564 KB Output is correct
8 Correct 23 ms 35692 KB Output is correct
9 Correct 22 ms 35564 KB Output is correct
10 Correct 23 ms 35564 KB Output is correct
11 Correct 22 ms 35692 KB Output is correct
12 Correct 23 ms 35692 KB Output is correct
13 Correct 23 ms 35564 KB Output is correct
14 Correct 23 ms 35692 KB Output is correct
15 Correct 23 ms 35692 KB Output is correct
16 Correct 23 ms 35564 KB Output is correct
17 Correct 23 ms 35564 KB Output is correct
18 Correct 23 ms 35692 KB Output is correct
19 Correct 23 ms 35692 KB Output is correct
20 Correct 24 ms 35564 KB Output is correct
21 Correct 23 ms 35692 KB Output is correct
22 Correct 23 ms 35572 KB Output is correct
23 Correct 23 ms 35584 KB Output is correct
24 Correct 23 ms 35564 KB Output is correct
25 Correct 23 ms 35564 KB Output is correct
26 Correct 30 ms 36844 KB Output is correct
27 Correct 26 ms 36204 KB Output is correct
28 Correct 28 ms 36460 KB Output is correct
29 Correct 31 ms 37100 KB Output is correct
30 Correct 28 ms 36716 KB Output is correct
31 Correct 23 ms 35564 KB Output is correct
32 Correct 27 ms 36204 KB Output is correct
33 Correct 23 ms 35692 KB Output is correct
34 Correct 26 ms 36332 KB Output is correct
35 Correct 31 ms 37100 KB Output is correct
36 Correct 27 ms 36460 KB Output is correct
37 Correct 28 ms 36736 KB Output is correct
38 Correct 23 ms 35692 KB Output is correct
39 Correct 29 ms 36844 KB Output is correct
40 Correct 26 ms 36332 KB Output is correct
41 Correct 26 ms 36332 KB Output is correct
42 Correct 30 ms 36460 KB Output is correct
43 Correct 26 ms 36332 KB Output is correct
44 Correct 23 ms 35692 KB Output is correct
45 Correct 28 ms 36460 KB Output is correct
46 Correct 28 ms 36588 KB Output is correct
47 Correct 23 ms 35692 KB Output is correct
48 Correct 26 ms 36460 KB Output is correct
49 Correct 28 ms 36512 KB Output is correct
50 Correct 29 ms 36716 KB Output is correct
51 Correct 27 ms 36332 KB Output is correct
52 Correct 25 ms 36332 KB Output is correct
53 Correct 25 ms 36332 KB Output is correct
54 Correct 27 ms 36588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 35564 KB Output is correct
2 Correct 23 ms 35692 KB Output is correct
3 Correct 23 ms 35692 KB Output is correct
4 Correct 23 ms 35564 KB Output is correct
5 Correct 23 ms 35564 KB Output is correct
6 Correct 23 ms 35692 KB Output is correct
7 Correct 22 ms 35564 KB Output is correct
8 Correct 23 ms 35692 KB Output is correct
9 Correct 22 ms 35564 KB Output is correct
10 Correct 23 ms 35564 KB Output is correct
11 Correct 22 ms 35692 KB Output is correct
12 Correct 23 ms 35692 KB Output is correct
13 Correct 23 ms 35564 KB Output is correct
14 Correct 23 ms 35692 KB Output is correct
15 Correct 23 ms 35692 KB Output is correct
16 Correct 23 ms 35564 KB Output is correct
17 Correct 23 ms 35564 KB Output is correct
18 Correct 23 ms 35692 KB Output is correct
19 Correct 23 ms 35692 KB Output is correct
20 Correct 24 ms 35564 KB Output is correct
21 Correct 23 ms 35692 KB Output is correct
22 Correct 23 ms 35572 KB Output is correct
23 Correct 23 ms 35584 KB Output is correct
24 Correct 23 ms 35564 KB Output is correct
25 Correct 22 ms 35564 KB Output is correct
26 Correct 165 ms 60592 KB Output is correct
27 Correct 337 ms 69996 KB Output is correct
28 Correct 26 ms 36332 KB Output is correct
29 Correct 27 ms 35564 KB Output is correct
30 Correct 23 ms 35712 KB Output is correct
31 Correct 262 ms 60524 KB Output is correct
32 Correct 26 ms 36332 KB Output is correct
33 Correct 307 ms 60140 KB Output is correct
34 Correct 334 ms 68216 KB Output is correct
35 Correct 26 ms 36332 KB Output is correct
36 Correct 241 ms 57580 KB Output is correct
37 Correct 27 ms 36332 KB Output is correct
38 Correct 27 ms 36332 KB Output is correct
39 Correct 175 ms 60516 KB Output is correct
40 Correct 27 ms 36332 KB Output is correct
41 Correct 264 ms 60524 KB Output is correct
42 Correct 270 ms 60140 KB Output is correct
43 Correct 23 ms 35692 KB Output is correct
44 Correct 263 ms 60012 KB Output is correct
45 Correct 307 ms 62956 KB Output is correct
46 Correct 27 ms 36332 KB Output is correct
47 Correct 29 ms 36588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 172 ms 59236 KB Output is correct
2 Correct 256 ms 64612 KB Output is correct
3 Correct 28 ms 36716 KB Output is correct
4 Correct 26 ms 36204 KB Output is correct
5 Correct 23 ms 35564 KB Output is correct
6 Correct 24 ms 35692 KB Output is correct
7 Correct 26 ms 36332 KB Output is correct
8 Correct 356 ms 74732 KB Output is correct
9 Correct 27 ms 36332 KB Output is correct
10 Correct 262 ms 59884 KB Output is correct
11 Correct 23 ms 35692 KB Output is correct
12 Correct 240 ms 57964 KB Output is correct
13 Correct 360 ms 72180 KB Output is correct
14 Correct 519 ms 93292 KB Output is correct
15 Correct 175 ms 60664 KB Output is correct
16 Correct 27 ms 36332 KB Output is correct
17 Correct 23 ms 35692 KB Output is correct
18 Correct 265 ms 66404 KB Output is correct
19 Correct 393 ms 67820 KB Output is correct
20 Correct 28 ms 36588 KB Output is correct
21 Correct 23 ms 35692 KB Output is correct
22 Correct 173 ms 58212 KB Output is correct
23 Correct 27 ms 36460 KB Output is correct
24 Correct 281 ms 60012 KB Output is correct
25 Correct 424 ms 70764 KB Output is correct
26 Correct 27 ms 36460 KB Output is correct
27 Correct 28 ms 36716 KB Output is correct
28 Correct 26 ms 36332 KB Output is correct
29 Correct 26 ms 36332 KB Output is correct
30 Correct 26 ms 36332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 35564 KB Output is correct
2 Correct 23 ms 35692 KB Output is correct
3 Correct 23 ms 35692 KB Output is correct
4 Correct 23 ms 35564 KB Output is correct
5 Correct 23 ms 35564 KB Output is correct
6 Correct 23 ms 35692 KB Output is correct
7 Correct 22 ms 35564 KB Output is correct
8 Correct 23 ms 35692 KB Output is correct
9 Correct 22 ms 35564 KB Output is correct
10 Correct 23 ms 35564 KB Output is correct
11 Correct 22 ms 35692 KB Output is correct
12 Correct 23 ms 35692 KB Output is correct
13 Correct 23 ms 35564 KB Output is correct
14 Correct 23 ms 35692 KB Output is correct
15 Correct 23 ms 35692 KB Output is correct
16 Correct 23 ms 35564 KB Output is correct
17 Correct 23 ms 35564 KB Output is correct
18 Correct 23 ms 35692 KB Output is correct
19 Correct 23 ms 35692 KB Output is correct
20 Correct 24 ms 35564 KB Output is correct
21 Correct 23 ms 35692 KB Output is correct
22 Correct 23 ms 35572 KB Output is correct
23 Correct 23 ms 35584 KB Output is correct
24 Correct 23 ms 35564 KB Output is correct
25 Correct 23 ms 35564 KB Output is correct
26 Correct 30 ms 36844 KB Output is correct
27 Correct 26 ms 36204 KB Output is correct
28 Correct 28 ms 36460 KB Output is correct
29 Correct 31 ms 37100 KB Output is correct
30 Correct 28 ms 36716 KB Output is correct
31 Correct 23 ms 35564 KB Output is correct
32 Correct 27 ms 36204 KB Output is correct
33 Correct 23 ms 35692 KB Output is correct
34 Correct 26 ms 36332 KB Output is correct
35 Correct 31 ms 37100 KB Output is correct
36 Correct 27 ms 36460 KB Output is correct
37 Correct 28 ms 36736 KB Output is correct
38 Correct 23 ms 35692 KB Output is correct
39 Correct 29 ms 36844 KB Output is correct
40 Correct 26 ms 36332 KB Output is correct
41 Correct 26 ms 36332 KB Output is correct
42 Correct 30 ms 36460 KB Output is correct
43 Correct 26 ms 36332 KB Output is correct
44 Correct 23 ms 35692 KB Output is correct
45 Correct 28 ms 36460 KB Output is correct
46 Correct 28 ms 36588 KB Output is correct
47 Correct 23 ms 35692 KB Output is correct
48 Correct 26 ms 36460 KB Output is correct
49 Correct 28 ms 36512 KB Output is correct
50 Correct 29 ms 36716 KB Output is correct
51 Correct 27 ms 36332 KB Output is correct
52 Correct 25 ms 36332 KB Output is correct
53 Correct 25 ms 36332 KB Output is correct
54 Correct 27 ms 36588 KB Output is correct
55 Correct 22 ms 35564 KB Output is correct
56 Correct 165 ms 60592 KB Output is correct
57 Correct 337 ms 69996 KB Output is correct
58 Correct 26 ms 36332 KB Output is correct
59 Correct 27 ms 35564 KB Output is correct
60 Correct 23 ms 35712 KB Output is correct
61 Correct 262 ms 60524 KB Output is correct
62 Correct 26 ms 36332 KB Output is correct
63 Correct 307 ms 60140 KB Output is correct
64 Correct 334 ms 68216 KB Output is correct
65 Correct 26 ms 36332 KB Output is correct
66 Correct 241 ms 57580 KB Output is correct
67 Correct 27 ms 36332 KB Output is correct
68 Correct 27 ms 36332 KB Output is correct
69 Correct 175 ms 60516 KB Output is correct
70 Correct 27 ms 36332 KB Output is correct
71 Correct 264 ms 60524 KB Output is correct
72 Correct 270 ms 60140 KB Output is correct
73 Correct 23 ms 35692 KB Output is correct
74 Correct 263 ms 60012 KB Output is correct
75 Correct 307 ms 62956 KB Output is correct
76 Correct 27 ms 36332 KB Output is correct
77 Correct 29 ms 36588 KB Output is correct
78 Correct 172 ms 59236 KB Output is correct
79 Correct 256 ms 64612 KB Output is correct
80 Correct 28 ms 36716 KB Output is correct
81 Correct 26 ms 36204 KB Output is correct
82 Correct 23 ms 35564 KB Output is correct
83 Correct 24 ms 35692 KB Output is correct
84 Correct 26 ms 36332 KB Output is correct
85 Correct 356 ms 74732 KB Output is correct
86 Correct 27 ms 36332 KB Output is correct
87 Correct 262 ms 59884 KB Output is correct
88 Correct 23 ms 35692 KB Output is correct
89 Correct 240 ms 57964 KB Output is correct
90 Correct 360 ms 72180 KB Output is correct
91 Correct 519 ms 93292 KB Output is correct
92 Correct 175 ms 60664 KB Output is correct
93 Correct 27 ms 36332 KB Output is correct
94 Correct 23 ms 35692 KB Output is correct
95 Correct 265 ms 66404 KB Output is correct
96 Correct 393 ms 67820 KB Output is correct
97 Correct 28 ms 36588 KB Output is correct
98 Correct 23 ms 35692 KB Output is correct
99 Correct 173 ms 58212 KB Output is correct
100 Correct 27 ms 36460 KB Output is correct
101 Correct 281 ms 60012 KB Output is correct
102 Correct 424 ms 70764 KB Output is correct
103 Correct 27 ms 36460 KB Output is correct
104 Correct 28 ms 36716 KB Output is correct
105 Correct 26 ms 36332 KB Output is correct
106 Correct 26 ms 36332 KB Output is correct
107 Correct 26 ms 36332 KB Output is correct
108 Correct 1014 ms 144736 KB Output is correct
109 Correct 1694 ms 158700 KB Output is correct
110 Correct 1725 ms 158868 KB Output is correct
111 Correct 2548 ms 193444 KB Output is correct
112 Correct 2309 ms 183840 KB Output is correct
113 Correct 1696 ms 191116 KB Output is correct
114 Correct 1974 ms 161344 KB Output is correct
115 Correct 2219 ms 161228 KB Output is correct
116 Correct 2870 ms 260040 KB Output is correct
117 Runtime error 2624 ms 262148 KB Execution killed with signal 9 (could be triggered by violating memory limits)
118 Halted 0 ms 0 KB -