Submission #570318

# Submission time Handle Problem Language Result Execution time Memory
570318 2022-05-29T08:20:03 Z franfill Mergers (JOI19_mergers) C++17
70 / 100
3000 ms 119264 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]++;
				cerr << conn[x] << "\n";
			}
		}

		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";
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 296 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 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 1 ms 296 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 304 KB Output is correct
15 Correct 1 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 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 296 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
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 296 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 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 1 ms 296 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 304 KB Output is correct
15 Correct 1 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 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 296 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 19 ms 596 KB Output is correct
27 Correct 4 ms 596 KB Output is correct
28 Correct 26 ms 868 KB Output is correct
29 Correct 33 ms 596 KB Output is correct
30 Correct 3 ms 468 KB Output is correct
31 Correct 1 ms 212 KB Output is correct
32 Correct 9 ms 984 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 3 ms 456 KB Output is correct
35 Correct 34 ms 596 KB Output is correct
36 Correct 6 ms 468 KB Output is correct
37 Correct 7 ms 468 KB Output is correct
38 Correct 1 ms 304 KB Output is correct
39 Correct 6 ms 576 KB Output is correct
40 Correct 3 ms 468 KB Output is correct
41 Correct 3 ms 468 KB Output is correct
42 Correct 8 ms 596 KB Output is correct
43 Correct 3 ms 1108 KB Output is correct
44 Correct 2 ms 300 KB Output is correct
45 Correct 7 ms 732 KB Output is correct
46 Correct 8 ms 584 KB Output is correct
47 Correct 1 ms 300 KB Output is correct
48 Correct 7 ms 468 KB Output is correct
49 Correct 32 ms 632 KB Output is correct
50 Correct 32 ms 800 KB Output is correct
51 Correct 7 ms 460 KB Output is correct
52 Correct 3 ms 564 KB Output is correct
53 Correct 8 ms 508 KB Output is correct
54 Correct 3 ms 460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 296 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 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 1 ms 296 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 304 KB Output is correct
15 Correct 1 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 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 296 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 117 ms 7956 KB Output is correct
27 Correct 126 ms 7756 KB Output is correct
28 Correct 4 ms 596 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 1 ms 212 KB Output is correct
31 Correct 127 ms 7728 KB Output is correct
32 Correct 3 ms 568 KB Output is correct
33 Correct 165 ms 28044 KB Output is correct
34 Correct 130 ms 7744 KB Output is correct
35 Correct 3 ms 468 KB Output is correct
36 Correct 135 ms 10056 KB Output is correct
37 Correct 5 ms 468 KB Output is correct
38 Correct 4 ms 456 KB Output is correct
39 Correct 120 ms 8080 KB Output is correct
40 Correct 3 ms 1108 KB Output is correct
41 Correct 119 ms 7780 KB Output is correct
42 Correct 140 ms 14420 KB Output is correct
43 Correct 1 ms 212 KB Output is correct
44 Correct 150 ms 28876 KB Output is correct
45 Correct 126 ms 18268 KB Output is correct
46 Correct 3 ms 468 KB Output is correct
47 Correct 4 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 104 ms 7648 KB Output is correct
2 Correct 1135 ms 11172 KB Output is correct
3 Correct 19 ms 596 KB Output is correct
4 Correct 4 ms 544 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 229 ms 8540 KB Output is correct
9 Correct 5 ms 468 KB Output is correct
10 Correct 134 ms 7716 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 121 ms 7756 KB Output is correct
13 Correct 347 ms 8724 KB Output is correct
14 Correct 1121 ms 10780 KB Output is correct
15 Correct 94 ms 8004 KB Output is correct
16 Correct 11 ms 592 KB Output is correct
17 Correct 2 ms 212 KB Output is correct
18 Correct 934 ms 10924 KB Output is correct
19 Correct 1142 ms 22032 KB Output is correct
20 Correct 7 ms 468 KB Output is correct
21 Correct 1 ms 296 KB Output is correct
22 Correct 315 ms 8936 KB Output is correct
23 Correct 6 ms 468 KB Output is correct
24 Correct 220 ms 7884 KB Output is correct
25 Correct 1120 ms 18372 KB Output is correct
26 Correct 35 ms 760 KB Output is correct
27 Correct 36 ms 780 KB Output is correct
28 Correct 6 ms 572 KB Output is correct
29 Correct 3 ms 468 KB Output is correct
30 Correct 9 ms 564 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 296 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 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 1 ms 296 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 304 KB Output is correct
15 Correct 1 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 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 296 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 19 ms 596 KB Output is correct
27 Correct 4 ms 596 KB Output is correct
28 Correct 26 ms 868 KB Output is correct
29 Correct 33 ms 596 KB Output is correct
30 Correct 3 ms 468 KB Output is correct
31 Correct 1 ms 212 KB Output is correct
32 Correct 9 ms 984 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 3 ms 456 KB Output is correct
35 Correct 34 ms 596 KB Output is correct
36 Correct 6 ms 468 KB Output is correct
37 Correct 7 ms 468 KB Output is correct
38 Correct 1 ms 304 KB Output is correct
39 Correct 6 ms 576 KB Output is correct
40 Correct 3 ms 468 KB Output is correct
41 Correct 3 ms 468 KB Output is correct
42 Correct 8 ms 596 KB Output is correct
43 Correct 3 ms 1108 KB Output is correct
44 Correct 2 ms 300 KB Output is correct
45 Correct 7 ms 732 KB Output is correct
46 Correct 8 ms 584 KB Output is correct
47 Correct 1 ms 300 KB Output is correct
48 Correct 7 ms 468 KB Output is correct
49 Correct 32 ms 632 KB Output is correct
50 Correct 32 ms 800 KB Output is correct
51 Correct 7 ms 460 KB Output is correct
52 Correct 3 ms 564 KB Output is correct
53 Correct 8 ms 508 KB Output is correct
54 Correct 3 ms 460 KB Output is correct
55 Correct 1 ms 212 KB Output is correct
56 Correct 117 ms 7956 KB Output is correct
57 Correct 126 ms 7756 KB Output is correct
58 Correct 4 ms 596 KB Output is correct
59 Correct 1 ms 212 KB Output is correct
60 Correct 1 ms 212 KB Output is correct
61 Correct 127 ms 7728 KB Output is correct
62 Correct 3 ms 568 KB Output is correct
63 Correct 165 ms 28044 KB Output is correct
64 Correct 130 ms 7744 KB Output is correct
65 Correct 3 ms 468 KB Output is correct
66 Correct 135 ms 10056 KB Output is correct
67 Correct 5 ms 468 KB Output is correct
68 Correct 4 ms 456 KB Output is correct
69 Correct 120 ms 8080 KB Output is correct
70 Correct 3 ms 1108 KB Output is correct
71 Correct 119 ms 7780 KB Output is correct
72 Correct 140 ms 14420 KB Output is correct
73 Correct 1 ms 212 KB Output is correct
74 Correct 150 ms 28876 KB Output is correct
75 Correct 126 ms 18268 KB Output is correct
76 Correct 3 ms 468 KB Output is correct
77 Correct 4 ms 468 KB Output is correct
78 Correct 104 ms 7648 KB Output is correct
79 Correct 1135 ms 11172 KB Output is correct
80 Correct 19 ms 596 KB Output is correct
81 Correct 4 ms 544 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 229 ms 8540 KB Output is correct
86 Correct 5 ms 468 KB Output is correct
87 Correct 134 ms 7716 KB Output is correct
88 Correct 1 ms 212 KB Output is correct
89 Correct 121 ms 7756 KB Output is correct
90 Correct 347 ms 8724 KB Output is correct
91 Correct 1121 ms 10780 KB Output is correct
92 Correct 94 ms 8004 KB Output is correct
93 Correct 11 ms 592 KB Output is correct
94 Correct 2 ms 212 KB Output is correct
95 Correct 934 ms 10924 KB Output is correct
96 Correct 1142 ms 22032 KB Output is correct
97 Correct 7 ms 468 KB Output is correct
98 Correct 1 ms 296 KB Output is correct
99 Correct 315 ms 8936 KB Output is correct
100 Correct 6 ms 468 KB Output is correct
101 Correct 220 ms 7884 KB Output is correct
102 Correct 1120 ms 18372 KB Output is correct
103 Correct 35 ms 760 KB Output is correct
104 Correct 36 ms 780 KB Output is correct
105 Correct 6 ms 572 KB Output is correct
106 Correct 3 ms 468 KB Output is correct
107 Correct 9 ms 564 KB Output is correct
108 Correct 1124 ms 41344 KB Output is correct
109 Correct 800 ms 87732 KB Output is correct
110 Correct 861 ms 119264 KB Output is correct
111 Execution timed out 3096 ms 112492 KB Time limit exceeded
112 Halted 0 ms 0 KB -