#include <bits/stdc++.h>
using namespace std;
using ii = pair<int, int>;
#define X first
#define Y second
int n, k, m;
int c[200007];
vector<int> G[200007];
vector<ii> C[200007];
vector<ii> E;
vector<int> G2[4000007];
int num[20][200007], jp[20][200007];
int nxt = 1;
int ord[200007];
int h[200007];
vector<int> topo;
bool vis[4000007];
int scc[4000007], scc_size[4000007];
int scc_cnt;
void dfs(int w) {
ord[w] = nxt++;
for(int u : G[w]) {
if(u != jp[0][w]) {
jp[0][u] = w;
h[u] = h[w] + 1;
dfs(u);
}
}
}
void mark_path(int a, int b, int c) {
if(h[a] < h[b])
swap(a, b);
for(int j = 17 ; j >= 0 ; j--) {
if(h[jp[j][a]] >= h[b]) {
E.emplace_back(c, num[j][a]);
a = jp[j][a];
}
}
if(a == b)
return ;
for(int j = 17 ; j >= 0 ; j--) {
if(jp[j][a] != jp[j][b]) {
E.emplace_back(c, num[j][a]);
E.emplace_back(c, num[j][b]);
a = jp[j][a];
b = jp[j][b];
}
}
E.emplace_back(c, num[0][a]);
}
void dfs2(int w) {
if(vis[w])
return ;
vis[w] = 1;
for(int u : G2[w])
dfs2(u);
topo.push_back(w);
}
void dfs3(int w) {
for(int u : G2[w]) {
if(!scc[u]) {
scc[u] = scc[w];
if(u && u <= k)
scc_size[scc[u]]++;
dfs3(u);
}
}
}
int main() {
scanf("%d%d", &n, &k);
for(int i = 1 ; i < n ; i++) {
int a, b;
scanf("%d%d", &a, &b);
G[a].push_back(b);
G[b].push_back(a);
}
for(int i = 1 ; i <= n ; i++)
scanf("%d", &c[i]);
h[1] = 1;
dfs(1);
m = k;
for(int i = 1 ; i <= n ; i++) {
num[0][i] = ++m;
//~ E.emplace_back(num[0][i], c[i]);
if(jp[0][i])
E.emplace_back(num[0][i], c[jp[0][i]]);
}
for(int j = 1 ; j <= 17 ; j++) {
for(int i = 1 ; i <= n ; i++) {
num[j][i] = ++m;
jp[j][i] = jp[j - 1][jp[j - 1][i]];
E.emplace_back(num[j][i], num[j - 1][i]);
E.emplace_back(num[j][i], num[j - 1][jp[j - 1][i]]);
}
}
for(int i = 1 ; i <= n ; i++)
C[c[i]].emplace_back(ord[i], i);
for(int i = 1 ; i <= k ; i++) {
sort(C[i].begin(), C[i].end());
for(int j = 0 ; j + 1 < C[i].size() ; j++)
mark_path(C[i][j].Y, C[i][j + 1].Y, i);
}
for(auto p : E) {
//~ cout << p.X << " " << p.Y << endl;
G2[p.X].push_back(p.Y);
}
for(int i = 1 ; i <= m ; i++)
if(!vis[i])
dfs2(i);
reverse(topo.begin(), topo.end());
for(int i = 1 ; i <= m ; i++)
G2[i].clear();
for(auto p : E)
G2[p.Y].push_back(p.X);
for(int w : topo) {
if(!scc[w]) {
scc[w] = ++scc_cnt;
if(w && w <= k)
scc_size[scc[w]]++;
dfs3(w);
}
}
//~ for(int i = 0 ; i <= m ; i++)
//~ cout << i << ": " << scc[i] << endl;
for(auto p : E)
if(scc[p.X] != scc[p.Y])
scc_size[scc[p.X]] = 0;
int res = 1e9;
for(int i = 1 ; i <= scc_cnt ; i++) {
//~ cout << i << " " << " " << scc_size[i] << endl;
if(scc_size[i])
res = min(res, scc_size[i] - 1);
}
printf("%d\n", res);
return 0;
}
Compilation message
capital_city.cpp: In function 'int main()':
capital_city.cpp:119:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j = 0 ; j + 1 < C[i].size() ; j++)
~~~~~~^~~~~~~~~~~~~
capital_city.cpp:83:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &n, &k);
~~~~~^~~~~~~~~~~~~~~~
capital_city.cpp:86:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &a, &b);
~~~~~^~~~~~~~~~~~~~~~
capital_city.cpp:91:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &c[i]);
~~~~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
61 ms |
103928 KB |
Output is correct |
2 |
Correct |
60 ms |
103928 KB |
Output is correct |
3 |
Correct |
60 ms |
103936 KB |
Output is correct |
4 |
Correct |
59 ms |
103928 KB |
Output is correct |
5 |
Correct |
60 ms |
103928 KB |
Output is correct |
6 |
Correct |
59 ms |
103928 KB |
Output is correct |
7 |
Correct |
60 ms |
103928 KB |
Output is correct |
8 |
Correct |
60 ms |
103928 KB |
Output is correct |
9 |
Correct |
61 ms |
103928 KB |
Output is correct |
10 |
Correct |
61 ms |
103800 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
61 ms |
103928 KB |
Output is correct |
2 |
Correct |
60 ms |
103928 KB |
Output is correct |
3 |
Correct |
60 ms |
103936 KB |
Output is correct |
4 |
Correct |
59 ms |
103928 KB |
Output is correct |
5 |
Correct |
60 ms |
103928 KB |
Output is correct |
6 |
Correct |
59 ms |
103928 KB |
Output is correct |
7 |
Correct |
60 ms |
103928 KB |
Output is correct |
8 |
Correct |
60 ms |
103928 KB |
Output is correct |
9 |
Correct |
61 ms |
103928 KB |
Output is correct |
10 |
Correct |
61 ms |
103800 KB |
Output is correct |
11 |
Correct |
70 ms |
106876 KB |
Output is correct |
12 |
Correct |
70 ms |
106864 KB |
Output is correct |
13 |
Correct |
70 ms |
106864 KB |
Output is correct |
14 |
Correct |
69 ms |
106888 KB |
Output is correct |
15 |
Correct |
74 ms |
107028 KB |
Output is correct |
16 |
Correct |
71 ms |
106832 KB |
Output is correct |
17 |
Correct |
68 ms |
106864 KB |
Output is correct |
18 |
Correct |
69 ms |
106864 KB |
Output is correct |
19 |
Correct |
70 ms |
106864 KB |
Output is correct |
20 |
Correct |
68 ms |
106864 KB |
Output is correct |
21 |
Correct |
70 ms |
106864 KB |
Output is correct |
22 |
Correct |
70 ms |
106864 KB |
Output is correct |
23 |
Correct |
73 ms |
107120 KB |
Output is correct |
24 |
Correct |
70 ms |
106864 KB |
Output is correct |
25 |
Correct |
70 ms |
106736 KB |
Output is correct |
26 |
Correct |
74 ms |
106996 KB |
Output is correct |
27 |
Correct |
70 ms |
106740 KB |
Output is correct |
28 |
Correct |
70 ms |
106864 KB |
Output is correct |
29 |
Correct |
70 ms |
106864 KB |
Output is correct |
30 |
Correct |
72 ms |
106864 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2879 ms |
431420 KB |
Output is correct |
2 |
Correct |
1075 ms |
397452 KB |
Output is correct |
3 |
Correct |
2898 ms |
408916 KB |
Output is correct |
4 |
Correct |
1070 ms |
397652 KB |
Output is correct |
5 |
Correct |
2812 ms |
431060 KB |
Output is correct |
6 |
Correct |
1092 ms |
397268 KB |
Output is correct |
7 |
Correct |
2704 ms |
419264 KB |
Output is correct |
8 |
Correct |
1065 ms |
394804 KB |
Output is correct |
9 |
Correct |
1575 ms |
382120 KB |
Output is correct |
10 |
Correct |
1542 ms |
380376 KB |
Output is correct |
11 |
Correct |
1604 ms |
382420 KB |
Output is correct |
12 |
Correct |
1622 ms |
383884 KB |
Output is correct |
13 |
Correct |
1625 ms |
380372 KB |
Output is correct |
14 |
Correct |
1628 ms |
384216 KB |
Output is correct |
15 |
Correct |
1694 ms |
383828 KB |
Output is correct |
16 |
Correct |
1648 ms |
380500 KB |
Output is correct |
17 |
Correct |
1645 ms |
381140 KB |
Output is correct |
18 |
Correct |
1653 ms |
381140 KB |
Output is correct |
19 |
Correct |
1724 ms |
383468 KB |
Output is correct |
20 |
Correct |
1511 ms |
384980 KB |
Output is correct |
21 |
Correct |
64 ms |
103800 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
61 ms |
103928 KB |
Output is correct |
2 |
Correct |
60 ms |
103928 KB |
Output is correct |
3 |
Correct |
60 ms |
103936 KB |
Output is correct |
4 |
Correct |
59 ms |
103928 KB |
Output is correct |
5 |
Correct |
60 ms |
103928 KB |
Output is correct |
6 |
Correct |
59 ms |
103928 KB |
Output is correct |
7 |
Correct |
60 ms |
103928 KB |
Output is correct |
8 |
Correct |
60 ms |
103928 KB |
Output is correct |
9 |
Correct |
61 ms |
103928 KB |
Output is correct |
10 |
Correct |
61 ms |
103800 KB |
Output is correct |
11 |
Correct |
70 ms |
106876 KB |
Output is correct |
12 |
Correct |
70 ms |
106864 KB |
Output is correct |
13 |
Correct |
70 ms |
106864 KB |
Output is correct |
14 |
Correct |
69 ms |
106888 KB |
Output is correct |
15 |
Correct |
74 ms |
107028 KB |
Output is correct |
16 |
Correct |
71 ms |
106832 KB |
Output is correct |
17 |
Correct |
68 ms |
106864 KB |
Output is correct |
18 |
Correct |
69 ms |
106864 KB |
Output is correct |
19 |
Correct |
70 ms |
106864 KB |
Output is correct |
20 |
Correct |
68 ms |
106864 KB |
Output is correct |
21 |
Correct |
70 ms |
106864 KB |
Output is correct |
22 |
Correct |
70 ms |
106864 KB |
Output is correct |
23 |
Correct |
73 ms |
107120 KB |
Output is correct |
24 |
Correct |
70 ms |
106864 KB |
Output is correct |
25 |
Correct |
70 ms |
106736 KB |
Output is correct |
26 |
Correct |
74 ms |
106996 KB |
Output is correct |
27 |
Correct |
70 ms |
106740 KB |
Output is correct |
28 |
Correct |
70 ms |
106864 KB |
Output is correct |
29 |
Correct |
70 ms |
106864 KB |
Output is correct |
30 |
Correct |
72 ms |
106864 KB |
Output is correct |
31 |
Correct |
2879 ms |
431420 KB |
Output is correct |
32 |
Correct |
1075 ms |
397452 KB |
Output is correct |
33 |
Correct |
2898 ms |
408916 KB |
Output is correct |
34 |
Correct |
1070 ms |
397652 KB |
Output is correct |
35 |
Correct |
2812 ms |
431060 KB |
Output is correct |
36 |
Correct |
1092 ms |
397268 KB |
Output is correct |
37 |
Correct |
2704 ms |
419264 KB |
Output is correct |
38 |
Correct |
1065 ms |
394804 KB |
Output is correct |
39 |
Correct |
1575 ms |
382120 KB |
Output is correct |
40 |
Correct |
1542 ms |
380376 KB |
Output is correct |
41 |
Correct |
1604 ms |
382420 KB |
Output is correct |
42 |
Correct |
1622 ms |
383884 KB |
Output is correct |
43 |
Correct |
1625 ms |
380372 KB |
Output is correct |
44 |
Correct |
1628 ms |
384216 KB |
Output is correct |
45 |
Correct |
1694 ms |
383828 KB |
Output is correct |
46 |
Correct |
1648 ms |
380500 KB |
Output is correct |
47 |
Correct |
1645 ms |
381140 KB |
Output is correct |
48 |
Correct |
1653 ms |
381140 KB |
Output is correct |
49 |
Correct |
1724 ms |
383468 KB |
Output is correct |
50 |
Correct |
1511 ms |
384980 KB |
Output is correct |
51 |
Correct |
64 ms |
103800 KB |
Output is correct |
52 |
Correct |
1510 ms |
407616 KB |
Output is correct |
53 |
Correct |
1490 ms |
407632 KB |
Output is correct |
54 |
Correct |
1509 ms |
407508 KB |
Output is correct |
55 |
Correct |
1511 ms |
407764 KB |
Output is correct |
56 |
Correct |
1508 ms |
407636 KB |
Output is correct |
57 |
Correct |
1538 ms |
407640 KB |
Output is correct |
58 |
Correct |
2060 ms |
385748 KB |
Output is correct |
59 |
Correct |
2080 ms |
386516 KB |
Output is correct |
60 |
Correct |
2477 ms |
404820 KB |
Output is correct |
61 |
Correct |
2489 ms |
402812 KB |
Output is correct |
62 |
Correct |
1090 ms |
397656 KB |
Output is correct |
63 |
Correct |
1114 ms |
397500 KB |
Output is correct |
64 |
Correct |
1046 ms |
395348 KB |
Output is correct |
65 |
Correct |
1078 ms |
397396 KB |
Output is correct |
66 |
Correct |
1322 ms |
389276 KB |
Output is correct |
67 |
Correct |
1484 ms |
390208 KB |
Output is correct |
68 |
Correct |
1338 ms |
389336 KB |
Output is correct |
69 |
Correct |
1372 ms |
384312 KB |
Output is correct |
70 |
Correct |
1381 ms |
384428 KB |
Output is correct |
71 |
Correct |
1401 ms |
389356 KB |
Output is correct |
72 |
Correct |
1354 ms |
389364 KB |
Output is correct |
73 |
Correct |
1517 ms |
387304 KB |
Output is correct |
74 |
Correct |
1332 ms |
389304 KB |
Output is correct |
75 |
Correct |
1386 ms |
384320 KB |
Output is correct |
76 |
Correct |
1646 ms |
381912 KB |
Output is correct |
77 |
Correct |
1585 ms |
381780 KB |
Output is correct |
78 |
Correct |
1733 ms |
380928 KB |
Output is correct |
79 |
Correct |
1675 ms |
379604 KB |
Output is correct |
80 |
Correct |
1537 ms |
384596 KB |
Output is correct |
81 |
Correct |
1603 ms |
381908 KB |
Output is correct |
82 |
Correct |
1703 ms |
381908 KB |
Output is correct |
83 |
Correct |
1628 ms |
380376 KB |
Output is correct |
84 |
Correct |
1737 ms |
383444 KB |
Output is correct |
85 |
Correct |
1654 ms |
382824 KB |
Output is correct |
86 |
Correct |
1702 ms |
379672 KB |
Output is correct |
87 |
Correct |
1597 ms |
381272 KB |
Output is correct |
88 |
Correct |
2156 ms |
383764 KB |
Output is correct |
89 |
Correct |
2028 ms |
381184 KB |
Output is correct |
90 |
Correct |
1975 ms |
381132 KB |
Output is correct |
91 |
Correct |
2057 ms |
383064 KB |
Output is correct |
92 |
Correct |
2118 ms |
382168 KB |
Output is correct |
93 |
Correct |
2025 ms |
381400 KB |
Output is correct |
94 |
Correct |
1997 ms |
380884 KB |
Output is correct |
95 |
Correct |
2017 ms |
382168 KB |
Output is correct |
96 |
Correct |
2128 ms |
381172 KB |
Output is correct |
97 |
Correct |
2085 ms |
383064 KB |
Output is correct |