Submission #333180

# Submission time Handle Problem Language Result Execution time Memory
333180 2020-12-04T21:27:35 Z Kenzo_1114 Mergers (JOI19_mergers) C++17
70 / 100
3000 ms 173088 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);

	s[a].clear();
 
	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:54:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   54 |  scanf("%d %d", &n, &k);
      |  ~~~~~^~~~~~~~~~~~~~~~~
mergers.cpp:58:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   58 |   scanf("%d %d", &u, &v);
      |   ~~~~~^~~~~~~~~~~~~~~~~
mergers.cpp:67:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   67 |   scanf("%d", &g[i]); qtt[g[i]]++;
      |   ~~~~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 24 ms 35564 KB Output is correct
2 Correct 27 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 24 ms 35564 KB Output is correct
7 Correct 25 ms 35692 KB Output is correct
8 Correct 24 ms 35564 KB Output is correct
9 Correct 24 ms 35564 KB Output is correct
10 Correct 23 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 24 ms 35564 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 35692 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 27 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 24 ms 35564 KB Output is correct
7 Correct 25 ms 35692 KB Output is correct
8 Correct 24 ms 35564 KB Output is correct
9 Correct 24 ms 35564 KB Output is correct
10 Correct 23 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 24 ms 35564 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 35692 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 25 ms 35564 KB Output is correct
26 Correct 29 ms 36204 KB Output is correct
27 Correct 27 ms 36076 KB Output is correct
28 Correct 30 ms 36332 KB Output is correct
29 Correct 30 ms 36332 KB Output is correct
30 Correct 29 ms 36204 KB Output is correct
31 Correct 24 ms 35564 KB Output is correct
32 Correct 27 ms 35948 KB Output is correct
33 Correct 24 ms 35564 KB Output is correct
34 Correct 27 ms 36204 KB Output is correct
35 Correct 31 ms 36332 KB Output is correct
36 Correct 28 ms 36076 KB Output is correct
37 Correct 29 ms 36204 KB Output is correct
38 Correct 24 ms 35564 KB Output is correct
39 Correct 31 ms 36204 KB Output is correct
40 Correct 27 ms 36204 KB Output is correct
41 Correct 27 ms 36332 KB Output is correct
42 Correct 27 ms 36076 KB Output is correct
43 Correct 28 ms 36204 KB Output is correct
44 Correct 24 ms 35564 KB Output is correct
45 Correct 28 ms 35948 KB Output is correct
46 Correct 29 ms 36076 KB Output is correct
47 Correct 24 ms 35564 KB Output is correct
48 Correct 27 ms 36076 KB Output is correct
49 Correct 28 ms 36332 KB Output is correct
50 Correct 29 ms 36460 KB Output is correct
51 Correct 29 ms 36076 KB Output is correct
52 Correct 27 ms 36204 KB Output is correct
53 Correct 27 ms 36076 KB Output is correct
54 Correct 28 ms 36204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 35564 KB Output is correct
2 Correct 27 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 24 ms 35564 KB Output is correct
7 Correct 25 ms 35692 KB Output is correct
8 Correct 24 ms 35564 KB Output is correct
9 Correct 24 ms 35564 KB Output is correct
10 Correct 23 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 24 ms 35564 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 35692 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 176 ms 54272 KB Output is correct
27 Correct 348 ms 54144 KB Output is correct
28 Correct 34 ms 36076 KB Output is correct
29 Correct 24 ms 35564 KB Output is correct
30 Correct 24 ms 35564 KB Output is correct
31 Correct 289 ms 54000 KB Output is correct
32 Correct 28 ms 36204 KB Output is correct
33 Correct 295 ms 53484 KB Output is correct
34 Correct 332 ms 53928 KB Output is correct
35 Correct 27 ms 36076 KB Output is correct
36 Correct 242 ms 50668 KB Output is correct
37 Correct 28 ms 36204 KB Output is correct
38 Correct 28 ms 36204 KB Output is correct
39 Correct 178 ms 54240 KB Output is correct
40 Correct 28 ms 36204 KB Output is correct
41 Correct 294 ms 53996 KB Output is correct
42 Correct 290 ms 53332 KB Output is correct
43 Correct 25 ms 35564 KB Output is correct
44 Correct 282 ms 53308 KB Output is correct
45 Correct 305 ms 53872 KB Output is correct
46 Correct 27 ms 36204 KB Output is correct
47 Correct 30 ms 36204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 180 ms 53992 KB Output is correct
2 Correct 244 ms 59112 KB Output is correct
3 Correct 29 ms 36204 KB Output is correct
4 Correct 27 ms 36076 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 341 ms 53096 KB Output is correct
9 Correct 28 ms 36076 KB Output is correct
10 Correct 282 ms 53096 KB Output is correct
11 Correct 28 ms 35564 KB Output is correct
12 Correct 260 ms 51180 KB Output is correct
13 Correct 354 ms 52800 KB Output is correct
14 Correct 514 ms 60780 KB Output is correct
15 Correct 188 ms 54112 KB Output is correct
16 Correct 28 ms 36076 KB Output is correct
17 Correct 24 ms 35564 KB Output is correct
18 Correct 251 ms 58608 KB Output is correct
19 Correct 387 ms 58348 KB Output is correct
20 Correct 28 ms 36076 KB Output is correct
21 Correct 24 ms 35564 KB Output is correct
22 Correct 172 ms 52456 KB Output is correct
23 Correct 29 ms 36204 KB Output is correct
24 Correct 285 ms 51820 KB Output is correct
25 Correct 432 ms 62444 KB Output is correct
26 Correct 29 ms 36332 KB Output is correct
27 Correct 29 ms 36460 KB Output is correct
28 Correct 27 ms 36076 KB Output is correct
29 Correct 28 ms 36204 KB Output is correct
30 Correct 27 ms 36076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 35564 KB Output is correct
2 Correct 27 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 24 ms 35564 KB Output is correct
7 Correct 25 ms 35692 KB Output is correct
8 Correct 24 ms 35564 KB Output is correct
9 Correct 24 ms 35564 KB Output is correct
10 Correct 23 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 24 ms 35564 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 35692 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 25 ms 35564 KB Output is correct
26 Correct 29 ms 36204 KB Output is correct
27 Correct 27 ms 36076 KB Output is correct
28 Correct 30 ms 36332 KB Output is correct
29 Correct 30 ms 36332 KB Output is correct
30 Correct 29 ms 36204 KB Output is correct
31 Correct 24 ms 35564 KB Output is correct
32 Correct 27 ms 35948 KB Output is correct
33 Correct 24 ms 35564 KB Output is correct
34 Correct 27 ms 36204 KB Output is correct
35 Correct 31 ms 36332 KB Output is correct
36 Correct 28 ms 36076 KB Output is correct
37 Correct 29 ms 36204 KB Output is correct
38 Correct 24 ms 35564 KB Output is correct
39 Correct 31 ms 36204 KB Output is correct
40 Correct 27 ms 36204 KB Output is correct
41 Correct 27 ms 36332 KB Output is correct
42 Correct 27 ms 36076 KB Output is correct
43 Correct 28 ms 36204 KB Output is correct
44 Correct 24 ms 35564 KB Output is correct
45 Correct 28 ms 35948 KB Output is correct
46 Correct 29 ms 36076 KB Output is correct
47 Correct 24 ms 35564 KB Output is correct
48 Correct 27 ms 36076 KB Output is correct
49 Correct 28 ms 36332 KB Output is correct
50 Correct 29 ms 36460 KB Output is correct
51 Correct 29 ms 36076 KB Output is correct
52 Correct 27 ms 36204 KB Output is correct
53 Correct 27 ms 36076 KB Output is correct
54 Correct 28 ms 36204 KB Output is correct
55 Correct 24 ms 35564 KB Output is correct
56 Correct 176 ms 54272 KB Output is correct
57 Correct 348 ms 54144 KB Output is correct
58 Correct 34 ms 36076 KB Output is correct
59 Correct 24 ms 35564 KB Output is correct
60 Correct 24 ms 35564 KB Output is correct
61 Correct 289 ms 54000 KB Output is correct
62 Correct 28 ms 36204 KB Output is correct
63 Correct 295 ms 53484 KB Output is correct
64 Correct 332 ms 53928 KB Output is correct
65 Correct 27 ms 36076 KB Output is correct
66 Correct 242 ms 50668 KB Output is correct
67 Correct 28 ms 36204 KB Output is correct
68 Correct 28 ms 36204 KB Output is correct
69 Correct 178 ms 54240 KB Output is correct
70 Correct 28 ms 36204 KB Output is correct
71 Correct 294 ms 53996 KB Output is correct
72 Correct 290 ms 53332 KB Output is correct
73 Correct 25 ms 35564 KB Output is correct
74 Correct 282 ms 53308 KB Output is correct
75 Correct 305 ms 53872 KB Output is correct
76 Correct 27 ms 36204 KB Output is correct
77 Correct 30 ms 36204 KB Output is correct
78 Correct 180 ms 53992 KB Output is correct
79 Correct 244 ms 59112 KB Output is correct
80 Correct 29 ms 36204 KB Output is correct
81 Correct 27 ms 36076 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 341 ms 53096 KB Output is correct
86 Correct 28 ms 36076 KB Output is correct
87 Correct 282 ms 53096 KB Output is correct
88 Correct 28 ms 35564 KB Output is correct
89 Correct 260 ms 51180 KB Output is correct
90 Correct 354 ms 52800 KB Output is correct
91 Correct 514 ms 60780 KB Output is correct
92 Correct 188 ms 54112 KB Output is correct
93 Correct 28 ms 36076 KB Output is correct
94 Correct 24 ms 35564 KB Output is correct
95 Correct 251 ms 58608 KB Output is correct
96 Correct 387 ms 58348 KB Output is correct
97 Correct 28 ms 36076 KB Output is correct
98 Correct 24 ms 35564 KB Output is correct
99 Correct 172 ms 52456 KB Output is correct
100 Correct 29 ms 36204 KB Output is correct
101 Correct 285 ms 51820 KB Output is correct
102 Correct 432 ms 62444 KB Output is correct
103 Correct 29 ms 36332 KB Output is correct
104 Correct 29 ms 36460 KB Output is correct
105 Correct 27 ms 36076 KB Output is correct
106 Correct 28 ms 36204 KB Output is correct
107 Correct 27 ms 36076 KB Output is correct
108 Correct 1089 ms 112264 KB Output is correct
109 Correct 1894 ms 123776 KB Output is correct
110 Correct 1866 ms 124012 KB Output is correct
111 Correct 2600 ms 144572 KB Output is correct
112 Correct 2494 ms 145572 KB Output is correct
113 Correct 1666 ms 149900 KB Output is correct
114 Correct 2045 ms 127360 KB Output is correct
115 Correct 1989 ms 127264 KB Output is correct
116 Correct 2816 ms 132328 KB Output is correct
117 Execution timed out 3080 ms 173088 KB Time limit exceeded
118 Halted 0 ms 0 KB -