//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define mem(a,v) memset((a), (v), sizeof (a))
#define enl printf("\n")
#define case(t) printf("Case #%d: ", (t))
#define ni(n) scanf("%d", &(n))
#define nl(n) scanf("%lld", &(n))
#define nai(a, n) for (int i = 0; i < (n); i++) ni(a[i])
#define nal(a, n) for (int i = 0; i < (n); i++) nl(a[i])
#define pri(n) printf("%d\n", (n))
#define prl(n) printf("%lld\n", (n))
#define pii pair<int, int>
#define pil pair<int, long long>
#define pll pair<long long, long long>
#define vii vector<pii>
#define vil vector<pil>
#define vll vector<pll>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef cc_hash_table<int,int,hash<int>> ht;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> oset;
const double pi = acos(-1);
const int MOD = 1e9 + 7;
const ll INF = 1e16 + 7;
const int MAXN = 1e6 + 5;
const double eps = 1e-9;
int deg[MAXN], col[MAXN], lo[MAXN], disc[MAXN];
int ind[MAXN];
pii edg[MAXN];
vii adj[MAXN];
int t = 0, vis[MAXN], cnt = 0, br[MAXN];
void dfs(int u, int p) {
disc[u] = lo[u] = t++;
vis[u] = 1;
for (pii nx: adj[u]) {
int v, id; tie(v, id) = nx;
if (!vis[v]) {
dfs(v, id);
lo[u] = min(lo[u], lo[v]);
if (disc[u] < lo[v])
br[id] = 1;
} else if (id != p)
lo[u] = min(lo[u], disc[v]);
}
}
int curC = 0;
void bfs(int cur) {
deque<int> pq;
pq.pb(cur);
vis[cur] = 1;
while (!pq.empty()) {
int u = pq.front(); pq.pop_front();
ind[u] = curC;
vis[u] = 1;
for (pii nx: adj[u]) {
int v, id; tie(v, id) = nx;
if (!br[id] && !vis[v])
pq.pb(v);
}
}
curC++;
}
int main() {
int n, k;
scanf("%d %d", &n, &k);
for (int i = 1; i < n; i++) {
int u, v;
scanf("%d %d", &u, &v);
edg[i] = {--u, --v};
}
for (int i = 0; i < n; i++) {
ni(col[i]); --col[i];
}
for (int i = 1; i < n; i++) {
int u, v; tie(u, v) = edg[i];
u = col[u], v = col[v];
if (u == v) continue;
adj[u].pb({v, cnt});
adj[v].pb({u, cnt});
cnt++;
}
dfs(0, -1);
mem(vis,0);
for (int i = 0; i < k; i++)
if (!vis[i])
bfs(i);
for (int i = 0; i < k; i++) {
for (pii nx: adj[i]) {
int u, id; tie(u, id) = nx;
if (!br[id]) continue;
deg[ind[u]]++;
}
}
int lvs = 0;
for (int i = 0; i < curC; i++)
if (deg[i] == 1)
lvs++;
pri((lvs+1)/2);
return 0;
}
Compilation message
mergers.cpp: In function 'int main()':
mergers.cpp:78:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &n, &k);
~~~~~^~~~~~~~~~~~~~~~~
mergers.cpp:81:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &u, &v);
~~~~~^~~~~~~~~~~~~~~~~
mergers.cpp:8:20: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
#define ni(n) scanf("%d", &(n))
~~~~~^~~~~~~~~~~~
mergers.cpp:85:3: note: in expansion of macro 'ni'
ni(col[i]); --col[i];
^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
27776 KB |
Output is correct |
2 |
Correct |
25 ms |
27768 KB |
Output is correct |
3 |
Correct |
25 ms |
27776 KB |
Output is correct |
4 |
Correct |
25 ms |
27776 KB |
Output is correct |
5 |
Correct |
28 ms |
27776 KB |
Output is correct |
6 |
Correct |
27 ms |
27776 KB |
Output is correct |
7 |
Correct |
30 ms |
27896 KB |
Output is correct |
8 |
Correct |
27 ms |
27776 KB |
Output is correct |
9 |
Correct |
26 ms |
27776 KB |
Output is correct |
10 |
Correct |
27 ms |
27776 KB |
Output is correct |
11 |
Correct |
25 ms |
27776 KB |
Output is correct |
12 |
Correct |
25 ms |
27776 KB |
Output is correct |
13 |
Correct |
27 ms |
27776 KB |
Output is correct |
14 |
Correct |
28 ms |
27776 KB |
Output is correct |
15 |
Correct |
27 ms |
27776 KB |
Output is correct |
16 |
Correct |
26 ms |
27768 KB |
Output is correct |
17 |
Correct |
28 ms |
27768 KB |
Output is correct |
18 |
Correct |
31 ms |
27896 KB |
Output is correct |
19 |
Correct |
27 ms |
27772 KB |
Output is correct |
20 |
Correct |
27 ms |
27776 KB |
Output is correct |
21 |
Correct |
30 ms |
27904 KB |
Output is correct |
22 |
Correct |
28 ms |
27768 KB |
Output is correct |
23 |
Correct |
27 ms |
27776 KB |
Output is correct |
24 |
Correct |
27 ms |
27776 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
27776 KB |
Output is correct |
2 |
Correct |
25 ms |
27768 KB |
Output is correct |
3 |
Correct |
25 ms |
27776 KB |
Output is correct |
4 |
Correct |
25 ms |
27776 KB |
Output is correct |
5 |
Correct |
28 ms |
27776 KB |
Output is correct |
6 |
Correct |
27 ms |
27776 KB |
Output is correct |
7 |
Correct |
30 ms |
27896 KB |
Output is correct |
8 |
Correct |
27 ms |
27776 KB |
Output is correct |
9 |
Correct |
26 ms |
27776 KB |
Output is correct |
10 |
Correct |
27 ms |
27776 KB |
Output is correct |
11 |
Correct |
25 ms |
27776 KB |
Output is correct |
12 |
Correct |
25 ms |
27776 KB |
Output is correct |
13 |
Correct |
27 ms |
27776 KB |
Output is correct |
14 |
Correct |
28 ms |
27776 KB |
Output is correct |
15 |
Correct |
27 ms |
27776 KB |
Output is correct |
16 |
Correct |
26 ms |
27768 KB |
Output is correct |
17 |
Correct |
28 ms |
27768 KB |
Output is correct |
18 |
Correct |
31 ms |
27896 KB |
Output is correct |
19 |
Correct |
27 ms |
27772 KB |
Output is correct |
20 |
Correct |
27 ms |
27776 KB |
Output is correct |
21 |
Correct |
30 ms |
27904 KB |
Output is correct |
22 |
Correct |
28 ms |
27768 KB |
Output is correct |
23 |
Correct |
27 ms |
27776 KB |
Output is correct |
24 |
Correct |
27 ms |
27776 KB |
Output is correct |
25 |
Correct |
31 ms |
27776 KB |
Output is correct |
26 |
Correct |
30 ms |
27904 KB |
Output is correct |
27 |
Correct |
29 ms |
27776 KB |
Output is correct |
28 |
Correct |
31 ms |
28024 KB |
Output is correct |
29 |
Correct |
31 ms |
27904 KB |
Output is correct |
30 |
Correct |
28 ms |
27948 KB |
Output is correct |
31 |
Correct |
28 ms |
27776 KB |
Output is correct |
32 |
Correct |
31 ms |
28288 KB |
Output is correct |
33 |
Correct |
30 ms |
27724 KB |
Output is correct |
34 |
Correct |
42 ms |
28508 KB |
Output is correct |
35 |
Correct |
28 ms |
27896 KB |
Output is correct |
36 |
Correct |
28 ms |
27768 KB |
Output is correct |
37 |
Correct |
30 ms |
27896 KB |
Output is correct |
38 |
Correct |
30 ms |
27768 KB |
Output is correct |
39 |
Correct |
34 ms |
28032 KB |
Output is correct |
40 |
Correct |
27 ms |
27776 KB |
Output is correct |
41 |
Correct |
30 ms |
27852 KB |
Output is correct |
42 |
Correct |
33 ms |
28604 KB |
Output is correct |
43 |
Correct |
29 ms |
27776 KB |
Output is correct |
44 |
Correct |
27 ms |
27860 KB |
Output is correct |
45 |
Correct |
34 ms |
28024 KB |
Output is correct |
46 |
Correct |
28 ms |
27904 KB |
Output is correct |
47 |
Correct |
27 ms |
27768 KB |
Output is correct |
48 |
Correct |
28 ms |
27956 KB |
Output is correct |
49 |
Correct |
31 ms |
28024 KB |
Output is correct |
50 |
Correct |
29 ms |
28032 KB |
Output is correct |
51 |
Correct |
27 ms |
27904 KB |
Output is correct |
52 |
Correct |
36 ms |
28252 KB |
Output is correct |
53 |
Correct |
28 ms |
27904 KB |
Output is correct |
54 |
Correct |
31 ms |
27904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
27776 KB |
Output is correct |
2 |
Correct |
25 ms |
27768 KB |
Output is correct |
3 |
Correct |
25 ms |
27776 KB |
Output is correct |
4 |
Correct |
25 ms |
27776 KB |
Output is correct |
5 |
Correct |
28 ms |
27776 KB |
Output is correct |
6 |
Correct |
27 ms |
27776 KB |
Output is correct |
7 |
Correct |
30 ms |
27896 KB |
Output is correct |
8 |
Correct |
27 ms |
27776 KB |
Output is correct |
9 |
Correct |
26 ms |
27776 KB |
Output is correct |
10 |
Correct |
27 ms |
27776 KB |
Output is correct |
11 |
Correct |
25 ms |
27776 KB |
Output is correct |
12 |
Correct |
25 ms |
27776 KB |
Output is correct |
13 |
Correct |
27 ms |
27776 KB |
Output is correct |
14 |
Correct |
28 ms |
27776 KB |
Output is correct |
15 |
Correct |
27 ms |
27776 KB |
Output is correct |
16 |
Correct |
26 ms |
27768 KB |
Output is correct |
17 |
Correct |
28 ms |
27768 KB |
Output is correct |
18 |
Correct |
31 ms |
27896 KB |
Output is correct |
19 |
Correct |
27 ms |
27772 KB |
Output is correct |
20 |
Correct |
27 ms |
27776 KB |
Output is correct |
21 |
Correct |
30 ms |
27904 KB |
Output is correct |
22 |
Correct |
28 ms |
27768 KB |
Output is correct |
23 |
Correct |
27 ms |
27776 KB |
Output is correct |
24 |
Correct |
27 ms |
27776 KB |
Output is correct |
25 |
Correct |
29 ms |
27776 KB |
Output is correct |
26 |
Runtime error |
444 ms |
263168 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
27 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
398 ms |
263168 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
27776 KB |
Output is correct |
2 |
Correct |
25 ms |
27768 KB |
Output is correct |
3 |
Correct |
25 ms |
27776 KB |
Output is correct |
4 |
Correct |
25 ms |
27776 KB |
Output is correct |
5 |
Correct |
28 ms |
27776 KB |
Output is correct |
6 |
Correct |
27 ms |
27776 KB |
Output is correct |
7 |
Correct |
30 ms |
27896 KB |
Output is correct |
8 |
Correct |
27 ms |
27776 KB |
Output is correct |
9 |
Correct |
26 ms |
27776 KB |
Output is correct |
10 |
Correct |
27 ms |
27776 KB |
Output is correct |
11 |
Correct |
25 ms |
27776 KB |
Output is correct |
12 |
Correct |
25 ms |
27776 KB |
Output is correct |
13 |
Correct |
27 ms |
27776 KB |
Output is correct |
14 |
Correct |
28 ms |
27776 KB |
Output is correct |
15 |
Correct |
27 ms |
27776 KB |
Output is correct |
16 |
Correct |
26 ms |
27768 KB |
Output is correct |
17 |
Correct |
28 ms |
27768 KB |
Output is correct |
18 |
Correct |
31 ms |
27896 KB |
Output is correct |
19 |
Correct |
27 ms |
27772 KB |
Output is correct |
20 |
Correct |
27 ms |
27776 KB |
Output is correct |
21 |
Correct |
30 ms |
27904 KB |
Output is correct |
22 |
Correct |
28 ms |
27768 KB |
Output is correct |
23 |
Correct |
27 ms |
27776 KB |
Output is correct |
24 |
Correct |
27 ms |
27776 KB |
Output is correct |
25 |
Correct |
31 ms |
27776 KB |
Output is correct |
26 |
Correct |
30 ms |
27904 KB |
Output is correct |
27 |
Correct |
29 ms |
27776 KB |
Output is correct |
28 |
Correct |
31 ms |
28024 KB |
Output is correct |
29 |
Correct |
31 ms |
27904 KB |
Output is correct |
30 |
Correct |
28 ms |
27948 KB |
Output is correct |
31 |
Correct |
28 ms |
27776 KB |
Output is correct |
32 |
Correct |
31 ms |
28288 KB |
Output is correct |
33 |
Correct |
30 ms |
27724 KB |
Output is correct |
34 |
Correct |
42 ms |
28508 KB |
Output is correct |
35 |
Correct |
28 ms |
27896 KB |
Output is correct |
36 |
Correct |
28 ms |
27768 KB |
Output is correct |
37 |
Correct |
30 ms |
27896 KB |
Output is correct |
38 |
Correct |
30 ms |
27768 KB |
Output is correct |
39 |
Correct |
34 ms |
28032 KB |
Output is correct |
40 |
Correct |
27 ms |
27776 KB |
Output is correct |
41 |
Correct |
30 ms |
27852 KB |
Output is correct |
42 |
Correct |
33 ms |
28604 KB |
Output is correct |
43 |
Correct |
29 ms |
27776 KB |
Output is correct |
44 |
Correct |
27 ms |
27860 KB |
Output is correct |
45 |
Correct |
34 ms |
28024 KB |
Output is correct |
46 |
Correct |
28 ms |
27904 KB |
Output is correct |
47 |
Correct |
27 ms |
27768 KB |
Output is correct |
48 |
Correct |
28 ms |
27956 KB |
Output is correct |
49 |
Correct |
31 ms |
28024 KB |
Output is correct |
50 |
Correct |
29 ms |
28032 KB |
Output is correct |
51 |
Correct |
27 ms |
27904 KB |
Output is correct |
52 |
Correct |
36 ms |
28252 KB |
Output is correct |
53 |
Correct |
28 ms |
27904 KB |
Output is correct |
54 |
Correct |
31 ms |
27904 KB |
Output is correct |
55 |
Correct |
29 ms |
27776 KB |
Output is correct |
56 |
Runtime error |
444 ms |
263168 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
57 |
Halted |
0 ms |
0 KB |
- |