답안 #333168

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
333168 2020-12-04T21:16:46 Z ly20 Mergers (JOI19_mergers) C++17
70 / 100
2655 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]++;
	}

	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]];
		have[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(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.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: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]]++;
      |   ~~~~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 35580 KB Output is correct
2 Correct 24 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 24 ms 35564 KB Output is correct
6 Correct 24 ms 35692 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 35692 KB Output is correct
10 Correct 24 ms 35564 KB Output is correct
11 Correct 24 ms 35692 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 35692 KB Output is correct
15 Correct 24 ms 35692 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 35692 KB Output is correct
22 Correct 24 ms 35692 KB Output is correct
23 Correct 25 ms 35564 KB Output is correct
24 Correct 24 ms 35564 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 35580 KB Output is correct
2 Correct 24 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 24 ms 35564 KB Output is correct
6 Correct 24 ms 35692 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 35692 KB Output is correct
10 Correct 24 ms 35564 KB Output is correct
11 Correct 24 ms 35692 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 35692 KB Output is correct
15 Correct 24 ms 35692 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 35692 KB Output is correct
22 Correct 24 ms 35692 KB Output is correct
23 Correct 25 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 29 ms 36716 KB Output is correct
27 Correct 28 ms 36204 KB Output is correct
28 Correct 32 ms 36588 KB Output is correct
29 Correct 31 ms 36972 KB Output is correct
30 Correct 29 ms 36724 KB Output is correct
31 Correct 24 ms 35712 KB Output is correct
32 Correct 29 ms 36204 KB Output is correct
33 Correct 24 ms 35692 KB Output is correct
34 Correct 28 ms 36332 KB Output is correct
35 Correct 31 ms 36972 KB Output is correct
36 Correct 27 ms 36332 KB Output is correct
37 Correct 30 ms 36716 KB Output is correct
38 Correct 24 ms 35692 KB Output is correct
39 Correct 30 ms 36716 KB Output is correct
40 Correct 28 ms 36332 KB Output is correct
41 Correct 27 ms 36332 KB Output is correct
42 Correct 28 ms 36332 KB Output is correct
43 Correct 29 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 29 ms 36588 KB Output is correct
47 Correct 24 ms 35692 KB Output is correct
48 Correct 28 ms 36460 KB Output is correct
49 Correct 28 ms 36588 KB Output is correct
50 Correct 31 ms 36716 KB Output is correct
51 Correct 27 ms 36332 KB Output is correct
52 Correct 28 ms 36476 KB Output is correct
53 Correct 27 ms 36332 KB Output is correct
54 Correct 36 ms 36616 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 35580 KB Output is correct
2 Correct 24 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 24 ms 35564 KB Output is correct
6 Correct 24 ms 35692 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 35692 KB Output is correct
10 Correct 24 ms 35564 KB Output is correct
11 Correct 24 ms 35692 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 35692 KB Output is correct
15 Correct 24 ms 35692 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 35692 KB Output is correct
22 Correct 24 ms 35692 KB Output is correct
23 Correct 25 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 183 ms 60648 KB Output is correct
27 Correct 350 ms 69100 KB Output is correct
28 Correct 27 ms 36204 KB Output is correct
29 Correct 24 ms 35564 KB Output is correct
30 Correct 24 ms 35692 KB Output is correct
31 Correct 269 ms 60400 KB Output is correct
32 Correct 27 ms 36332 KB Output is correct
33 Correct 305 ms 59884 KB Output is correct
34 Correct 329 ms 67296 KB Output is correct
35 Correct 28 ms 36332 KB Output is correct
36 Correct 228 ms 57196 KB Output is correct
37 Correct 28 ms 36332 KB Output is correct
38 Correct 28 ms 36332 KB Output is correct
39 Correct 176 ms 60640 KB Output is correct
40 Correct 28 ms 36332 KB Output is correct
41 Correct 274 ms 60524 KB Output is correct
42 Correct 299 ms 59884 KB Output is correct
43 Correct 24 ms 35692 KB Output is correct
44 Correct 265 ms 59884 KB Output is correct
45 Correct 303 ms 62704 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 185 ms 59496 KB Output is correct
2 Correct 284 ms 64616 KB Output is correct
3 Correct 30 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 35820 KB Output is correct
7 Correct 27 ms 36332 KB Output is correct
8 Correct 364 ms 72936 KB Output is correct
9 Correct 27 ms 36332 KB Output is correct
10 Correct 269 ms 59624 KB Output is correct
11 Correct 24 ms 35692 KB Output is correct
12 Correct 236 ms 57580 KB Output is correct
13 Correct 347 ms 71016 KB Output is correct
14 Correct 513 ms 90896 KB Output is correct
15 Correct 172 ms 60640 KB Output is correct
16 Correct 27 ms 36332 KB Output is correct
17 Correct 24 ms 35692 KB Output is correct
18 Correct 282 ms 66272 KB Output is correct
19 Correct 380 ms 65260 KB Output is correct
20 Correct 29 ms 36588 KB Output is correct
21 Correct 24 ms 35692 KB Output is correct
22 Correct 171 ms 58216 KB Output is correct
23 Correct 28 ms 36460 KB Output is correct
24 Correct 241 ms 59500 KB Output is correct
25 Correct 440 ms 71152 KB Output is correct
26 Correct 28 ms 36588 KB Output is correct
27 Correct 29 ms 36716 KB Output is correct
28 Correct 27 ms 36332 KB Output is correct
29 Correct 27 ms 36332 KB Output is correct
30 Correct 27 ms 36332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 35580 KB Output is correct
2 Correct 24 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 24 ms 35564 KB Output is correct
6 Correct 24 ms 35692 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 35692 KB Output is correct
10 Correct 24 ms 35564 KB Output is correct
11 Correct 24 ms 35692 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 35692 KB Output is correct
15 Correct 24 ms 35692 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 35692 KB Output is correct
22 Correct 24 ms 35692 KB Output is correct
23 Correct 25 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 29 ms 36716 KB Output is correct
27 Correct 28 ms 36204 KB Output is correct
28 Correct 32 ms 36588 KB Output is correct
29 Correct 31 ms 36972 KB Output is correct
30 Correct 29 ms 36724 KB Output is correct
31 Correct 24 ms 35712 KB Output is correct
32 Correct 29 ms 36204 KB Output is correct
33 Correct 24 ms 35692 KB Output is correct
34 Correct 28 ms 36332 KB Output is correct
35 Correct 31 ms 36972 KB Output is correct
36 Correct 27 ms 36332 KB Output is correct
37 Correct 30 ms 36716 KB Output is correct
38 Correct 24 ms 35692 KB Output is correct
39 Correct 30 ms 36716 KB Output is correct
40 Correct 28 ms 36332 KB Output is correct
41 Correct 27 ms 36332 KB Output is correct
42 Correct 28 ms 36332 KB Output is correct
43 Correct 29 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 29 ms 36588 KB Output is correct
47 Correct 24 ms 35692 KB Output is correct
48 Correct 28 ms 36460 KB Output is correct
49 Correct 28 ms 36588 KB Output is correct
50 Correct 31 ms 36716 KB Output is correct
51 Correct 27 ms 36332 KB Output is correct
52 Correct 28 ms 36476 KB Output is correct
53 Correct 27 ms 36332 KB Output is correct
54 Correct 36 ms 36616 KB Output is correct
55 Correct 24 ms 35564 KB Output is correct
56 Correct 183 ms 60648 KB Output is correct
57 Correct 350 ms 69100 KB Output is correct
58 Correct 27 ms 36204 KB Output is correct
59 Correct 24 ms 35564 KB Output is correct
60 Correct 24 ms 35692 KB Output is correct
61 Correct 269 ms 60400 KB Output is correct
62 Correct 27 ms 36332 KB Output is correct
63 Correct 305 ms 59884 KB Output is correct
64 Correct 329 ms 67296 KB Output is correct
65 Correct 28 ms 36332 KB Output is correct
66 Correct 228 ms 57196 KB Output is correct
67 Correct 28 ms 36332 KB Output is correct
68 Correct 28 ms 36332 KB Output is correct
69 Correct 176 ms 60640 KB Output is correct
70 Correct 28 ms 36332 KB Output is correct
71 Correct 274 ms 60524 KB Output is correct
72 Correct 299 ms 59884 KB Output is correct
73 Correct 24 ms 35692 KB Output is correct
74 Correct 265 ms 59884 KB Output is correct
75 Correct 303 ms 62704 KB Output is correct
76 Correct 27 ms 36332 KB Output is correct
77 Correct 29 ms 36588 KB Output is correct
78 Correct 185 ms 59496 KB Output is correct
79 Correct 284 ms 64616 KB Output is correct
80 Correct 30 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 35820 KB Output is correct
84 Correct 27 ms 36332 KB Output is correct
85 Correct 364 ms 72936 KB Output is correct
86 Correct 27 ms 36332 KB Output is correct
87 Correct 269 ms 59624 KB Output is correct
88 Correct 24 ms 35692 KB Output is correct
89 Correct 236 ms 57580 KB Output is correct
90 Correct 347 ms 71016 KB Output is correct
91 Correct 513 ms 90896 KB Output is correct
92 Correct 172 ms 60640 KB Output is correct
93 Correct 27 ms 36332 KB Output is correct
94 Correct 24 ms 35692 KB Output is correct
95 Correct 282 ms 66272 KB Output is correct
96 Correct 380 ms 65260 KB Output is correct
97 Correct 29 ms 36588 KB Output is correct
98 Correct 24 ms 35692 KB Output is correct
99 Correct 171 ms 58216 KB Output is correct
100 Correct 28 ms 36460 KB Output is correct
101 Correct 241 ms 59500 KB Output is correct
102 Correct 440 ms 71152 KB Output is correct
103 Correct 28 ms 36588 KB Output is correct
104 Correct 29 ms 36716 KB Output is correct
105 Correct 27 ms 36332 KB Output is correct
106 Correct 27 ms 36332 KB Output is correct
107 Correct 27 ms 36332 KB Output is correct
108 Correct 1046 ms 137776 KB Output is correct
109 Correct 1799 ms 152172 KB Output is correct
110 Correct 1701 ms 152428 KB Output is correct
111 Correct 2499 ms 172312 KB Output is correct
112 Correct 2467 ms 185492 KB Output is correct
113 Correct 1729 ms 183608 KB Output is correct
114 Correct 1944 ms 155108 KB Output is correct
115 Correct 1933 ms 154984 KB Output is correct
116 Correct 2655 ms 243316 KB Output is correct
117 Runtime error 2350 ms 262148 KB Execution killed with signal 9 (could be triggered by violating memory limits)
118 Halted 0 ms 0 KB -