Submission #333176

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

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

int par[MAXN], depth[MAXN], need[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;
	need[b] += need[a];

	for(it = s[a].begin(); it != s[a].end(); it++)
		if(s[b].find(*it) != s[b].end()) need[b] -= qtt[*it];
		else 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[u]++, in[v]++;
	}

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

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

	while(!q.empty())
	{
		int v = q.top(); 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[u]--;
			if(need[find(v)] == 0)	Special(u, v);
			join(v, u);
			if(in[u] <= 1)	q.push(u);
		}
	}

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

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

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

			if(specialEdge[make_pair(u, v)]){	ans++; in[u] = 0; continue;	}
			if(in[u] == 1)	in[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:52:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   52 |  scanf("%d %d", &n, &k);
      |  ~~~~~^~~~~~~~~~~~~~~~~
mergers.cpp:56:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   56 |   scanf("%d %d", &u, &v);
      |   ~~~~~^~~~~~~~~~~~~~~~~
mergers.cpp:65:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   65 |   scanf("%d", &g[i]); qtt[g[i]]++;
      |   ~~~~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 24 ms 35564 KB Output is correct
2 Correct 24 ms 35564 KB Output is correct
3 Correct 24 ms 35564 KB Output is correct
4 Correct 24 ms 35564 KB Output is correct
5 Correct 24 ms 35564 KB Output is correct
6 Correct 25 ms 35564 KB Output is correct
7 Correct 24 ms 35564 KB Output is correct
8 Correct 25 ms 35564 KB Output is correct
9 Correct 24 ms 35564 KB Output is correct
10 Correct 24 ms 35564 KB Output is correct
11 Correct 24 ms 35564 KB Output is correct
12 Correct 24 ms 35564 KB Output is correct
13 Correct 25 ms 35692 KB Output is correct
14 Correct 24 ms 35564 KB Output is correct
15 Correct 24 ms 35564 KB Output is correct
16 Correct 24 ms 35564 KB Output is correct
17 Correct 24 ms 35564 KB Output is correct
18 Correct 24 ms 35564 KB Output is correct
19 Correct 24 ms 35564 KB Output is correct
20 Correct 24 ms 35564 KB Output is correct
21 Correct 24 ms 35564 KB Output is correct
22 Correct 24 ms 35564 KB Output is correct
23 Correct 24 ms 35564 KB Output is correct
24 Correct 24 ms 35564 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 35564 KB Output is correct
2 Correct 24 ms 35564 KB Output is correct
3 Correct 24 ms 35564 KB Output is correct
4 Correct 24 ms 35564 KB Output is correct
5 Correct 24 ms 35564 KB Output is correct
6 Correct 25 ms 35564 KB Output is correct
7 Correct 24 ms 35564 KB Output is correct
8 Correct 25 ms 35564 KB Output is correct
9 Correct 24 ms 35564 KB Output is correct
10 Correct 24 ms 35564 KB Output is correct
11 Correct 24 ms 35564 KB Output is correct
12 Correct 24 ms 35564 KB Output is correct
13 Correct 25 ms 35692 KB Output is correct
14 Correct 24 ms 35564 KB Output is correct
15 Correct 24 ms 35564 KB Output is correct
16 Correct 24 ms 35564 KB Output is correct
17 Correct 24 ms 35564 KB Output is correct
18 Correct 24 ms 35564 KB Output is correct
19 Correct 24 ms 35564 KB Output is correct
20 Correct 24 ms 35564 KB Output is correct
21 Correct 24 ms 35564 KB Output is correct
22 Correct 24 ms 35564 KB Output is correct
23 Correct 24 ms 35564 KB Output is correct
24 Correct 24 ms 35564 KB Output is correct
25 Correct 24 ms 35564 KB Output is correct
26 Correct 30 ms 36716 KB Output is correct
27 Correct 27 ms 36204 KB Output is correct
28 Correct 30 ms 36588 KB Output is correct
29 Correct 31 ms 37100 KB Output is correct
30 Correct 29 ms 36588 KB Output is correct
31 Correct 24 ms 35564 KB Output is correct
32 Correct 27 ms 36204 KB Output is correct
33 Correct 24 ms 35564 KB Output is correct
34 Correct 27 ms 36332 KB Output is correct
35 Correct 31 ms 36992 KB Output is correct
36 Correct 27 ms 36332 KB Output is correct
37 Correct 29 ms 36588 KB Output is correct
38 Correct 24 ms 35564 KB Output is correct
39 Correct 33 ms 36716 KB Output is correct
40 Correct 28 ms 36332 KB Output is correct
41 Correct 28 ms 36332 KB Output is correct
42 Correct 27 ms 36332 KB Output is correct
43 Correct 27 ms 36332 KB Output is correct
44 Correct 24 ms 35692 KB Output is correct
45 Correct 27 ms 36332 KB Output is correct
46 Correct 28 ms 36460 KB Output is correct
47 Correct 24 ms 35564 KB Output is correct
48 Correct 28 ms 36332 KB Output is correct
49 Correct 28 ms 36460 KB Output is correct
50 Correct 29 ms 36588 KB Output is correct
51 Correct 28 ms 36332 KB Output is correct
52 Correct 28 ms 36332 KB Output is correct
53 Correct 29 ms 36204 KB Output is correct
54 Correct 29 ms 36588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 35564 KB Output is correct
2 Correct 24 ms 35564 KB Output is correct
3 Correct 24 ms 35564 KB Output is correct
4 Correct 24 ms 35564 KB Output is correct
5 Correct 24 ms 35564 KB Output is correct
6 Correct 25 ms 35564 KB Output is correct
7 Correct 24 ms 35564 KB Output is correct
8 Correct 25 ms 35564 KB Output is correct
9 Correct 24 ms 35564 KB Output is correct
10 Correct 24 ms 35564 KB Output is correct
11 Correct 24 ms 35564 KB Output is correct
12 Correct 24 ms 35564 KB Output is correct
13 Correct 25 ms 35692 KB Output is correct
14 Correct 24 ms 35564 KB Output is correct
15 Correct 24 ms 35564 KB Output is correct
16 Correct 24 ms 35564 KB Output is correct
17 Correct 24 ms 35564 KB Output is correct
18 Correct 24 ms 35564 KB Output is correct
19 Correct 24 ms 35564 KB Output is correct
20 Correct 24 ms 35564 KB Output is correct
21 Correct 24 ms 35564 KB Output is correct
22 Correct 24 ms 35564 KB Output is correct
23 Correct 24 ms 35564 KB Output is correct
24 Correct 24 ms 35564 KB Output is correct
25 Correct 26 ms 35708 KB Output is correct
26 Correct 187 ms 59112 KB Output is correct
27 Correct 347 ms 67460 KB Output is correct
28 Correct 28 ms 36332 KB Output is correct
29 Correct 24 ms 35564 KB Output is correct
30 Correct 24 ms 35564 KB Output is correct
31 Correct 276 ms 58864 KB Output is correct
32 Correct 27 ms 36332 KB Output is correct
33 Correct 280 ms 58348 KB Output is correct
34 Correct 325 ms 65772 KB Output is correct
35 Correct 27 ms 36332 KB Output is correct
36 Correct 220 ms 55660 KB Output is correct
37 Correct 27 ms 36332 KB Output is correct
38 Correct 27 ms 36332 KB Output is correct
39 Correct 174 ms 59232 KB Output is correct
40 Correct 27 ms 36332 KB Output is correct
41 Correct 273 ms 58988 KB Output is correct
42 Correct 286 ms 58348 KB Output is correct
43 Correct 24 ms 35564 KB Output is correct
44 Correct 270 ms 58220 KB Output is correct
45 Correct 296 ms 61168 KB Output is correct
46 Correct 27 ms 36460 KB Output is correct
47 Correct 29 ms 36588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 190 ms 58856 KB Output is correct
2 Correct 277 ms 63848 KB Output is correct
3 Correct 29 ms 36588 KB Output is correct
4 Correct 27 ms 36204 KB Output is correct
5 Correct 24 ms 35564 KB Output is correct
6 Correct 24 ms 35564 KB Output is correct
7 Correct 27 ms 36332 KB Output is correct
8 Correct 355 ms 71272 KB Output is correct
9 Correct 28 ms 36268 KB Output is correct
10 Correct 256 ms 58056 KB Output is correct
11 Correct 24 ms 35564 KB Output is correct
12 Correct 224 ms 55916 KB Output is correct
13 Correct 342 ms 69352 KB Output is correct
14 Correct 506 ms 89196 KB Output is correct
15 Correct 170 ms 59104 KB Output is correct
16 Correct 27 ms 36332 KB Output is correct
17 Correct 24 ms 35692 KB Output is correct
18 Correct 280 ms 64736 KB Output is correct
19 Correct 388 ms 63340 KB Output is correct
20 Correct 28 ms 36460 KB Output is correct
21 Correct 24 ms 35564 KB Output is correct
22 Correct 172 ms 56424 KB Output is correct
23 Correct 28 ms 36332 KB Output is correct
24 Correct 258 ms 57964 KB Output is correct
25 Correct 445 ms 69616 KB Output is correct
26 Correct 28 ms 36460 KB Output is correct
27 Correct 30 ms 36588 KB Output is correct
28 Correct 28 ms 36332 KB Output is correct
29 Correct 27 ms 36332 KB Output is correct
30 Correct 27 ms 36204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 35564 KB Output is correct
2 Correct 24 ms 35564 KB Output is correct
3 Correct 24 ms 35564 KB Output is correct
4 Correct 24 ms 35564 KB Output is correct
5 Correct 24 ms 35564 KB Output is correct
6 Correct 25 ms 35564 KB Output is correct
7 Correct 24 ms 35564 KB Output is correct
8 Correct 25 ms 35564 KB Output is correct
9 Correct 24 ms 35564 KB Output is correct
10 Correct 24 ms 35564 KB Output is correct
11 Correct 24 ms 35564 KB Output is correct
12 Correct 24 ms 35564 KB Output is correct
13 Correct 25 ms 35692 KB Output is correct
14 Correct 24 ms 35564 KB Output is correct
15 Correct 24 ms 35564 KB Output is correct
16 Correct 24 ms 35564 KB Output is correct
17 Correct 24 ms 35564 KB Output is correct
18 Correct 24 ms 35564 KB Output is correct
19 Correct 24 ms 35564 KB Output is correct
20 Correct 24 ms 35564 KB Output is correct
21 Correct 24 ms 35564 KB Output is correct
22 Correct 24 ms 35564 KB Output is correct
23 Correct 24 ms 35564 KB Output is correct
24 Correct 24 ms 35564 KB Output is correct
25 Correct 24 ms 35564 KB Output is correct
26 Correct 30 ms 36716 KB Output is correct
27 Correct 27 ms 36204 KB Output is correct
28 Correct 30 ms 36588 KB Output is correct
29 Correct 31 ms 37100 KB Output is correct
30 Correct 29 ms 36588 KB Output is correct
31 Correct 24 ms 35564 KB Output is correct
32 Correct 27 ms 36204 KB Output is correct
33 Correct 24 ms 35564 KB Output is correct
34 Correct 27 ms 36332 KB Output is correct
35 Correct 31 ms 36992 KB Output is correct
36 Correct 27 ms 36332 KB Output is correct
37 Correct 29 ms 36588 KB Output is correct
38 Correct 24 ms 35564 KB Output is correct
39 Correct 33 ms 36716 KB Output is correct
40 Correct 28 ms 36332 KB Output is correct
41 Correct 28 ms 36332 KB Output is correct
42 Correct 27 ms 36332 KB Output is correct
43 Correct 27 ms 36332 KB Output is correct
44 Correct 24 ms 35692 KB Output is correct
45 Correct 27 ms 36332 KB Output is correct
46 Correct 28 ms 36460 KB Output is correct
47 Correct 24 ms 35564 KB Output is correct
48 Correct 28 ms 36332 KB Output is correct
49 Correct 28 ms 36460 KB Output is correct
50 Correct 29 ms 36588 KB Output is correct
51 Correct 28 ms 36332 KB Output is correct
52 Correct 28 ms 36332 KB Output is correct
53 Correct 29 ms 36204 KB Output is correct
54 Correct 29 ms 36588 KB Output is correct
55 Correct 26 ms 35708 KB Output is correct
56 Correct 187 ms 59112 KB Output is correct
57 Correct 347 ms 67460 KB Output is correct
58 Correct 28 ms 36332 KB Output is correct
59 Correct 24 ms 35564 KB Output is correct
60 Correct 24 ms 35564 KB Output is correct
61 Correct 276 ms 58864 KB Output is correct
62 Correct 27 ms 36332 KB Output is correct
63 Correct 280 ms 58348 KB Output is correct
64 Correct 325 ms 65772 KB Output is correct
65 Correct 27 ms 36332 KB Output is correct
66 Correct 220 ms 55660 KB Output is correct
67 Correct 27 ms 36332 KB Output is correct
68 Correct 27 ms 36332 KB Output is correct
69 Correct 174 ms 59232 KB Output is correct
70 Correct 27 ms 36332 KB Output is correct
71 Correct 273 ms 58988 KB Output is correct
72 Correct 286 ms 58348 KB Output is correct
73 Correct 24 ms 35564 KB Output is correct
74 Correct 270 ms 58220 KB Output is correct
75 Correct 296 ms 61168 KB Output is correct
76 Correct 27 ms 36460 KB Output is correct
77 Correct 29 ms 36588 KB Output is correct
78 Correct 190 ms 58856 KB Output is correct
79 Correct 277 ms 63848 KB Output is correct
80 Correct 29 ms 36588 KB Output is correct
81 Correct 27 ms 36204 KB Output is correct
82 Correct 24 ms 35564 KB Output is correct
83 Correct 24 ms 35564 KB Output is correct
84 Correct 27 ms 36332 KB Output is correct
85 Correct 355 ms 71272 KB Output is correct
86 Correct 28 ms 36268 KB Output is correct
87 Correct 256 ms 58056 KB Output is correct
88 Correct 24 ms 35564 KB Output is correct
89 Correct 224 ms 55916 KB Output is correct
90 Correct 342 ms 69352 KB Output is correct
91 Correct 506 ms 89196 KB Output is correct
92 Correct 170 ms 59104 KB Output is correct
93 Correct 27 ms 36332 KB Output is correct
94 Correct 24 ms 35692 KB Output is correct
95 Correct 280 ms 64736 KB Output is correct
96 Correct 388 ms 63340 KB Output is correct
97 Correct 28 ms 36460 KB Output is correct
98 Correct 24 ms 35564 KB Output is correct
99 Correct 172 ms 56424 KB Output is correct
100 Correct 28 ms 36332 KB Output is correct
101 Correct 258 ms 57964 KB Output is correct
102 Correct 445 ms 69616 KB Output is correct
103 Correct 28 ms 36460 KB Output is correct
104 Correct 30 ms 36588 KB Output is correct
105 Correct 28 ms 36332 KB Output is correct
106 Correct 27 ms 36332 KB Output is correct
107 Correct 27 ms 36204 KB Output is correct
108 Correct 1062 ms 133288 KB Output is correct
109 Correct 1797 ms 147436 KB Output is correct
110 Correct 1674 ms 147564 KB Output is correct
111 Correct 2469 ms 168048 KB Output is correct
112 Correct 2459 ms 181340 KB Output is correct
113 Correct 1729 ms 179596 KB Output is correct
114 Correct 1967 ms 150884 KB Output is correct
115 Correct 1979 ms 150868 KB Output is correct
116 Correct 2667 ms 239592 KB Output is correct
117 Runtime error 2296 ms 262148 KB Execution killed with signal 9 (could be triggered by violating memory limits)
118 Halted 0 ms 0 KB -