Submission #472194

# Submission time Handle Problem Language Result Execution time Memory
472194 2021-09-13T09:11:32 Z prvocislo Mergers (JOI19_mergers) C++17
100 / 100
883 ms 75404 KB
#include <iostream>
#include <vector>
using namespace std;

const int maxn = 5e5 + 5;
int n, k, t = 0;
int l[maxn], r[maxn];
int ls[maxn], rs[maxn], s[maxn];
int l2[maxn], r2[maxn];
vector<int> g[maxn];
bool c[maxn];
void dfs_time(int u, int p = -1)
{
	l[u] = t++;
	for (int v : g[u]) if (v != p) dfs_time(v, u);
	r[u] = t - 1;
}
void dfs_critical(int u, int p = -1)
{
	l2[u] = min(l[u], ls[s[u]]);
	r2[u] = max(r[u], rs[s[u]]);
	for (int v : g[u]) if (v != p)
	{
		dfs_critical(v, u);
		l2[u] = min(l2[u], l2[v]);
		r2[u] = max(r2[u], r2[v]);
	}
	if (u && l[u] == l2[u] && r[u] == r2[u])
		c[u] = true;
}
int cuts = 0;
bool dfs_cover(int u, int p = -1)
{
	bool is_covered = false;
	for (int v : g[u]) if (v != p) is_covered |= dfs_cover(v, u);
	if (!is_covered && c[u])
	{
		cuts++;
		return true;
	}
	return is_covered;
}
int dfs_cuts_root_can_reach(int u, int p = -1)
{
	if (c[u]) return 1;
	int sum = 0;
	for (int v : g[u]) if (v != p) sum += dfs_cuts_root_can_reach(v, u);
	return sum;
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> n >> k;
	for (int i = 0, a, b; i < n - 1; i++)
	{
		cin >> a >> b;
		g[--a].push_back(--b);
		g[b].push_back(a);
	}
	dfs_time(0);
	for (int i = 0; i < k; i++) ls[i] = 1e9, rs[i] = -1e9;
	for (int i = 0; i < n; i++)
	{
		cin >> s[i]; s[i]--;
		ls[s[i]] = min(ls[s[i]], l[i]);
		rs[s[i]] = max(rs[s[i]], r[i]);
	}
	dfs_critical(0);
	dfs_cover(0);
	int ans = (cuts + 1) / 2;
	int cnt = dfs_cuts_root_can_reach(0);
	if (cuts % 2 == 0 && cnt == 1) ans++; // musime to este spojit aj s korenom
	cout << ans << "\n";
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 7 ms 12108 KB Output is correct
2 Correct 7 ms 12056 KB Output is correct
3 Correct 7 ms 12108 KB Output is correct
4 Correct 7 ms 12104 KB Output is correct
5 Correct 7 ms 12068 KB Output is correct
6 Correct 7 ms 12064 KB Output is correct
7 Correct 7 ms 12108 KB Output is correct
8 Correct 7 ms 12108 KB Output is correct
9 Correct 7 ms 12108 KB Output is correct
10 Correct 7 ms 12108 KB Output is correct
11 Correct 7 ms 12064 KB Output is correct
12 Correct 7 ms 12068 KB Output is correct
13 Correct 7 ms 12044 KB Output is correct
14 Correct 7 ms 12108 KB Output is correct
15 Correct 7 ms 12108 KB Output is correct
16 Correct 7 ms 12108 KB Output is correct
17 Correct 7 ms 12108 KB Output is correct
18 Correct 7 ms 12108 KB Output is correct
19 Correct 7 ms 12108 KB Output is correct
20 Correct 7 ms 12236 KB Output is correct
21 Correct 7 ms 12008 KB Output is correct
22 Correct 7 ms 12108 KB Output is correct
23 Correct 7 ms 12108 KB Output is correct
24 Correct 7 ms 12124 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 12108 KB Output is correct
2 Correct 7 ms 12056 KB Output is correct
3 Correct 7 ms 12108 KB Output is correct
4 Correct 7 ms 12104 KB Output is correct
5 Correct 7 ms 12068 KB Output is correct
6 Correct 7 ms 12064 KB Output is correct
7 Correct 7 ms 12108 KB Output is correct
8 Correct 7 ms 12108 KB Output is correct
9 Correct 7 ms 12108 KB Output is correct
10 Correct 7 ms 12108 KB Output is correct
11 Correct 7 ms 12064 KB Output is correct
12 Correct 7 ms 12068 KB Output is correct
13 Correct 7 ms 12044 KB Output is correct
14 Correct 7 ms 12108 KB Output is correct
15 Correct 7 ms 12108 KB Output is correct
16 Correct 7 ms 12108 KB Output is correct
17 Correct 7 ms 12108 KB Output is correct
18 Correct 7 ms 12108 KB Output is correct
19 Correct 7 ms 12108 KB Output is correct
20 Correct 7 ms 12236 KB Output is correct
21 Correct 7 ms 12008 KB Output is correct
22 Correct 7 ms 12108 KB Output is correct
23 Correct 7 ms 12108 KB Output is correct
24 Correct 7 ms 12124 KB Output is correct
25 Correct 7 ms 12108 KB Output is correct
26 Correct 9 ms 12236 KB Output is correct
27 Correct 9 ms 12324 KB Output is correct
28 Correct 9 ms 12440 KB Output is correct
29 Correct 9 ms 12236 KB Output is correct
30 Correct 9 ms 12208 KB Output is correct
31 Correct 7 ms 12072 KB Output is correct
32 Correct 9 ms 12500 KB Output is correct
33 Correct 8 ms 12108 KB Output is correct
34 Correct 9 ms 12220 KB Output is correct
35 Correct 10 ms 12236 KB Output is correct
36 Correct 9 ms 12236 KB Output is correct
37 Correct 9 ms 12324 KB Output is correct
38 Correct 7 ms 12068 KB Output is correct
39 Correct 9 ms 12236 KB Output is correct
40 Correct 8 ms 12236 KB Output is correct
41 Correct 9 ms 12204 KB Output is correct
42 Correct 9 ms 12300 KB Output is correct
43 Correct 9 ms 12464 KB Output is correct
44 Correct 7 ms 12108 KB Output is correct
45 Correct 9 ms 12360 KB Output is correct
46 Correct 9 ms 12236 KB Output is correct
47 Correct 7 ms 12112 KB Output is correct
48 Correct 9 ms 12236 KB Output is correct
49 Correct 9 ms 12336 KB Output is correct
50 Correct 9 ms 12336 KB Output is correct
51 Correct 9 ms 12216 KB Output is correct
52 Correct 9 ms 12336 KB Output is correct
53 Correct 9 ms 12240 KB Output is correct
54 Correct 9 ms 12244 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 12108 KB Output is correct
2 Correct 7 ms 12056 KB Output is correct
3 Correct 7 ms 12108 KB Output is correct
4 Correct 7 ms 12104 KB Output is correct
5 Correct 7 ms 12068 KB Output is correct
6 Correct 7 ms 12064 KB Output is correct
7 Correct 7 ms 12108 KB Output is correct
8 Correct 7 ms 12108 KB Output is correct
9 Correct 7 ms 12108 KB Output is correct
10 Correct 7 ms 12108 KB Output is correct
11 Correct 7 ms 12064 KB Output is correct
12 Correct 7 ms 12068 KB Output is correct
13 Correct 7 ms 12044 KB Output is correct
14 Correct 7 ms 12108 KB Output is correct
15 Correct 7 ms 12108 KB Output is correct
16 Correct 7 ms 12108 KB Output is correct
17 Correct 7 ms 12108 KB Output is correct
18 Correct 7 ms 12108 KB Output is correct
19 Correct 7 ms 12108 KB Output is correct
20 Correct 7 ms 12236 KB Output is correct
21 Correct 7 ms 12008 KB Output is correct
22 Correct 7 ms 12108 KB Output is correct
23 Correct 7 ms 12108 KB Output is correct
24 Correct 7 ms 12124 KB Output is correct
25 Correct 7 ms 12108 KB Output is correct
26 Correct 55 ms 18976 KB Output is correct
27 Correct 83 ms 18652 KB Output is correct
28 Correct 9 ms 12236 KB Output is correct
29 Correct 7 ms 12108 KB Output is correct
30 Correct 7 ms 12108 KB Output is correct
31 Correct 82 ms 18720 KB Output is correct
32 Correct 8 ms 12236 KB Output is correct
33 Correct 111 ms 25784 KB Output is correct
34 Correct 81 ms 18648 KB Output is correct
35 Correct 10 ms 12236 KB Output is correct
36 Correct 85 ms 19512 KB Output is correct
37 Correct 9 ms 12308 KB Output is correct
38 Correct 9 ms 12236 KB Output is correct
39 Correct 58 ms 19004 KB Output is correct
40 Correct 9 ms 12460 KB Output is correct
41 Correct 93 ms 18596 KB Output is correct
42 Correct 98 ms 21008 KB Output is correct
43 Correct 7 ms 12108 KB Output is correct
44 Correct 108 ms 26100 KB Output is correct
45 Correct 93 ms 22392 KB Output is correct
46 Correct 8 ms 12236 KB Output is correct
47 Correct 9 ms 12236 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 58 ms 18996 KB Output is correct
2 Correct 63 ms 20192 KB Output is correct
3 Correct 9 ms 12236 KB Output is correct
4 Correct 9 ms 12236 KB Output is correct
5 Correct 7 ms 12108 KB Output is correct
6 Correct 7 ms 12108 KB Output is correct
7 Correct 9 ms 12296 KB Output is correct
8 Correct 98 ms 19524 KB Output is correct
9 Correct 9 ms 12316 KB Output is correct
10 Correct 106 ms 18848 KB Output is correct
11 Correct 8 ms 12108 KB Output is correct
12 Correct 86 ms 18844 KB Output is correct
13 Correct 99 ms 19420 KB Output is correct
14 Correct 90 ms 19784 KB Output is correct
15 Correct 58 ms 19000 KB Output is correct
16 Correct 9 ms 12284 KB Output is correct
17 Correct 8 ms 12124 KB Output is correct
18 Correct 62 ms 20000 KB Output is correct
19 Correct 103 ms 23748 KB Output is correct
20 Correct 9 ms 12236 KB Output is correct
21 Correct 7 ms 12068 KB Output is correct
22 Correct 61 ms 19684 KB Output is correct
23 Correct 10 ms 12236 KB Output is correct
24 Correct 96 ms 19012 KB Output is correct
25 Correct 98 ms 22532 KB Output is correct
26 Correct 8 ms 12236 KB Output is correct
27 Correct 8 ms 12280 KB Output is correct
28 Correct 9 ms 12204 KB Output is correct
29 Correct 9 ms 12208 KB Output is correct
30 Correct 9 ms 12344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 12108 KB Output is correct
2 Correct 7 ms 12056 KB Output is correct
3 Correct 7 ms 12108 KB Output is correct
4 Correct 7 ms 12104 KB Output is correct
5 Correct 7 ms 12068 KB Output is correct
6 Correct 7 ms 12064 KB Output is correct
7 Correct 7 ms 12108 KB Output is correct
8 Correct 7 ms 12108 KB Output is correct
9 Correct 7 ms 12108 KB Output is correct
10 Correct 7 ms 12108 KB Output is correct
11 Correct 7 ms 12064 KB Output is correct
12 Correct 7 ms 12068 KB Output is correct
13 Correct 7 ms 12044 KB Output is correct
14 Correct 7 ms 12108 KB Output is correct
15 Correct 7 ms 12108 KB Output is correct
16 Correct 7 ms 12108 KB Output is correct
17 Correct 7 ms 12108 KB Output is correct
18 Correct 7 ms 12108 KB Output is correct
19 Correct 7 ms 12108 KB Output is correct
20 Correct 7 ms 12236 KB Output is correct
21 Correct 7 ms 12008 KB Output is correct
22 Correct 7 ms 12108 KB Output is correct
23 Correct 7 ms 12108 KB Output is correct
24 Correct 7 ms 12124 KB Output is correct
25 Correct 7 ms 12108 KB Output is correct
26 Correct 9 ms 12236 KB Output is correct
27 Correct 9 ms 12324 KB Output is correct
28 Correct 9 ms 12440 KB Output is correct
29 Correct 9 ms 12236 KB Output is correct
30 Correct 9 ms 12208 KB Output is correct
31 Correct 7 ms 12072 KB Output is correct
32 Correct 9 ms 12500 KB Output is correct
33 Correct 8 ms 12108 KB Output is correct
34 Correct 9 ms 12220 KB Output is correct
35 Correct 10 ms 12236 KB Output is correct
36 Correct 9 ms 12236 KB Output is correct
37 Correct 9 ms 12324 KB Output is correct
38 Correct 7 ms 12068 KB Output is correct
39 Correct 9 ms 12236 KB Output is correct
40 Correct 8 ms 12236 KB Output is correct
41 Correct 9 ms 12204 KB Output is correct
42 Correct 9 ms 12300 KB Output is correct
43 Correct 9 ms 12464 KB Output is correct
44 Correct 7 ms 12108 KB Output is correct
45 Correct 9 ms 12360 KB Output is correct
46 Correct 9 ms 12236 KB Output is correct
47 Correct 7 ms 12112 KB Output is correct
48 Correct 9 ms 12236 KB Output is correct
49 Correct 9 ms 12336 KB Output is correct
50 Correct 9 ms 12336 KB Output is correct
51 Correct 9 ms 12216 KB Output is correct
52 Correct 9 ms 12336 KB Output is correct
53 Correct 9 ms 12240 KB Output is correct
54 Correct 9 ms 12244 KB Output is correct
55 Correct 7 ms 12108 KB Output is correct
56 Correct 55 ms 18976 KB Output is correct
57 Correct 83 ms 18652 KB Output is correct
58 Correct 9 ms 12236 KB Output is correct
59 Correct 7 ms 12108 KB Output is correct
60 Correct 7 ms 12108 KB Output is correct
61 Correct 82 ms 18720 KB Output is correct
62 Correct 8 ms 12236 KB Output is correct
63 Correct 111 ms 25784 KB Output is correct
64 Correct 81 ms 18648 KB Output is correct
65 Correct 10 ms 12236 KB Output is correct
66 Correct 85 ms 19512 KB Output is correct
67 Correct 9 ms 12308 KB Output is correct
68 Correct 9 ms 12236 KB Output is correct
69 Correct 58 ms 19004 KB Output is correct
70 Correct 9 ms 12460 KB Output is correct
71 Correct 93 ms 18596 KB Output is correct
72 Correct 98 ms 21008 KB Output is correct
73 Correct 7 ms 12108 KB Output is correct
74 Correct 108 ms 26100 KB Output is correct
75 Correct 93 ms 22392 KB Output is correct
76 Correct 8 ms 12236 KB Output is correct
77 Correct 9 ms 12236 KB Output is correct
78 Correct 58 ms 18996 KB Output is correct
79 Correct 63 ms 20192 KB Output is correct
80 Correct 9 ms 12236 KB Output is correct
81 Correct 9 ms 12236 KB Output is correct
82 Correct 7 ms 12108 KB Output is correct
83 Correct 7 ms 12108 KB Output is correct
84 Correct 9 ms 12296 KB Output is correct
85 Correct 98 ms 19524 KB Output is correct
86 Correct 9 ms 12316 KB Output is correct
87 Correct 106 ms 18848 KB Output is correct
88 Correct 8 ms 12108 KB Output is correct
89 Correct 86 ms 18844 KB Output is correct
90 Correct 99 ms 19420 KB Output is correct
91 Correct 90 ms 19784 KB Output is correct
92 Correct 58 ms 19000 KB Output is correct
93 Correct 9 ms 12284 KB Output is correct
94 Correct 8 ms 12124 KB Output is correct
95 Correct 62 ms 20000 KB Output is correct
96 Correct 103 ms 23748 KB Output is correct
97 Correct 9 ms 12236 KB Output is correct
98 Correct 7 ms 12068 KB Output is correct
99 Correct 61 ms 19684 KB Output is correct
100 Correct 10 ms 12236 KB Output is correct
101 Correct 96 ms 19012 KB Output is correct
102 Correct 98 ms 22532 KB Output is correct
103 Correct 8 ms 12236 KB Output is correct
104 Correct 8 ms 12280 KB Output is correct
105 Correct 9 ms 12204 KB Output is correct
106 Correct 9 ms 12208 KB Output is correct
107 Correct 9 ms 12344 KB Output is correct
108 Correct 400 ms 51360 KB Output is correct
109 Correct 830 ms 63632 KB Output is correct
110 Correct 883 ms 74868 KB Output is correct
111 Correct 826 ms 75404 KB Output is correct
112 Correct 775 ms 65636 KB Output is correct
113 Correct 424 ms 53420 KB Output is correct
114 Correct 666 ms 45764 KB Output is correct
115 Correct 705 ms 45748 KB Output is correct
116 Correct 678 ms 48812 KB Output is correct
117 Correct 717 ms 52240 KB Output is correct
118 Correct 658 ms 46312 KB Output is correct
119 Correct 684 ms 52356 KB Output is correct
120 Correct 769 ms 66316 KB Output is correct
121 Correct 727 ms 52476 KB Output is correct
122 Correct 746 ms 50628 KB Output is correct
123 Correct 413 ms 53892 KB Output is correct
124 Correct 794 ms 51632 KB Output is correct