답안 #333172

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
333172 2020-12-04T21:21:41 Z ly20 Mergers (JOI19_mergers) C++17
70 / 100
2666 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];
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[0][u]++, in[0][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[0][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[0][u]--;
			if(need[find(v)] == 0)	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.top(); 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: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]]++;
      |   ~~~~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 35692 KB Output is correct
2 Correct 24 ms 35564 KB Output is correct
3 Correct 24 ms 35564 KB Output is correct
4 Correct 27 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 24 ms 35564 KB Output is correct
8 Correct 24 ms 35692 KB Output is correct
9 Correct 24 ms 35564 KB Output is correct
10 Correct 24 ms 35660 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
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 35692 KB Output is correct
2 Correct 24 ms 35564 KB Output is correct
3 Correct 24 ms 35564 KB Output is correct
4 Correct 27 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 24 ms 35564 KB Output is correct
8 Correct 24 ms 35692 KB Output is correct
9 Correct 24 ms 35564 KB Output is correct
10 Correct 24 ms 35660 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 35 ms 36716 KB Output is correct
27 Correct 27 ms 36204 KB Output is correct
28 Correct 29 ms 36608 KB Output is correct
29 Correct 31 ms 36972 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 30 ms 36332 KB Output is correct
35 Correct 31 ms 36972 KB Output is correct
36 Correct 28 ms 36332 KB Output is correct
37 Correct 29 ms 36588 KB Output is correct
38 Correct 24 ms 35684 KB Output is correct
39 Correct 30 ms 36716 KB Output is correct
40 Correct 27 ms 36332 KB Output is correct
41 Correct 28 ms 36332 KB Output is correct
42 Correct 28 ms 36332 KB Output is correct
43 Correct 27 ms 36352 KB Output is correct
44 Correct 24 ms 35692 KB Output is correct
45 Correct 27 ms 36332 KB Output is correct
46 Correct 29 ms 36716 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 30 ms 36588 KB Output is correct
51 Correct 28 ms 36352 KB Output is correct
52 Correct 27 ms 36460 KB Output is correct
53 Correct 27 ms 36332 KB Output is correct
54 Correct 29 ms 36588 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 35692 KB Output is correct
2 Correct 24 ms 35564 KB Output is correct
3 Correct 24 ms 35564 KB Output is correct
4 Correct 27 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 24 ms 35564 KB Output is correct
8 Correct 24 ms 35692 KB Output is correct
9 Correct 24 ms 35564 KB Output is correct
10 Correct 24 ms 35660 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 190 ms 59624 KB Output is correct
27 Correct 340 ms 67768 KB Output is correct
28 Correct 27 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 281 ms 59376 KB Output is correct
32 Correct 27 ms 36332 KB Output is correct
33 Correct 288 ms 58860 KB Output is correct
34 Correct 344 ms 66028 KB Output is correct
35 Correct 28 ms 36332 KB Output is correct
36 Correct 227 ms 56044 KB Output is correct
37 Correct 27 ms 36332 KB Output is correct
38 Correct 28 ms 36332 KB Output is correct
39 Correct 181 ms 59488 KB Output is correct
40 Correct 28 ms 36332 KB Output is correct
41 Correct 283 ms 59372 KB Output is correct
42 Correct 291 ms 58732 KB Output is correct
43 Correct 24 ms 35564 KB Output is correct
44 Correct 246 ms 58604 KB Output is correct
45 Correct 293 ms 61552 KB Output is correct
46 Correct 27 ms 36332 KB Output is correct
47 Correct 29 ms 36588 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 186 ms 59236 KB Output is correct
2 Correct 280 ms 64232 KB Output is correct
3 Correct 30 ms 36716 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 357 ms 71656 KB Output is correct
9 Correct 27 ms 36332 KB Output is correct
10 Correct 273 ms 58472 KB Output is correct
11 Correct 24 ms 35564 KB Output is correct
12 Correct 231 ms 56332 KB Output is correct
13 Correct 365 ms 69864 KB Output is correct
14 Correct 511 ms 89624 KB Output is correct
15 Correct 168 ms 59504 KB Output is correct
16 Correct 27 ms 36332 KB Output is correct
17 Correct 24 ms 35692 KB Output is correct
18 Correct 287 ms 65040 KB Output is correct
19 Correct 369 ms 63852 KB Output is correct
20 Correct 28 ms 36588 KB Output is correct
21 Correct 24 ms 35564 KB Output is correct
22 Correct 175 ms 56936 KB Output is correct
23 Correct 28 ms 36332 KB Output is correct
24 Correct 255 ms 58348 KB Output is correct
25 Correct 462 ms 70128 KB Output is correct
26 Correct 28 ms 36460 KB Output is correct
27 Correct 31 ms 36588 KB Output is correct
28 Correct 27 ms 36332 KB Output is correct
29 Correct 27 ms 36332 KB Output is correct
30 Correct 25 ms 36332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 35692 KB Output is correct
2 Correct 24 ms 35564 KB Output is correct
3 Correct 24 ms 35564 KB Output is correct
4 Correct 27 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 24 ms 35564 KB Output is correct
8 Correct 24 ms 35692 KB Output is correct
9 Correct 24 ms 35564 KB Output is correct
10 Correct 24 ms 35660 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 35 ms 36716 KB Output is correct
27 Correct 27 ms 36204 KB Output is correct
28 Correct 29 ms 36608 KB Output is correct
29 Correct 31 ms 36972 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 30 ms 36332 KB Output is correct
35 Correct 31 ms 36972 KB Output is correct
36 Correct 28 ms 36332 KB Output is correct
37 Correct 29 ms 36588 KB Output is correct
38 Correct 24 ms 35684 KB Output is correct
39 Correct 30 ms 36716 KB Output is correct
40 Correct 27 ms 36332 KB Output is correct
41 Correct 28 ms 36332 KB Output is correct
42 Correct 28 ms 36332 KB Output is correct
43 Correct 27 ms 36352 KB Output is correct
44 Correct 24 ms 35692 KB Output is correct
45 Correct 27 ms 36332 KB Output is correct
46 Correct 29 ms 36716 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 30 ms 36588 KB Output is correct
51 Correct 28 ms 36352 KB Output is correct
52 Correct 27 ms 36460 KB Output is correct
53 Correct 27 ms 36332 KB Output is correct
54 Correct 29 ms 36588 KB Output is correct
55 Correct 25 ms 35564 KB Output is correct
56 Correct 190 ms 59624 KB Output is correct
57 Correct 340 ms 67768 KB Output is correct
58 Correct 27 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 281 ms 59376 KB Output is correct
62 Correct 27 ms 36332 KB Output is correct
63 Correct 288 ms 58860 KB Output is correct
64 Correct 344 ms 66028 KB Output is correct
65 Correct 28 ms 36332 KB Output is correct
66 Correct 227 ms 56044 KB Output is correct
67 Correct 27 ms 36332 KB Output is correct
68 Correct 28 ms 36332 KB Output is correct
69 Correct 181 ms 59488 KB Output is correct
70 Correct 28 ms 36332 KB Output is correct
71 Correct 283 ms 59372 KB Output is correct
72 Correct 291 ms 58732 KB Output is correct
73 Correct 24 ms 35564 KB Output is correct
74 Correct 246 ms 58604 KB Output is correct
75 Correct 293 ms 61552 KB Output is correct
76 Correct 27 ms 36332 KB Output is correct
77 Correct 29 ms 36588 KB Output is correct
78 Correct 186 ms 59236 KB Output is correct
79 Correct 280 ms 64232 KB Output is correct
80 Correct 30 ms 36716 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 357 ms 71656 KB Output is correct
86 Correct 27 ms 36332 KB Output is correct
87 Correct 273 ms 58472 KB Output is correct
88 Correct 24 ms 35564 KB Output is correct
89 Correct 231 ms 56332 KB Output is correct
90 Correct 365 ms 69864 KB Output is correct
91 Correct 511 ms 89624 KB Output is correct
92 Correct 168 ms 59504 KB Output is correct
93 Correct 27 ms 36332 KB Output is correct
94 Correct 24 ms 35692 KB Output is correct
95 Correct 287 ms 65040 KB Output is correct
96 Correct 369 ms 63852 KB Output is correct
97 Correct 28 ms 36588 KB Output is correct
98 Correct 24 ms 35564 KB Output is correct
99 Correct 175 ms 56936 KB Output is correct
100 Correct 28 ms 36332 KB Output is correct
101 Correct 255 ms 58348 KB Output is correct
102 Correct 462 ms 70128 KB Output is correct
103 Correct 28 ms 36460 KB Output is correct
104 Correct 31 ms 36588 KB Output is correct
105 Correct 27 ms 36332 KB Output is correct
106 Correct 27 ms 36332 KB Output is correct
107 Correct 25 ms 36332 KB Output is correct
108 Correct 1068 ms 135004 KB Output is correct
109 Correct 1903 ms 149412 KB Output is correct
110 Correct 1772 ms 149472 KB Output is correct
111 Correct 2494 ms 170148 KB Output is correct
112 Correct 2550 ms 183032 KB Output is correct
113 Correct 1714 ms 181388 KB Output is correct
114 Correct 1910 ms 153060 KB Output is correct
115 Correct 1935 ms 153064 KB Output is correct
116 Correct 2666 ms 241468 KB Output is correct
117 Runtime error 2255 ms 262148 KB Execution killed with signal 9 (could be triggered by violating memory limits)
118 Halted 0 ms 0 KB -