답안 #109764

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
109764 2019-05-08T03:29:37 Z luciocf Mergers (JOI19_mergers) C++14
34 / 100
3000 ms 15976 KB
#include <bits/stdc++.h>

using namespace std;

const int maxn = 1e5+10;

int n, k;

int pai[maxn], peso[maxn];

int s[maxn];

int par[maxn], grau[maxn];

bool mark[maxn];

vector<int> grafo[maxn], state[maxn];

void init(void)
{
	for (int i = 1; i <= n; i++)
		pai[i] = i, peso[i] = 1;
}

int Find(int x)
{
	if (pai[x] == x) return x;
	return pai[x] = Find(pai[x]);
}

void Join(int x, int y)
{
	x = Find(x), y = Find(y);
	if (x == y) return;

	if (peso[x] < peso[y]) swap(x, y);

	pai[y] = x, peso[x] += peso[y];
}

void dfs(int u, int p)
{
	for (auto v: grafo[u])
	{
		if (v == p) continue;

		par[v] = u;
		dfs(v, u);
	}
}

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

	for (int i = 1; i < n; i++)
	{
		int u, v;
		scanf("%d %d", &u, &v);

		grafo[u].push_back(v);
		grafo[v].push_back(u);
	}

	for (int i = 1; i <= n; i++)
	{
		scanf("%d", &s[i]);

		state[s[i]].push_back(i);
	}

	init();

	for (int i = 1; i <= n; i++)
	{
		if (mark[s[i]]) continue;
		mark[s[i]] = 1;

		par[i] = 0;
		dfs(i, 0);

		for (auto v: state[s[i]])
		{
			while (v != i)
			{
				Join(i, v);
				v = par[v];
			}
		}
	}

	for (int i = 1; i <= n; i++)
		for (auto v: grafo[i])
			if (Find(v) != Find(i))
				grau[Find(v)]++;

	int leaf = 0;

	for (int i = 1; i <= n; i++)
		if (grau[i] == 1)
			leaf++;

	printf("%d\n", (leaf+1)/2);
}

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]
  scanf("%d %d", &n, &k);
  ~~~~~^~~~~~~~~~~~~~~~~
mergers.cpp:59:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   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]
   scanf("%d", &s[i]);
   ~~~~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 5120 KB Output is correct
2 Correct 6 ms 4992 KB Output is correct
3 Correct 6 ms 5120 KB Output is correct
4 Correct 6 ms 4992 KB Output is correct
5 Correct 7 ms 4992 KB Output is correct
6 Correct 7 ms 4992 KB Output is correct
7 Correct 6 ms 4992 KB Output is correct
8 Correct 7 ms 5120 KB Output is correct
9 Correct 7 ms 5120 KB Output is correct
10 Correct 7 ms 5120 KB Output is correct
11 Correct 7 ms 5120 KB Output is correct
12 Correct 8 ms 4992 KB Output is correct
13 Correct 7 ms 4992 KB Output is correct
14 Correct 6 ms 4992 KB Output is correct
15 Correct 7 ms 5120 KB Output is correct
16 Correct 6 ms 5120 KB Output is correct
17 Correct 6 ms 4992 KB Output is correct
18 Correct 7 ms 4992 KB Output is correct
19 Correct 6 ms 4992 KB Output is correct
20 Correct 7 ms 5120 KB Output is correct
21 Correct 8 ms 5120 KB Output is correct
22 Correct 7 ms 5120 KB Output is correct
23 Correct 8 ms 5120 KB Output is correct
24 Correct 8 ms 5120 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 5120 KB Output is correct
2 Correct 6 ms 4992 KB Output is correct
3 Correct 6 ms 5120 KB Output is correct
4 Correct 6 ms 4992 KB Output is correct
5 Correct 7 ms 4992 KB Output is correct
6 Correct 7 ms 4992 KB Output is correct
7 Correct 6 ms 4992 KB Output is correct
8 Correct 7 ms 5120 KB Output is correct
9 Correct 7 ms 5120 KB Output is correct
10 Correct 7 ms 5120 KB Output is correct
11 Correct 7 ms 5120 KB Output is correct
12 Correct 8 ms 4992 KB Output is correct
13 Correct 7 ms 4992 KB Output is correct
14 Correct 6 ms 4992 KB Output is correct
15 Correct 7 ms 5120 KB Output is correct
16 Correct 6 ms 5120 KB Output is correct
17 Correct 6 ms 4992 KB Output is correct
18 Correct 7 ms 4992 KB Output is correct
19 Correct 6 ms 4992 KB Output is correct
20 Correct 7 ms 5120 KB Output is correct
21 Correct 8 ms 5120 KB Output is correct
22 Correct 7 ms 5120 KB Output is correct
23 Correct 8 ms 5120 KB Output is correct
24 Correct 8 ms 5120 KB Output is correct
25 Correct 7 ms 4992 KB Output is correct
26 Correct 158 ms 5320 KB Output is correct
27 Correct 16 ms 5248 KB Output is correct
28 Correct 159 ms 5456 KB Output is correct
29 Correct 155 ms 5368 KB Output is correct
30 Correct 19 ms 5248 KB Output is correct
31 Correct 6 ms 4992 KB Output is correct
32 Correct 281 ms 5492 KB Output is correct
33 Correct 6 ms 4992 KB Output is correct
34 Correct 11 ms 5248 KB Output is correct
35 Correct 214 ms 5368 KB Output is correct
36 Correct 12 ms 5248 KB Output is correct
37 Correct 86 ms 5248 KB Output is correct
38 Correct 7 ms 5120 KB Output is correct
39 Correct 102 ms 5368 KB Output is correct
40 Correct 9 ms 5248 KB Output is correct
41 Correct 10 ms 5248 KB Output is correct
42 Correct 33 ms 5248 KB Output is correct
43 Correct 32 ms 5376 KB Output is correct
44 Correct 7 ms 5120 KB Output is correct
45 Correct 218 ms 5496 KB Output is correct
46 Correct 92 ms 5220 KB Output is correct
47 Correct 6 ms 4992 KB Output is correct
48 Correct 55 ms 5248 KB Output is correct
49 Correct 65 ms 5368 KB Output is correct
50 Correct 184 ms 5428 KB Output is correct
51 Correct 52 ms 5296 KB Output is correct
52 Correct 9 ms 5248 KB Output is correct
53 Correct 33 ms 5248 KB Output is correct
54 Correct 11 ms 5248 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 5120 KB Output is correct
2 Correct 6 ms 4992 KB Output is correct
3 Correct 6 ms 5120 KB Output is correct
4 Correct 6 ms 4992 KB Output is correct
5 Correct 7 ms 4992 KB Output is correct
6 Correct 7 ms 4992 KB Output is correct
7 Correct 6 ms 4992 KB Output is correct
8 Correct 7 ms 5120 KB Output is correct
9 Correct 7 ms 5120 KB Output is correct
10 Correct 7 ms 5120 KB Output is correct
11 Correct 7 ms 5120 KB Output is correct
12 Correct 8 ms 4992 KB Output is correct
13 Correct 7 ms 4992 KB Output is correct
14 Correct 6 ms 4992 KB Output is correct
15 Correct 7 ms 5120 KB Output is correct
16 Correct 6 ms 5120 KB Output is correct
17 Correct 6 ms 4992 KB Output is correct
18 Correct 7 ms 4992 KB Output is correct
19 Correct 6 ms 4992 KB Output is correct
20 Correct 7 ms 5120 KB Output is correct
21 Correct 8 ms 5120 KB Output is correct
22 Correct 7 ms 5120 KB Output is correct
23 Correct 8 ms 5120 KB Output is correct
24 Correct 8 ms 5120 KB Output is correct
25 Correct 6 ms 5120 KB Output is correct
26 Correct 314 ms 12064 KB Output is correct
27 Correct 569 ms 11800 KB Output is correct
28 Correct 12 ms 5248 KB Output is correct
29 Correct 6 ms 4992 KB Output is correct
30 Correct 7 ms 4992 KB Output is correct
31 Correct 627 ms 11720 KB Output is correct
32 Correct 10 ms 5248 KB Output is correct
33 Execution timed out 3084 ms 15976 KB Time limit exceeded
34 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 247 ms 10864 KB Output is correct
2 Execution timed out 3011 ms 13688 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 5120 KB Output is correct
2 Correct 6 ms 4992 KB Output is correct
3 Correct 6 ms 5120 KB Output is correct
4 Correct 6 ms 4992 KB Output is correct
5 Correct 7 ms 4992 KB Output is correct
6 Correct 7 ms 4992 KB Output is correct
7 Correct 6 ms 4992 KB Output is correct
8 Correct 7 ms 5120 KB Output is correct
9 Correct 7 ms 5120 KB Output is correct
10 Correct 7 ms 5120 KB Output is correct
11 Correct 7 ms 5120 KB Output is correct
12 Correct 8 ms 4992 KB Output is correct
13 Correct 7 ms 4992 KB Output is correct
14 Correct 6 ms 4992 KB Output is correct
15 Correct 7 ms 5120 KB Output is correct
16 Correct 6 ms 5120 KB Output is correct
17 Correct 6 ms 4992 KB Output is correct
18 Correct 7 ms 4992 KB Output is correct
19 Correct 6 ms 4992 KB Output is correct
20 Correct 7 ms 5120 KB Output is correct
21 Correct 8 ms 5120 KB Output is correct
22 Correct 7 ms 5120 KB Output is correct
23 Correct 8 ms 5120 KB Output is correct
24 Correct 8 ms 5120 KB Output is correct
25 Correct 7 ms 4992 KB Output is correct
26 Correct 158 ms 5320 KB Output is correct
27 Correct 16 ms 5248 KB Output is correct
28 Correct 159 ms 5456 KB Output is correct
29 Correct 155 ms 5368 KB Output is correct
30 Correct 19 ms 5248 KB Output is correct
31 Correct 6 ms 4992 KB Output is correct
32 Correct 281 ms 5492 KB Output is correct
33 Correct 6 ms 4992 KB Output is correct
34 Correct 11 ms 5248 KB Output is correct
35 Correct 214 ms 5368 KB Output is correct
36 Correct 12 ms 5248 KB Output is correct
37 Correct 86 ms 5248 KB Output is correct
38 Correct 7 ms 5120 KB Output is correct
39 Correct 102 ms 5368 KB Output is correct
40 Correct 9 ms 5248 KB Output is correct
41 Correct 10 ms 5248 KB Output is correct
42 Correct 33 ms 5248 KB Output is correct
43 Correct 32 ms 5376 KB Output is correct
44 Correct 7 ms 5120 KB Output is correct
45 Correct 218 ms 5496 KB Output is correct
46 Correct 92 ms 5220 KB Output is correct
47 Correct 6 ms 4992 KB Output is correct
48 Correct 55 ms 5248 KB Output is correct
49 Correct 65 ms 5368 KB Output is correct
50 Correct 184 ms 5428 KB Output is correct
51 Correct 52 ms 5296 KB Output is correct
52 Correct 9 ms 5248 KB Output is correct
53 Correct 33 ms 5248 KB Output is correct
54 Correct 11 ms 5248 KB Output is correct
55 Correct 6 ms 5120 KB Output is correct
56 Correct 314 ms 12064 KB Output is correct
57 Correct 569 ms 11800 KB Output is correct
58 Correct 12 ms 5248 KB Output is correct
59 Correct 6 ms 4992 KB Output is correct
60 Correct 7 ms 4992 KB Output is correct
61 Correct 627 ms 11720 KB Output is correct
62 Correct 10 ms 5248 KB Output is correct
63 Execution timed out 3084 ms 15976 KB Time limit exceeded
64 Halted 0 ms 0 KB -