Submission #411393

# Submission time Handle Problem Language Result Execution time Memory
411393 2021-05-25T07:45:21 Z amoo_safar Mergers (JOI19_mergers) C++17
70 / 100
155 ms 19580 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 = 2e5 + 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 4 ms 5708 KB Output is correct
2 Correct 4 ms 5708 KB Output is correct
3 Correct 5 ms 5808 KB Output is correct
4 Correct 4 ms 5816 KB Output is correct
5 Correct 4 ms 5708 KB Output is correct
6 Correct 4 ms 5708 KB Output is correct
7 Correct 4 ms 5708 KB Output is correct
8 Correct 4 ms 5708 KB Output is correct
9 Correct 4 ms 5708 KB Output is correct
10 Correct 4 ms 5708 KB Output is correct
11 Correct 4 ms 5708 KB Output is correct
12 Correct 4 ms 5708 KB Output is correct
13 Correct 4 ms 5804 KB Output is correct
14 Correct 4 ms 5716 KB Output is correct
15 Correct 4 ms 5708 KB Output is correct
16 Correct 4 ms 5740 KB Output is correct
17 Correct 4 ms 5804 KB Output is correct
18 Correct 4 ms 5812 KB Output is correct
19 Correct 4 ms 5708 KB Output is correct
20 Correct 5 ms 5708 KB Output is correct
21 Correct 4 ms 5836 KB Output is correct
22 Correct 4 ms 5816 KB Output is correct
23 Correct 4 ms 5808 KB Output is correct
24 Correct 4 ms 5708 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5708 KB Output is correct
2 Correct 4 ms 5708 KB Output is correct
3 Correct 5 ms 5808 KB Output is correct
4 Correct 4 ms 5816 KB Output is correct
5 Correct 4 ms 5708 KB Output is correct
6 Correct 4 ms 5708 KB Output is correct
7 Correct 4 ms 5708 KB Output is correct
8 Correct 4 ms 5708 KB Output is correct
9 Correct 4 ms 5708 KB Output is correct
10 Correct 4 ms 5708 KB Output is correct
11 Correct 4 ms 5708 KB Output is correct
12 Correct 4 ms 5708 KB Output is correct
13 Correct 4 ms 5804 KB Output is correct
14 Correct 4 ms 5716 KB Output is correct
15 Correct 4 ms 5708 KB Output is correct
16 Correct 4 ms 5740 KB Output is correct
17 Correct 4 ms 5804 KB Output is correct
18 Correct 4 ms 5812 KB Output is correct
19 Correct 4 ms 5708 KB Output is correct
20 Correct 5 ms 5708 KB Output is correct
21 Correct 4 ms 5836 KB Output is correct
22 Correct 4 ms 5816 KB Output is correct
23 Correct 4 ms 5808 KB Output is correct
24 Correct 4 ms 5708 KB Output is correct
25 Correct 4 ms 5812 KB Output is correct
26 Correct 6 ms 6072 KB Output is correct
27 Correct 5 ms 5952 KB Output is correct
28 Correct 6 ms 6092 KB Output is correct
29 Correct 7 ms 6092 KB Output is correct
30 Correct 5 ms 5964 KB Output is correct
31 Correct 4 ms 5708 KB Output is correct
32 Correct 6 ms 6092 KB Output is correct
33 Correct 5 ms 5708 KB Output is correct
34 Correct 6 ms 5964 KB Output is correct
35 Correct 8 ms 6092 KB Output is correct
36 Correct 6 ms 5964 KB Output is correct
37 Correct 7 ms 5964 KB Output is correct
38 Correct 4 ms 5708 KB Output is correct
39 Correct 6 ms 5964 KB Output is correct
40 Correct 5 ms 5952 KB Output is correct
41 Correct 6 ms 5952 KB Output is correct
42 Correct 5 ms 5964 KB Output is correct
43 Correct 6 ms 6092 KB Output is correct
44 Correct 4 ms 5820 KB Output is correct
45 Correct 5 ms 5964 KB Output is correct
46 Correct 6 ms 5952 KB Output is correct
47 Correct 4 ms 5708 KB Output is correct
48 Correct 6 ms 5936 KB Output is correct
49 Correct 7 ms 6076 KB Output is correct
50 Correct 7 ms 6120 KB Output is correct
51 Correct 7 ms 5964 KB Output is correct
52 Correct 7 ms 5952 KB Output is correct
53 Correct 5 ms 5964 KB Output is correct
54 Correct 7 ms 5964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5708 KB Output is correct
2 Correct 4 ms 5708 KB Output is correct
3 Correct 5 ms 5808 KB Output is correct
4 Correct 4 ms 5816 KB Output is correct
5 Correct 4 ms 5708 KB Output is correct
6 Correct 4 ms 5708 KB Output is correct
7 Correct 4 ms 5708 KB Output is correct
8 Correct 4 ms 5708 KB Output is correct
9 Correct 4 ms 5708 KB Output is correct
10 Correct 4 ms 5708 KB Output is correct
11 Correct 4 ms 5708 KB Output is correct
12 Correct 4 ms 5708 KB Output is correct
13 Correct 4 ms 5804 KB Output is correct
14 Correct 4 ms 5716 KB Output is correct
15 Correct 4 ms 5708 KB Output is correct
16 Correct 4 ms 5740 KB Output is correct
17 Correct 4 ms 5804 KB Output is correct
18 Correct 4 ms 5812 KB Output is correct
19 Correct 4 ms 5708 KB Output is correct
20 Correct 5 ms 5708 KB Output is correct
21 Correct 4 ms 5836 KB Output is correct
22 Correct 4 ms 5816 KB Output is correct
23 Correct 4 ms 5808 KB Output is correct
24 Correct 4 ms 5708 KB Output is correct
25 Correct 4 ms 5708 KB Output is correct
26 Correct 53 ms 13100 KB Output is correct
27 Correct 77 ms 12808 KB Output is correct
28 Correct 5 ms 5964 KB Output is correct
29 Correct 4 ms 5812 KB Output is correct
30 Correct 4 ms 5708 KB Output is correct
31 Correct 74 ms 12888 KB Output is correct
32 Correct 5 ms 5964 KB Output is correct
33 Correct 78 ms 14200 KB Output is correct
34 Correct 66 ms 12788 KB Output is correct
35 Correct 5 ms 5964 KB Output is correct
36 Correct 79 ms 12896 KB Output is correct
37 Correct 6 ms 5964 KB Output is correct
38 Correct 6 ms 5932 KB Output is correct
39 Correct 61 ms 13060 KB Output is correct
40 Correct 5 ms 6100 KB Output is correct
41 Correct 69 ms 12724 KB Output is correct
42 Correct 69 ms 13960 KB Output is correct
43 Correct 4 ms 5708 KB Output is correct
44 Correct 79 ms 14420 KB Output is correct
45 Correct 67 ms 13928 KB Output is correct
46 Correct 5 ms 5956 KB Output is correct
47 Correct 5 ms 5952 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 56 ms 13000 KB Output is correct
2 Correct 111 ms 18552 KB Output is correct
3 Correct 6 ms 6092 KB Output is correct
4 Correct 5 ms 5964 KB Output is correct
5 Correct 4 ms 5708 KB Output is correct
6 Correct 4 ms 5808 KB Output is correct
7 Correct 5 ms 5964 KB Output is correct
8 Correct 83 ms 13684 KB Output is correct
9 Correct 6 ms 5924 KB Output is correct
10 Correct 74 ms 12904 KB Output is correct
11 Correct 4 ms 5708 KB Output is correct
12 Correct 92 ms 12840 KB Output is correct
13 Correct 83 ms 14156 KB Output is correct
14 Correct 128 ms 18120 KB Output is correct
15 Correct 54 ms 12976 KB Output is correct
16 Correct 7 ms 5948 KB Output is correct
17 Correct 4 ms 5708 KB Output is correct
18 Correct 100 ms 17472 KB Output is correct
19 Correct 138 ms 19580 KB Output is correct
20 Correct 6 ms 5952 KB Output is correct
21 Correct 4 ms 5708 KB Output is correct
22 Correct 63 ms 14312 KB Output is correct
23 Correct 6 ms 5964 KB Output is correct
24 Correct 76 ms 13344 KB Output is correct
25 Correct 155 ms 18684 KB Output is correct
26 Correct 6 ms 6092 KB Output is correct
27 Correct 6 ms 6216 KB Output is correct
28 Correct 5 ms 5964 KB Output is correct
29 Correct 5 ms 5964 KB Output is correct
30 Correct 5 ms 5952 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5708 KB Output is correct
2 Correct 4 ms 5708 KB Output is correct
3 Correct 5 ms 5808 KB Output is correct
4 Correct 4 ms 5816 KB Output is correct
5 Correct 4 ms 5708 KB Output is correct
6 Correct 4 ms 5708 KB Output is correct
7 Correct 4 ms 5708 KB Output is correct
8 Correct 4 ms 5708 KB Output is correct
9 Correct 4 ms 5708 KB Output is correct
10 Correct 4 ms 5708 KB Output is correct
11 Correct 4 ms 5708 KB Output is correct
12 Correct 4 ms 5708 KB Output is correct
13 Correct 4 ms 5804 KB Output is correct
14 Correct 4 ms 5716 KB Output is correct
15 Correct 4 ms 5708 KB Output is correct
16 Correct 4 ms 5740 KB Output is correct
17 Correct 4 ms 5804 KB Output is correct
18 Correct 4 ms 5812 KB Output is correct
19 Correct 4 ms 5708 KB Output is correct
20 Correct 5 ms 5708 KB Output is correct
21 Correct 4 ms 5836 KB Output is correct
22 Correct 4 ms 5816 KB Output is correct
23 Correct 4 ms 5808 KB Output is correct
24 Correct 4 ms 5708 KB Output is correct
25 Correct 4 ms 5812 KB Output is correct
26 Correct 6 ms 6072 KB Output is correct
27 Correct 5 ms 5952 KB Output is correct
28 Correct 6 ms 6092 KB Output is correct
29 Correct 7 ms 6092 KB Output is correct
30 Correct 5 ms 5964 KB Output is correct
31 Correct 4 ms 5708 KB Output is correct
32 Correct 6 ms 6092 KB Output is correct
33 Correct 5 ms 5708 KB Output is correct
34 Correct 6 ms 5964 KB Output is correct
35 Correct 8 ms 6092 KB Output is correct
36 Correct 6 ms 5964 KB Output is correct
37 Correct 7 ms 5964 KB Output is correct
38 Correct 4 ms 5708 KB Output is correct
39 Correct 6 ms 5964 KB Output is correct
40 Correct 5 ms 5952 KB Output is correct
41 Correct 6 ms 5952 KB Output is correct
42 Correct 5 ms 5964 KB Output is correct
43 Correct 6 ms 6092 KB Output is correct
44 Correct 4 ms 5820 KB Output is correct
45 Correct 5 ms 5964 KB Output is correct
46 Correct 6 ms 5952 KB Output is correct
47 Correct 4 ms 5708 KB Output is correct
48 Correct 6 ms 5936 KB Output is correct
49 Correct 7 ms 6076 KB Output is correct
50 Correct 7 ms 6120 KB Output is correct
51 Correct 7 ms 5964 KB Output is correct
52 Correct 7 ms 5952 KB Output is correct
53 Correct 5 ms 5964 KB Output is correct
54 Correct 7 ms 5964 KB Output is correct
55 Correct 4 ms 5708 KB Output is correct
56 Correct 53 ms 13100 KB Output is correct
57 Correct 77 ms 12808 KB Output is correct
58 Correct 5 ms 5964 KB Output is correct
59 Correct 4 ms 5812 KB Output is correct
60 Correct 4 ms 5708 KB Output is correct
61 Correct 74 ms 12888 KB Output is correct
62 Correct 5 ms 5964 KB Output is correct
63 Correct 78 ms 14200 KB Output is correct
64 Correct 66 ms 12788 KB Output is correct
65 Correct 5 ms 5964 KB Output is correct
66 Correct 79 ms 12896 KB Output is correct
67 Correct 6 ms 5964 KB Output is correct
68 Correct 6 ms 5932 KB Output is correct
69 Correct 61 ms 13060 KB Output is correct
70 Correct 5 ms 6100 KB Output is correct
71 Correct 69 ms 12724 KB Output is correct
72 Correct 69 ms 13960 KB Output is correct
73 Correct 4 ms 5708 KB Output is correct
74 Correct 79 ms 14420 KB Output is correct
75 Correct 67 ms 13928 KB Output is correct
76 Correct 5 ms 5956 KB Output is correct
77 Correct 5 ms 5952 KB Output is correct
78 Correct 56 ms 13000 KB Output is correct
79 Correct 111 ms 18552 KB Output is correct
80 Correct 6 ms 6092 KB Output is correct
81 Correct 5 ms 5964 KB Output is correct
82 Correct 4 ms 5708 KB Output is correct
83 Correct 4 ms 5808 KB Output is correct
84 Correct 5 ms 5964 KB Output is correct
85 Correct 83 ms 13684 KB Output is correct
86 Correct 6 ms 5924 KB Output is correct
87 Correct 74 ms 12904 KB Output is correct
88 Correct 4 ms 5708 KB Output is correct
89 Correct 92 ms 12840 KB Output is correct
90 Correct 83 ms 14156 KB Output is correct
91 Correct 128 ms 18120 KB Output is correct
92 Correct 54 ms 12976 KB Output is correct
93 Correct 7 ms 5948 KB Output is correct
94 Correct 4 ms 5708 KB Output is correct
95 Correct 100 ms 17472 KB Output is correct
96 Correct 138 ms 19580 KB Output is correct
97 Correct 6 ms 5952 KB Output is correct
98 Correct 4 ms 5708 KB Output is correct
99 Correct 63 ms 14312 KB Output is correct
100 Correct 6 ms 5964 KB Output is correct
101 Correct 76 ms 13344 KB Output is correct
102 Correct 155 ms 18684 KB Output is correct
103 Correct 6 ms 6092 KB Output is correct
104 Correct 6 ms 6216 KB Output is correct
105 Correct 5 ms 5964 KB Output is correct
106 Correct 5 ms 5964 KB Output is correct
107 Correct 5 ms 5952 KB Output is correct
108 Runtime error 10 ms 11500 KB Execution killed with signal 11
109 Halted 0 ms 0 KB -