답안 #570320

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
570320 2022-05-29T08:21:00 Z franfill Mergers (JOI19_mergers) C++17
100 / 100
930 ms 101016 KB
#include<bits/stdc++.h>
using namespace std;

struct dsu
{
	vector < int > pa;
	vector < int > siz;

	dsu(int n)
	{
		pa.resize(n);
		siz.resize(n, 1);
		iota(pa.begin(), pa.end(), 0);
	}
	
	dsu(int n, vector < int > &sizes) : siz(sizes)
	{
		pa.resize(n);
		iota(pa.begin(), pa.end(), 0);
	}

	int find(int x)
	{
		return pa[x]==x?x:(pa[x]=find(pa[x]));
	}

	bool unite(int a, int b)
	{
		a=find(a), b=find(b);
		if (a == b) return false;
		if (siz[a] < siz[b]) swap(a, b);
		siz[a] += siz[b];
		pa[b] = a;
		return true;
	}

	int get_siz(int x)
	{
		return siz[find(x)];
	}
};

int main()
{
	int n, k;
	cin >> n >> k;
	vector < vector < int > > g(n);
	for (int i = 0; i < n-1; i++)
	{
		int a, b;
		cin >> a >> b;
		a--, b--;
		g[a].push_back(b);
		g[b].push_back(a);
	}
	vector < int > state(n), sizes(k, 0);
	for (auto &s : state)
	{
		cin >> s; s--;
		sizes[s]++;
	}
	dsu cities(n), states(k, sizes);	
	vector < int > conn(n, 0);
	int ans = 0;
	auto dfs = [&] (int x, int p, auto dfs) -> bool
	{
		for (auto y : g[x]) if (y != p)
		{
			if (dfs(y, x, dfs))
			{
				conn[x] += conn[y];
				//cerr << x << "->" << y << "," << conn[x] << "\n";
				cities.unite(x, y);
				states.unite(state[x], state[y]);
			}
			else
			{
				//cerr << x << "++\n";
				conn[x]++;
			}
		}

		if (cities.get_siz(x) == states.get_siz(state[x]))
		{
			if (p != -1) conn[x]++;
			//cerr << x << ": " << conn[x] << "\n";
			if (conn[x] == 1) ans++;
			return false;
		}
		else
		{
			return true;
		}
	};
	dfs(0, -1, dfs);
	cout << ans/2 + ans%2 << "\n";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 288 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 296 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 288 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 296 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 3 ms 468 KB Output is correct
27 Correct 3 ms 468 KB Output is correct
28 Correct 4 ms 784 KB Output is correct
29 Correct 3 ms 468 KB Output is correct
30 Correct 3 ms 468 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 4 ms 852 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 4 ms 468 KB Output is correct
35 Correct 4 ms 468 KB Output is correct
36 Correct 8 ms 468 KB Output is correct
37 Correct 3 ms 468 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Correct 5 ms 472 KB Output is correct
40 Correct 3 ms 448 KB Output is correct
41 Correct 3 ms 524 KB Output is correct
42 Correct 3 ms 472 KB Output is correct
43 Correct 5 ms 940 KB Output is correct
44 Correct 1 ms 216 KB Output is correct
45 Correct 4 ms 604 KB Output is correct
46 Correct 3 ms 476 KB Output is correct
47 Correct 1 ms 212 KB Output is correct
48 Correct 3 ms 476 KB Output is correct
49 Correct 3 ms 468 KB Output is correct
50 Correct 3 ms 576 KB Output is correct
51 Correct 3 ms 468 KB Output is correct
52 Correct 5 ms 468 KB Output is correct
53 Correct 3 ms 468 KB Output is correct
54 Correct 3 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 288 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 296 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 107 ms 7724 KB Output is correct
27 Correct 128 ms 7452 KB Output is correct
28 Correct 3 ms 468 KB Output is correct
29 Correct 0 ms 212 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 134 ms 7356 KB Output is correct
32 Correct 3 ms 468 KB Output is correct
33 Correct 129 ms 23348 KB Output is correct
34 Correct 146 ms 7420 KB Output is correct
35 Correct 4 ms 468 KB Output is correct
36 Correct 175 ms 9160 KB Output is correct
37 Correct 3 ms 468 KB Output is correct
38 Correct 3 ms 400 KB Output is correct
39 Correct 114 ms 7656 KB Output is correct
40 Correct 3 ms 980 KB Output is correct
41 Correct 157 ms 7456 KB Output is correct
42 Correct 156 ms 12584 KB Output is correct
43 Correct 1 ms 212 KB Output is correct
44 Correct 139 ms 24008 KB Output is correct
45 Correct 139 ms 15664 KB Output is correct
46 Correct 3 ms 468 KB Output is correct
47 Correct 3 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 134 ms 7684 KB Output is correct
2 Correct 113 ms 8908 KB Output is correct
3 Correct 5 ms 468 KB Output is correct
4 Correct 8 ms 468 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 3 ms 468 KB Output is correct
8 Correct 147 ms 7968 KB Output is correct
9 Correct 3 ms 468 KB Output is correct
10 Correct 131 ms 7476 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 138 ms 7372 KB Output is correct
13 Correct 152 ms 7988 KB Output is correct
14 Correct 154 ms 8584 KB Output is correct
15 Correct 91 ms 7708 KB Output is correct
16 Correct 4 ms 468 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 121 ms 8840 KB Output is correct
19 Correct 162 ms 17304 KB Output is correct
20 Correct 5 ms 468 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 181 ms 8300 KB Output is correct
23 Correct 3 ms 468 KB Output is correct
24 Correct 140 ms 7472 KB Output is correct
25 Correct 162 ms 14356 KB Output is correct
26 Correct 4 ms 468 KB Output is correct
27 Correct 3 ms 596 KB Output is correct
28 Correct 4 ms 468 KB Output is correct
29 Correct 3 ms 468 KB Output is correct
30 Correct 3 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 288 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 296 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 3 ms 468 KB Output is correct
27 Correct 3 ms 468 KB Output is correct
28 Correct 4 ms 784 KB Output is correct
29 Correct 3 ms 468 KB Output is correct
30 Correct 3 ms 468 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 4 ms 852 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 4 ms 468 KB Output is correct
35 Correct 4 ms 468 KB Output is correct
36 Correct 8 ms 468 KB Output is correct
37 Correct 3 ms 468 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Correct 5 ms 472 KB Output is correct
40 Correct 3 ms 448 KB Output is correct
41 Correct 3 ms 524 KB Output is correct
42 Correct 3 ms 472 KB Output is correct
43 Correct 5 ms 940 KB Output is correct
44 Correct 1 ms 216 KB Output is correct
45 Correct 4 ms 604 KB Output is correct
46 Correct 3 ms 476 KB Output is correct
47 Correct 1 ms 212 KB Output is correct
48 Correct 3 ms 476 KB Output is correct
49 Correct 3 ms 468 KB Output is correct
50 Correct 3 ms 576 KB Output is correct
51 Correct 3 ms 468 KB Output is correct
52 Correct 5 ms 468 KB Output is correct
53 Correct 3 ms 468 KB Output is correct
54 Correct 3 ms 468 KB Output is correct
55 Correct 1 ms 212 KB Output is correct
56 Correct 107 ms 7724 KB Output is correct
57 Correct 128 ms 7452 KB Output is correct
58 Correct 3 ms 468 KB Output is correct
59 Correct 0 ms 212 KB Output is correct
60 Correct 0 ms 212 KB Output is correct
61 Correct 134 ms 7356 KB Output is correct
62 Correct 3 ms 468 KB Output is correct
63 Correct 129 ms 23348 KB Output is correct
64 Correct 146 ms 7420 KB Output is correct
65 Correct 4 ms 468 KB Output is correct
66 Correct 175 ms 9160 KB Output is correct
67 Correct 3 ms 468 KB Output is correct
68 Correct 3 ms 400 KB Output is correct
69 Correct 114 ms 7656 KB Output is correct
70 Correct 3 ms 980 KB Output is correct
71 Correct 157 ms 7456 KB Output is correct
72 Correct 156 ms 12584 KB Output is correct
73 Correct 1 ms 212 KB Output is correct
74 Correct 139 ms 24008 KB Output is correct
75 Correct 139 ms 15664 KB Output is correct
76 Correct 3 ms 468 KB Output is correct
77 Correct 3 ms 468 KB Output is correct
78 Correct 134 ms 7684 KB Output is correct
79 Correct 113 ms 8908 KB Output is correct
80 Correct 5 ms 468 KB Output is correct
81 Correct 8 ms 468 KB Output is correct
82 Correct 1 ms 212 KB Output is correct
83 Correct 1 ms 212 KB Output is correct
84 Correct 3 ms 468 KB Output is correct
85 Correct 147 ms 7968 KB Output is correct
86 Correct 3 ms 468 KB Output is correct
87 Correct 131 ms 7476 KB Output is correct
88 Correct 1 ms 212 KB Output is correct
89 Correct 138 ms 7372 KB Output is correct
90 Correct 152 ms 7988 KB Output is correct
91 Correct 154 ms 8584 KB Output is correct
92 Correct 91 ms 7708 KB Output is correct
93 Correct 4 ms 468 KB Output is correct
94 Correct 0 ms 212 KB Output is correct
95 Correct 121 ms 8840 KB Output is correct
96 Correct 162 ms 17304 KB Output is correct
97 Correct 5 ms 468 KB Output is correct
98 Correct 1 ms 212 KB Output is correct
99 Correct 181 ms 8300 KB Output is correct
100 Correct 3 ms 468 KB Output is correct
101 Correct 140 ms 7472 KB Output is correct
102 Correct 162 ms 14356 KB Output is correct
103 Correct 4 ms 468 KB Output is correct
104 Correct 3 ms 596 KB Output is correct
105 Correct 4 ms 468 KB Output is correct
106 Correct 3 ms 468 KB Output is correct
107 Correct 3 ms 468 KB Output is correct
108 Correct 834 ms 39808 KB Output is correct
109 Correct 803 ms 76300 KB Output is correct
110 Correct 822 ms 101016 KB Output is correct
111 Correct 875 ms 93260 KB Output is correct
112 Correct 927 ms 72148 KB Output is correct
113 Correct 691 ms 43156 KB Output is correct
114 Correct 735 ms 36360 KB Output is correct
115 Correct 730 ms 36544 KB Output is correct
116 Correct 868 ms 37664 KB Output is correct
117 Correct 850 ms 42276 KB Output is correct
118 Correct 804 ms 35680 KB Output is correct
119 Correct 908 ms 42320 KB Output is correct
120 Correct 898 ms 73576 KB Output is correct
121 Correct 886 ms 42404 KB Output is correct
122 Correct 855 ms 39928 KB Output is correct
123 Correct 735 ms 43824 KB Output is correct
124 Correct 930 ms 41720 KB Output is correct