Submission #411394

# Submission time Handle Problem Language Result Execution time Memory
411394 2021-05-25T07:46:37 Z amoo_safar Mergers (JOI19_mergers) C++17
100 / 100
1293 ms 85076 KB
// vaziat meshki-ghermeze !
#include <bits/stdc++.h>

#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x << " : " << x << '\n'

using namespace std;

typedef long long ll;
typedef long double ld;
typedef string str;
typedef pair<ll, ll> pll;

const ll Mod = 1000000007LL;
const int N = 5e5 + 10;
const ll Inf = 2242545357980376863LL;
const ll Log = 30;

int dep[N], par[N], anc[N];
vector<int> G[N];
void DFS(int u, int p, int d){
	dep[u] = d;
	anc[u] = p;
	for(auto adj : G[u])
		if(adj != p)
			DFS(adj, u, d + 1);
}

int Find(int u){
	if(par[u] == u) return u;
	return par[u] = Find(par[u]);
}
void Unite(int u, int v){
	u = Find(u);
	v = Find(v);
	par[u] = v;
}

void Merge(int u, int v){
	// cerr << "!! " << u << ' ' << v << '\n';
	while((u = Find(u)) != (v = Find(v))){
		if(dep[u] < dep[v]) swap(u, v);
		Unite(u, anc[u]);
	}
}
int la[N];

int main(){
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	iota(par, par + N, 0);
	int n, k;
	cin >> n >> k;
	int u, v;
	vector<pll> E;
	for(int i = 1; i < n; i++){
		cin >> u >> v;
		G[u].pb(v);
		G[v].pb(u);
		E.pb({u, v});
	}
	DFS(u, 0, 1);
	int s;
	for(int i = 1; i <= n; i++){
		cin >> s;
		if(la[s]) Merge(la[s], i);
		la[s] = i;
	}
	map<int, int> deg;
	for(auto [u, v] : E)
		if(Find(u) != Find(v))
			deg[Find(u)] ++, deg[Find(v)] ++;
	int cnt = 0;
	for(auto [u, d] : deg) cnt += (d == 1);
	// debug(cnt);
	cout << (cnt + 1) / 2 << '\n';
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 10 ms 14028 KB Output is correct
2 Correct 10 ms 14028 KB Output is correct
3 Correct 8 ms 14028 KB Output is correct
4 Correct 8 ms 13972 KB Output is correct
5 Correct 8 ms 14028 KB Output is correct
6 Correct 8 ms 14028 KB Output is correct
7 Correct 8 ms 14028 KB Output is correct
8 Correct 8 ms 14028 KB Output is correct
9 Correct 8 ms 14028 KB Output is correct
10 Correct 10 ms 14028 KB Output is correct
11 Correct 8 ms 14028 KB Output is correct
12 Correct 8 ms 14028 KB Output is correct
13 Correct 8 ms 14028 KB Output is correct
14 Correct 8 ms 14044 KB Output is correct
15 Correct 8 ms 14028 KB Output is correct
16 Correct 8 ms 14028 KB Output is correct
17 Correct 8 ms 14028 KB Output is correct
18 Correct 8 ms 14028 KB Output is correct
19 Correct 8 ms 14028 KB Output is correct
20 Correct 9 ms 14028 KB Output is correct
21 Correct 8 ms 13928 KB Output is correct
22 Correct 8 ms 14028 KB Output is correct
23 Correct 8 ms 14028 KB Output is correct
24 Correct 8 ms 14028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 14028 KB Output is correct
2 Correct 10 ms 14028 KB Output is correct
3 Correct 8 ms 14028 KB Output is correct
4 Correct 8 ms 13972 KB Output is correct
5 Correct 8 ms 14028 KB Output is correct
6 Correct 8 ms 14028 KB Output is correct
7 Correct 8 ms 14028 KB Output is correct
8 Correct 8 ms 14028 KB Output is correct
9 Correct 8 ms 14028 KB Output is correct
10 Correct 10 ms 14028 KB Output is correct
11 Correct 8 ms 14028 KB Output is correct
12 Correct 8 ms 14028 KB Output is correct
13 Correct 8 ms 14028 KB Output is correct
14 Correct 8 ms 14044 KB Output is correct
15 Correct 8 ms 14028 KB Output is correct
16 Correct 8 ms 14028 KB Output is correct
17 Correct 8 ms 14028 KB Output is correct
18 Correct 8 ms 14028 KB Output is correct
19 Correct 8 ms 14028 KB Output is correct
20 Correct 9 ms 14028 KB Output is correct
21 Correct 8 ms 13928 KB Output is correct
22 Correct 8 ms 14028 KB Output is correct
23 Correct 8 ms 14028 KB Output is correct
24 Correct 8 ms 14028 KB Output is correct
25 Correct 8 ms 14028 KB Output is correct
26 Correct 10 ms 14236 KB Output is correct
27 Correct 9 ms 14156 KB Output is correct
28 Correct 11 ms 14216 KB Output is correct
29 Correct 11 ms 14284 KB Output is correct
30 Correct 10 ms 14156 KB Output is correct
31 Correct 8 ms 13940 KB Output is correct
32 Correct 10 ms 14156 KB Output is correct
33 Correct 9 ms 14020 KB Output is correct
34 Correct 10 ms 14232 KB Output is correct
35 Correct 11 ms 14364 KB Output is correct
36 Correct 10 ms 14156 KB Output is correct
37 Correct 10 ms 14144 KB Output is correct
38 Correct 8 ms 14044 KB Output is correct
39 Correct 10 ms 14196 KB Output is correct
40 Correct 10 ms 14156 KB Output is correct
41 Correct 10 ms 14132 KB Output is correct
42 Correct 10 ms 14156 KB Output is correct
43 Correct 10 ms 14284 KB Output is correct
44 Correct 8 ms 14028 KB Output is correct
45 Correct 10 ms 14156 KB Output is correct
46 Correct 10 ms 14156 KB Output is correct
47 Correct 8 ms 14028 KB Output is correct
48 Correct 10 ms 14156 KB Output is correct
49 Correct 10 ms 14284 KB Output is correct
50 Correct 11 ms 14388 KB Output is correct
51 Correct 10 ms 14156 KB Output is correct
52 Correct 10 ms 14156 KB Output is correct
53 Correct 10 ms 14156 KB Output is correct
54 Correct 10 ms 14156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 14028 KB Output is correct
2 Correct 10 ms 14028 KB Output is correct
3 Correct 8 ms 14028 KB Output is correct
4 Correct 8 ms 13972 KB Output is correct
5 Correct 8 ms 14028 KB Output is correct
6 Correct 8 ms 14028 KB Output is correct
7 Correct 8 ms 14028 KB Output is correct
8 Correct 8 ms 14028 KB Output is correct
9 Correct 8 ms 14028 KB Output is correct
10 Correct 10 ms 14028 KB Output is correct
11 Correct 8 ms 14028 KB Output is correct
12 Correct 8 ms 14028 KB Output is correct
13 Correct 8 ms 14028 KB Output is correct
14 Correct 8 ms 14044 KB Output is correct
15 Correct 8 ms 14028 KB Output is correct
16 Correct 8 ms 14028 KB Output is correct
17 Correct 8 ms 14028 KB Output is correct
18 Correct 8 ms 14028 KB Output is correct
19 Correct 8 ms 14028 KB Output is correct
20 Correct 9 ms 14028 KB Output is correct
21 Correct 8 ms 13928 KB Output is correct
22 Correct 8 ms 14028 KB Output is correct
23 Correct 8 ms 14028 KB Output is correct
24 Correct 8 ms 14028 KB Output is correct
25 Correct 8 ms 14028 KB Output is correct
26 Correct 54 ms 19892 KB Output is correct
27 Correct 71 ms 19572 KB Output is correct
28 Correct 10 ms 14156 KB Output is correct
29 Correct 9 ms 14028 KB Output is correct
30 Correct 8 ms 13936 KB Output is correct
31 Correct 82 ms 19580 KB Output is correct
32 Correct 11 ms 14156 KB Output is correct
33 Correct 80 ms 21080 KB Output is correct
34 Correct 70 ms 19568 KB Output is correct
35 Correct 12 ms 14156 KB Output is correct
36 Correct 71 ms 19644 KB Output is correct
37 Correct 11 ms 14156 KB Output is correct
38 Correct 10 ms 14156 KB Output is correct
39 Correct 57 ms 19856 KB Output is correct
40 Correct 10 ms 14284 KB Output is correct
41 Correct 69 ms 19516 KB Output is correct
42 Correct 74 ms 20736 KB Output is correct
43 Correct 9 ms 14028 KB Output is correct
44 Correct 72 ms 21288 KB Output is correct
45 Correct 76 ms 20780 KB Output is correct
46 Correct 10 ms 14156 KB Output is correct
47 Correct 10 ms 14136 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 55 ms 19904 KB Output is correct
2 Correct 103 ms 24876 KB Output is correct
3 Correct 11 ms 14284 KB Output is correct
4 Correct 10 ms 14156 KB Output is correct
5 Correct 9 ms 14032 KB Output is correct
6 Correct 8 ms 14028 KB Output is correct
7 Correct 10 ms 14212 KB Output is correct
8 Correct 77 ms 20272 KB Output is correct
9 Correct 10 ms 14196 KB Output is correct
10 Correct 72 ms 19632 KB Output is correct
11 Correct 9 ms 14028 KB Output is correct
12 Correct 75 ms 19532 KB Output is correct
13 Correct 84 ms 20600 KB Output is correct
14 Correct 149 ms 24696 KB Output is correct
15 Correct 57 ms 19908 KB Output is correct
16 Correct 10 ms 14156 KB Output is correct
17 Correct 9 ms 14028 KB Output is correct
18 Correct 100 ms 24052 KB Output is correct
19 Correct 133 ms 26032 KB Output is correct
20 Correct 10 ms 14216 KB Output is correct
21 Correct 9 ms 14028 KB Output is correct
22 Correct 67 ms 20864 KB Output is correct
23 Correct 10 ms 14156 KB Output is correct
24 Correct 84 ms 19976 KB Output is correct
25 Correct 141 ms 25192 KB Output is correct
26 Correct 10 ms 14284 KB Output is correct
27 Correct 11 ms 14296 KB Output is correct
28 Correct 10 ms 14200 KB Output is correct
29 Correct 10 ms 14156 KB Output is correct
30 Correct 10 ms 14156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 14028 KB Output is correct
2 Correct 10 ms 14028 KB Output is correct
3 Correct 8 ms 14028 KB Output is correct
4 Correct 8 ms 13972 KB Output is correct
5 Correct 8 ms 14028 KB Output is correct
6 Correct 8 ms 14028 KB Output is correct
7 Correct 8 ms 14028 KB Output is correct
8 Correct 8 ms 14028 KB Output is correct
9 Correct 8 ms 14028 KB Output is correct
10 Correct 10 ms 14028 KB Output is correct
11 Correct 8 ms 14028 KB Output is correct
12 Correct 8 ms 14028 KB Output is correct
13 Correct 8 ms 14028 KB Output is correct
14 Correct 8 ms 14044 KB Output is correct
15 Correct 8 ms 14028 KB Output is correct
16 Correct 8 ms 14028 KB Output is correct
17 Correct 8 ms 14028 KB Output is correct
18 Correct 8 ms 14028 KB Output is correct
19 Correct 8 ms 14028 KB Output is correct
20 Correct 9 ms 14028 KB Output is correct
21 Correct 8 ms 13928 KB Output is correct
22 Correct 8 ms 14028 KB Output is correct
23 Correct 8 ms 14028 KB Output is correct
24 Correct 8 ms 14028 KB Output is correct
25 Correct 8 ms 14028 KB Output is correct
26 Correct 10 ms 14236 KB Output is correct
27 Correct 9 ms 14156 KB Output is correct
28 Correct 11 ms 14216 KB Output is correct
29 Correct 11 ms 14284 KB Output is correct
30 Correct 10 ms 14156 KB Output is correct
31 Correct 8 ms 13940 KB Output is correct
32 Correct 10 ms 14156 KB Output is correct
33 Correct 9 ms 14020 KB Output is correct
34 Correct 10 ms 14232 KB Output is correct
35 Correct 11 ms 14364 KB Output is correct
36 Correct 10 ms 14156 KB Output is correct
37 Correct 10 ms 14144 KB Output is correct
38 Correct 8 ms 14044 KB Output is correct
39 Correct 10 ms 14196 KB Output is correct
40 Correct 10 ms 14156 KB Output is correct
41 Correct 10 ms 14132 KB Output is correct
42 Correct 10 ms 14156 KB Output is correct
43 Correct 10 ms 14284 KB Output is correct
44 Correct 8 ms 14028 KB Output is correct
45 Correct 10 ms 14156 KB Output is correct
46 Correct 10 ms 14156 KB Output is correct
47 Correct 8 ms 14028 KB Output is correct
48 Correct 10 ms 14156 KB Output is correct
49 Correct 10 ms 14284 KB Output is correct
50 Correct 11 ms 14388 KB Output is correct
51 Correct 10 ms 14156 KB Output is correct
52 Correct 10 ms 14156 KB Output is correct
53 Correct 10 ms 14156 KB Output is correct
54 Correct 10 ms 14156 KB Output is correct
55 Correct 8 ms 14028 KB Output is correct
56 Correct 54 ms 19892 KB Output is correct
57 Correct 71 ms 19572 KB Output is correct
58 Correct 10 ms 14156 KB Output is correct
59 Correct 9 ms 14028 KB Output is correct
60 Correct 8 ms 13936 KB Output is correct
61 Correct 82 ms 19580 KB Output is correct
62 Correct 11 ms 14156 KB Output is correct
63 Correct 80 ms 21080 KB Output is correct
64 Correct 70 ms 19568 KB Output is correct
65 Correct 12 ms 14156 KB Output is correct
66 Correct 71 ms 19644 KB Output is correct
67 Correct 11 ms 14156 KB Output is correct
68 Correct 10 ms 14156 KB Output is correct
69 Correct 57 ms 19856 KB Output is correct
70 Correct 10 ms 14284 KB Output is correct
71 Correct 69 ms 19516 KB Output is correct
72 Correct 74 ms 20736 KB Output is correct
73 Correct 9 ms 14028 KB Output is correct
74 Correct 72 ms 21288 KB Output is correct
75 Correct 76 ms 20780 KB Output is correct
76 Correct 10 ms 14156 KB Output is correct
77 Correct 10 ms 14136 KB Output is correct
78 Correct 55 ms 19904 KB Output is correct
79 Correct 103 ms 24876 KB Output is correct
80 Correct 11 ms 14284 KB Output is correct
81 Correct 10 ms 14156 KB Output is correct
82 Correct 9 ms 14032 KB Output is correct
83 Correct 8 ms 14028 KB Output is correct
84 Correct 10 ms 14212 KB Output is correct
85 Correct 77 ms 20272 KB Output is correct
86 Correct 10 ms 14196 KB Output is correct
87 Correct 72 ms 19632 KB Output is correct
88 Correct 9 ms 14028 KB Output is correct
89 Correct 75 ms 19532 KB Output is correct
90 Correct 84 ms 20600 KB Output is correct
91 Correct 149 ms 24696 KB Output is correct
92 Correct 57 ms 19908 KB Output is correct
93 Correct 10 ms 14156 KB Output is correct
94 Correct 9 ms 14028 KB Output is correct
95 Correct 100 ms 24052 KB Output is correct
96 Correct 133 ms 26032 KB Output is correct
97 Correct 10 ms 14216 KB Output is correct
98 Correct 9 ms 14028 KB Output is correct
99 Correct 67 ms 20864 KB Output is correct
100 Correct 10 ms 14156 KB Output is correct
101 Correct 84 ms 19976 KB Output is correct
102 Correct 141 ms 25192 KB Output is correct
103 Correct 10 ms 14284 KB Output is correct
104 Correct 11 ms 14296 KB Output is correct
105 Correct 10 ms 14200 KB Output is correct
106 Correct 10 ms 14156 KB Output is correct
107 Correct 10 ms 14156 KB Output is correct
108 Correct 406 ms 56060 KB Output is correct
109 Correct 562 ms 55012 KB Output is correct
110 Correct 587 ms 60740 KB Output is correct
111 Correct 1180 ms 85076 KB Output is correct
112 Correct 756 ms 70328 KB Output is correct
113 Correct 792 ms 74220 KB Output is correct
114 Correct 524 ms 49616 KB Output is correct
115 Correct 516 ms 49508 KB Output is correct
116 Correct 567 ms 51848 KB Output is correct
117 Correct 1293 ms 77300 KB Output is correct
118 Correct 500 ms 49740 KB Output is correct
119 Correct 1269 ms 77360 KB Output is correct
120 Correct 1287 ms 80584 KB Output is correct
121 Correct 1268 ms 77152 KB Output is correct
122 Correct 612 ms 57156 KB Output is correct
123 Correct 943 ms 78780 KB Output is correct
124 Correct 997 ms 67656 KB Output is correct