// Knapsack DP is harder than FFT.
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii; typedef pair<ll,ll> pll;
#define ff first
#define ss second
#define pb emplace_back
#define FOR(i,n) for(int i = 0; i < (n); ++i)
#define FOO(i,a,b) for(int i = (a); i <= (b); ++i)
#define AI(x) begin(x),end(x)
template<class I> bool chmax(I &a, I b){ return a < b ? (a = b, true) : false;}
template<class I> bool chmin(I &a, I b){ return a > b ? (a = b, true) : false;}
#ifdef OWO
#define debug(args...) LKJ("[ " + string(#args) + " ]", args)
void LKJ(){ cerr<<endl;}
template<class I, class...T> void LKJ(I&&x, T&&...t){ cerr<<x<<", ", LKJ(t...);}
template<class I> void DE(I a, I b){ while(a < b) cerr<<*a<<" \n"[next(a) == b], ++a;}
#else
#define debug(...) 0
#define DE(...) 0
#endif
const int N = 5e5 + 225;
int n, k;
vector<pii> adj[N];
int lst[N];
int low[N], pre[N], tot;
void tarjan(int u, int p){
low[u] = pre[u] = ++tot;
int cnt = -1;
for(int i = 0; i < adj[u].size(); ++i){
auto &[v, e] = adj[u][i];
if(v == p and cnt == -1){ cnt = i; continue;}
if(pre[v] == 0){
tarjan(v, u);
chmin(low[u], low[v]);
if(low[v] == pre[v])
e = 1;
}
else if(pre[v] < pre[u])
chmin(low[u], pre[v]);
}
if(low[u] == pre[u] && cnt != -1)
adj[u][cnt].ss = 1;
}
int mp[N];
void dfs(int u, int id){
mp[u] = id;
for(auto [v, e]: adj[u]){
if(e) continue;
if(mp[v]) continue;
dfs(v, id);
}
}
int bccnt = 0;
void uni(){
FOO(i,1,n){
if(mp[i] == 0){
dfs(i, ++bccnt);
}
}
}
vector<int> G[N];
void mke(){
FOO(i,1,n){
for(auto [v, e]: adj[i])
if(e){
G[mp[i]].pb(mp[v]);
}
}
}
int leaf(int u, int p){
int ans = 0;
for(int v: G[u])
if(v != p)
ans += leaf(v, u);
if(G[u].size() <= 1) ++ans;
return ans;
}
int32_t main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> n >> k;
for(int a, b, i = 1; i < n; ++i){
cin >> a >> b;
adj[a].pb(b, 0);
adj[b].pb(a, 0);
}
for(int i = 1; i <= n; ++i){
int c; cin >> c;
if(lst[c] != 0){
adj[i].pb(lst[c], 0);
adj[lst[c]].pb(i, 0);
}
lst[c] = i;
}
tarjan(1, -1);
uni();
if(bccnt == 1){
cout << 0;
return 0;
}
mke();
int ans = leaf(1, -1);
cout << (ans + 1) / 2;
return 0;
}
Compilation message
mergers.cpp: In function 'void tarjan(int, int)':
mergers.cpp:32:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
32 | for(int i = 0; i < adj[u].size(); ++i){
| ~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
23916 KB |
Output is correct |
2 |
Correct |
20 ms |
23916 KB |
Output is correct |
3 |
Correct |
17 ms |
23916 KB |
Output is correct |
4 |
Correct |
16 ms |
23916 KB |
Output is correct |
5 |
Correct |
17 ms |
23916 KB |
Output is correct |
6 |
Correct |
16 ms |
23916 KB |
Output is correct |
7 |
Correct |
16 ms |
23916 KB |
Output is correct |
8 |
Correct |
16 ms |
23916 KB |
Output is correct |
9 |
Correct |
17 ms |
23916 KB |
Output is correct |
10 |
Correct |
17 ms |
23916 KB |
Output is correct |
11 |
Correct |
18 ms |
23916 KB |
Output is correct |
12 |
Correct |
16 ms |
23916 KB |
Output is correct |
13 |
Correct |
17 ms |
23916 KB |
Output is correct |
14 |
Correct |
17 ms |
23916 KB |
Output is correct |
15 |
Correct |
17 ms |
23916 KB |
Output is correct |
16 |
Correct |
17 ms |
23916 KB |
Output is correct |
17 |
Correct |
16 ms |
23916 KB |
Output is correct |
18 |
Correct |
17 ms |
23916 KB |
Output is correct |
19 |
Correct |
17 ms |
23916 KB |
Output is correct |
20 |
Correct |
16 ms |
23916 KB |
Output is correct |
21 |
Correct |
17 ms |
23916 KB |
Output is correct |
22 |
Correct |
17 ms |
23916 KB |
Output is correct |
23 |
Correct |
17 ms |
23916 KB |
Output is correct |
24 |
Correct |
16 ms |
23916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
23916 KB |
Output is correct |
2 |
Correct |
20 ms |
23916 KB |
Output is correct |
3 |
Correct |
17 ms |
23916 KB |
Output is correct |
4 |
Correct |
16 ms |
23916 KB |
Output is correct |
5 |
Correct |
17 ms |
23916 KB |
Output is correct |
6 |
Correct |
16 ms |
23916 KB |
Output is correct |
7 |
Correct |
16 ms |
23916 KB |
Output is correct |
8 |
Correct |
16 ms |
23916 KB |
Output is correct |
9 |
Correct |
17 ms |
23916 KB |
Output is correct |
10 |
Correct |
17 ms |
23916 KB |
Output is correct |
11 |
Correct |
18 ms |
23916 KB |
Output is correct |
12 |
Correct |
16 ms |
23916 KB |
Output is correct |
13 |
Correct |
17 ms |
23916 KB |
Output is correct |
14 |
Correct |
17 ms |
23916 KB |
Output is correct |
15 |
Correct |
17 ms |
23916 KB |
Output is correct |
16 |
Correct |
17 ms |
23916 KB |
Output is correct |
17 |
Correct |
16 ms |
23916 KB |
Output is correct |
18 |
Correct |
17 ms |
23916 KB |
Output is correct |
19 |
Correct |
17 ms |
23916 KB |
Output is correct |
20 |
Correct |
16 ms |
23916 KB |
Output is correct |
21 |
Correct |
17 ms |
23916 KB |
Output is correct |
22 |
Correct |
17 ms |
23916 KB |
Output is correct |
23 |
Correct |
17 ms |
23916 KB |
Output is correct |
24 |
Correct |
16 ms |
23916 KB |
Output is correct |
25 |
Correct |
16 ms |
23916 KB |
Output is correct |
26 |
Correct |
19 ms |
24044 KB |
Output is correct |
27 |
Correct |
18 ms |
24172 KB |
Output is correct |
28 |
Correct |
20 ms |
24172 KB |
Output is correct |
29 |
Correct |
22 ms |
24172 KB |
Output is correct |
30 |
Correct |
19 ms |
24300 KB |
Output is correct |
31 |
Correct |
16 ms |
23916 KB |
Output is correct |
32 |
Correct |
18 ms |
24172 KB |
Output is correct |
33 |
Correct |
16 ms |
23916 KB |
Output is correct |
34 |
Correct |
19 ms |
24172 KB |
Output is correct |
35 |
Correct |
19 ms |
24172 KB |
Output is correct |
36 |
Correct |
18 ms |
24172 KB |
Output is correct |
37 |
Correct |
18 ms |
24044 KB |
Output is correct |
38 |
Correct |
19 ms |
23916 KB |
Output is correct |
39 |
Correct |
19 ms |
24172 KB |
Output is correct |
40 |
Correct |
21 ms |
24300 KB |
Output is correct |
41 |
Correct |
18 ms |
24172 KB |
Output is correct |
42 |
Correct |
18 ms |
24044 KB |
Output is correct |
43 |
Correct |
18 ms |
24300 KB |
Output is correct |
44 |
Correct |
17 ms |
23792 KB |
Output is correct |
45 |
Correct |
18 ms |
24044 KB |
Output is correct |
46 |
Correct |
18 ms |
24044 KB |
Output is correct |
47 |
Correct |
16 ms |
23916 KB |
Output is correct |
48 |
Correct |
19 ms |
24320 KB |
Output is correct |
49 |
Correct |
18 ms |
24172 KB |
Output is correct |
50 |
Correct |
19 ms |
24172 KB |
Output is correct |
51 |
Correct |
18 ms |
24172 KB |
Output is correct |
52 |
Correct |
18 ms |
24172 KB |
Output is correct |
53 |
Correct |
18 ms |
24044 KB |
Output is correct |
54 |
Correct |
18 ms |
24300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
23916 KB |
Output is correct |
2 |
Correct |
20 ms |
23916 KB |
Output is correct |
3 |
Correct |
17 ms |
23916 KB |
Output is correct |
4 |
Correct |
16 ms |
23916 KB |
Output is correct |
5 |
Correct |
17 ms |
23916 KB |
Output is correct |
6 |
Correct |
16 ms |
23916 KB |
Output is correct |
7 |
Correct |
16 ms |
23916 KB |
Output is correct |
8 |
Correct |
16 ms |
23916 KB |
Output is correct |
9 |
Correct |
17 ms |
23916 KB |
Output is correct |
10 |
Correct |
17 ms |
23916 KB |
Output is correct |
11 |
Correct |
18 ms |
23916 KB |
Output is correct |
12 |
Correct |
16 ms |
23916 KB |
Output is correct |
13 |
Correct |
17 ms |
23916 KB |
Output is correct |
14 |
Correct |
17 ms |
23916 KB |
Output is correct |
15 |
Correct |
17 ms |
23916 KB |
Output is correct |
16 |
Correct |
17 ms |
23916 KB |
Output is correct |
17 |
Correct |
16 ms |
23916 KB |
Output is correct |
18 |
Correct |
17 ms |
23916 KB |
Output is correct |
19 |
Correct |
17 ms |
23916 KB |
Output is correct |
20 |
Correct |
16 ms |
23916 KB |
Output is correct |
21 |
Correct |
17 ms |
23916 KB |
Output is correct |
22 |
Correct |
17 ms |
23916 KB |
Output is correct |
23 |
Correct |
17 ms |
23916 KB |
Output is correct |
24 |
Correct |
16 ms |
23916 KB |
Output is correct |
25 |
Correct |
17 ms |
23916 KB |
Output is correct |
26 |
Correct |
109 ms |
31584 KB |
Output is correct |
27 |
Correct |
132 ms |
36844 KB |
Output is correct |
28 |
Correct |
18 ms |
24172 KB |
Output is correct |
29 |
Correct |
17 ms |
23916 KB |
Output is correct |
30 |
Correct |
17 ms |
23916 KB |
Output is correct |
31 |
Correct |
156 ms |
32364 KB |
Output is correct |
32 |
Correct |
21 ms |
24172 KB |
Output is correct |
33 |
Correct |
131 ms |
38508 KB |
Output is correct |
34 |
Correct |
147 ms |
35808 KB |
Output is correct |
35 |
Correct |
18 ms |
24172 KB |
Output is correct |
36 |
Correct |
131 ms |
31852 KB |
Output is correct |
37 |
Correct |
21 ms |
24300 KB |
Output is correct |
38 |
Correct |
18 ms |
24172 KB |
Output is correct |
39 |
Correct |
114 ms |
31708 KB |
Output is correct |
40 |
Correct |
19 ms |
24300 KB |
Output is correct |
41 |
Correct |
136 ms |
36844 KB |
Output is correct |
42 |
Correct |
138 ms |
38380 KB |
Output is correct |
43 |
Correct |
17 ms |
23916 KB |
Output is correct |
44 |
Correct |
144 ms |
38520 KB |
Output is correct |
45 |
Correct |
138 ms |
37356 KB |
Output is correct |
46 |
Correct |
20 ms |
24172 KB |
Output is correct |
47 |
Correct |
19 ms |
24300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
113 ms |
31604 KB |
Output is correct |
2 |
Correct |
88 ms |
32864 KB |
Output is correct |
3 |
Correct |
18 ms |
24172 KB |
Output is correct |
4 |
Correct |
18 ms |
24172 KB |
Output is correct |
5 |
Correct |
16 ms |
24044 KB |
Output is correct |
6 |
Correct |
17 ms |
23916 KB |
Output is correct |
7 |
Correct |
20 ms |
24172 KB |
Output is correct |
8 |
Correct |
136 ms |
32236 KB |
Output is correct |
9 |
Correct |
18 ms |
24172 KB |
Output is correct |
10 |
Correct |
141 ms |
33280 KB |
Output is correct |
11 |
Correct |
17 ms |
23916 KB |
Output is correct |
12 |
Correct |
144 ms |
32388 KB |
Output is correct |
13 |
Correct |
166 ms |
32236 KB |
Output is correct |
14 |
Correct |
132 ms |
34156 KB |
Output is correct |
15 |
Correct |
120 ms |
33124 KB |
Output is correct |
16 |
Correct |
18 ms |
24044 KB |
Output is correct |
17 |
Correct |
18 ms |
23916 KB |
Output is correct |
18 |
Correct |
91 ms |
33920 KB |
Output is correct |
19 |
Correct |
139 ms |
37484 KB |
Output is correct |
20 |
Correct |
19 ms |
24172 KB |
Output is correct |
21 |
Correct |
19 ms |
23916 KB |
Output is correct |
22 |
Correct |
102 ms |
31832 KB |
Output is correct |
23 |
Correct |
19 ms |
24172 KB |
Output is correct |
24 |
Correct |
148 ms |
32748 KB |
Output is correct |
25 |
Correct |
128 ms |
36972 KB |
Output is correct |
26 |
Correct |
19 ms |
24172 KB |
Output is correct |
27 |
Correct |
18 ms |
24172 KB |
Output is correct |
28 |
Correct |
20 ms |
24172 KB |
Output is correct |
29 |
Correct |
18 ms |
24300 KB |
Output is correct |
30 |
Correct |
18 ms |
24044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
23916 KB |
Output is correct |
2 |
Correct |
20 ms |
23916 KB |
Output is correct |
3 |
Correct |
17 ms |
23916 KB |
Output is correct |
4 |
Correct |
16 ms |
23916 KB |
Output is correct |
5 |
Correct |
17 ms |
23916 KB |
Output is correct |
6 |
Correct |
16 ms |
23916 KB |
Output is correct |
7 |
Correct |
16 ms |
23916 KB |
Output is correct |
8 |
Correct |
16 ms |
23916 KB |
Output is correct |
9 |
Correct |
17 ms |
23916 KB |
Output is correct |
10 |
Correct |
17 ms |
23916 KB |
Output is correct |
11 |
Correct |
18 ms |
23916 KB |
Output is correct |
12 |
Correct |
16 ms |
23916 KB |
Output is correct |
13 |
Correct |
17 ms |
23916 KB |
Output is correct |
14 |
Correct |
17 ms |
23916 KB |
Output is correct |
15 |
Correct |
17 ms |
23916 KB |
Output is correct |
16 |
Correct |
17 ms |
23916 KB |
Output is correct |
17 |
Correct |
16 ms |
23916 KB |
Output is correct |
18 |
Correct |
17 ms |
23916 KB |
Output is correct |
19 |
Correct |
17 ms |
23916 KB |
Output is correct |
20 |
Correct |
16 ms |
23916 KB |
Output is correct |
21 |
Correct |
17 ms |
23916 KB |
Output is correct |
22 |
Correct |
17 ms |
23916 KB |
Output is correct |
23 |
Correct |
17 ms |
23916 KB |
Output is correct |
24 |
Correct |
16 ms |
23916 KB |
Output is correct |
25 |
Correct |
16 ms |
23916 KB |
Output is correct |
26 |
Correct |
19 ms |
24044 KB |
Output is correct |
27 |
Correct |
18 ms |
24172 KB |
Output is correct |
28 |
Correct |
20 ms |
24172 KB |
Output is correct |
29 |
Correct |
22 ms |
24172 KB |
Output is correct |
30 |
Correct |
19 ms |
24300 KB |
Output is correct |
31 |
Correct |
16 ms |
23916 KB |
Output is correct |
32 |
Correct |
18 ms |
24172 KB |
Output is correct |
33 |
Correct |
16 ms |
23916 KB |
Output is correct |
34 |
Correct |
19 ms |
24172 KB |
Output is correct |
35 |
Correct |
19 ms |
24172 KB |
Output is correct |
36 |
Correct |
18 ms |
24172 KB |
Output is correct |
37 |
Correct |
18 ms |
24044 KB |
Output is correct |
38 |
Correct |
19 ms |
23916 KB |
Output is correct |
39 |
Correct |
19 ms |
24172 KB |
Output is correct |
40 |
Correct |
21 ms |
24300 KB |
Output is correct |
41 |
Correct |
18 ms |
24172 KB |
Output is correct |
42 |
Correct |
18 ms |
24044 KB |
Output is correct |
43 |
Correct |
18 ms |
24300 KB |
Output is correct |
44 |
Correct |
17 ms |
23792 KB |
Output is correct |
45 |
Correct |
18 ms |
24044 KB |
Output is correct |
46 |
Correct |
18 ms |
24044 KB |
Output is correct |
47 |
Correct |
16 ms |
23916 KB |
Output is correct |
48 |
Correct |
19 ms |
24320 KB |
Output is correct |
49 |
Correct |
18 ms |
24172 KB |
Output is correct |
50 |
Correct |
19 ms |
24172 KB |
Output is correct |
51 |
Correct |
18 ms |
24172 KB |
Output is correct |
52 |
Correct |
18 ms |
24172 KB |
Output is correct |
53 |
Correct |
18 ms |
24044 KB |
Output is correct |
54 |
Correct |
18 ms |
24300 KB |
Output is correct |
55 |
Correct |
17 ms |
23916 KB |
Output is correct |
56 |
Correct |
109 ms |
31584 KB |
Output is correct |
57 |
Correct |
132 ms |
36844 KB |
Output is correct |
58 |
Correct |
18 ms |
24172 KB |
Output is correct |
59 |
Correct |
17 ms |
23916 KB |
Output is correct |
60 |
Correct |
17 ms |
23916 KB |
Output is correct |
61 |
Correct |
156 ms |
32364 KB |
Output is correct |
62 |
Correct |
21 ms |
24172 KB |
Output is correct |
63 |
Correct |
131 ms |
38508 KB |
Output is correct |
64 |
Correct |
147 ms |
35808 KB |
Output is correct |
65 |
Correct |
18 ms |
24172 KB |
Output is correct |
66 |
Correct |
131 ms |
31852 KB |
Output is correct |
67 |
Correct |
21 ms |
24300 KB |
Output is correct |
68 |
Correct |
18 ms |
24172 KB |
Output is correct |
69 |
Correct |
114 ms |
31708 KB |
Output is correct |
70 |
Correct |
19 ms |
24300 KB |
Output is correct |
71 |
Correct |
136 ms |
36844 KB |
Output is correct |
72 |
Correct |
138 ms |
38380 KB |
Output is correct |
73 |
Correct |
17 ms |
23916 KB |
Output is correct |
74 |
Correct |
144 ms |
38520 KB |
Output is correct |
75 |
Correct |
138 ms |
37356 KB |
Output is correct |
76 |
Correct |
20 ms |
24172 KB |
Output is correct |
77 |
Correct |
19 ms |
24300 KB |
Output is correct |
78 |
Correct |
113 ms |
31604 KB |
Output is correct |
79 |
Correct |
88 ms |
32864 KB |
Output is correct |
80 |
Correct |
18 ms |
24172 KB |
Output is correct |
81 |
Correct |
18 ms |
24172 KB |
Output is correct |
82 |
Correct |
16 ms |
24044 KB |
Output is correct |
83 |
Correct |
17 ms |
23916 KB |
Output is correct |
84 |
Correct |
20 ms |
24172 KB |
Output is correct |
85 |
Correct |
136 ms |
32236 KB |
Output is correct |
86 |
Correct |
18 ms |
24172 KB |
Output is correct |
87 |
Correct |
141 ms |
33280 KB |
Output is correct |
88 |
Correct |
17 ms |
23916 KB |
Output is correct |
89 |
Correct |
144 ms |
32388 KB |
Output is correct |
90 |
Correct |
166 ms |
32236 KB |
Output is correct |
91 |
Correct |
132 ms |
34156 KB |
Output is correct |
92 |
Correct |
120 ms |
33124 KB |
Output is correct |
93 |
Correct |
18 ms |
24044 KB |
Output is correct |
94 |
Correct |
18 ms |
23916 KB |
Output is correct |
95 |
Correct |
91 ms |
33920 KB |
Output is correct |
96 |
Correct |
139 ms |
37484 KB |
Output is correct |
97 |
Correct |
19 ms |
24172 KB |
Output is correct |
98 |
Correct |
19 ms |
23916 KB |
Output is correct |
99 |
Correct |
102 ms |
31832 KB |
Output is correct |
100 |
Correct |
19 ms |
24172 KB |
Output is correct |
101 |
Correct |
148 ms |
32748 KB |
Output is correct |
102 |
Correct |
128 ms |
36972 KB |
Output is correct |
103 |
Correct |
19 ms |
24172 KB |
Output is correct |
104 |
Correct |
18 ms |
24172 KB |
Output is correct |
105 |
Correct |
20 ms |
24172 KB |
Output is correct |
106 |
Correct |
18 ms |
24300 KB |
Output is correct |
107 |
Correct |
18 ms |
24044 KB |
Output is correct |
108 |
Correct |
740 ms |
65664 KB |
Output is correct |
109 |
Correct |
907 ms |
104052 KB |
Output is correct |
110 |
Correct |
895 ms |
104420 KB |
Output is correct |
111 |
Correct |
943 ms |
95468 KB |
Output is correct |
112 |
Correct |
790 ms |
82272 KB |
Output is correct |
113 |
Correct |
575 ms |
75592 KB |
Output is correct |
114 |
Correct |
905 ms |
90988 KB |
Output is correct |
115 |
Correct |
877 ms |
96108 KB |
Output is correct |
116 |
Correct |
976 ms |
70856 KB |
Output is correct |
117 |
Correct |
788 ms |
76524 KB |
Output is correct |
118 |
Correct |
854 ms |
69868 KB |
Output is correct |
119 |
Correct |
796 ms |
76600 KB |
Output is correct |
120 |
Correct |
842 ms |
91716 KB |
Output is correct |
121 |
Correct |
814 ms |
76660 KB |
Output is correct |
122 |
Correct |
930 ms |
83300 KB |
Output is correct |
123 |
Correct |
559 ms |
79080 KB |
Output is correct |
124 |
Correct |
776 ms |
67308 KB |
Output is correct |