#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
/*
Draw a directed edge between city A and city B, if there lies a town of city B on the path between two
towns of city A.
Answer = (minimum size of any SCC) - 1
*/
const int maxN = 200000;
int N, K;
vector<int> edge[1+maxN];
int col[1+maxN];
vector<int> col_count(1+maxN, 0);
int parent[1+maxN];
vector<int> order(1);
vector<int> order_index(1+maxN, 0);
vector<int> subtree(1+maxN, 1);
void dfs(int u)
{
order_index[u] = order.size();
order.push_back(u);
for(int v: edge[u])
{
if(order_index[v]) continue;
parent[v] = u;
dfs(v);
subtree[u] += subtree[v];
}
}
struct segtree
{
int l;
int r;
vector<int> v;
segtree* left = NULL;
segtree* right = NULL;
segtree()
{
;
}
segtree(int L, int R)
{
l = L;
r = R;
if(l == r)
{
v.push_back(col[ order[l] ]);
}
else
{
int m = (l+r)/2;
left = new segtree(l, m);
right = new segtree(m+1, r);
for(int x: left->v) v.push_back(x);
for(int x: right->v) v.push_back(x);
sort(v.begin(), v.end());
}
}
int count(int L, int R, int V)
{
if(R < l || r < L) return 0;
else if(L <= l && r <= R)
{
int i1 = -1, i2 = -1;
for(int bit = 18; bit >= 0; bit--)
{
if(i1 + (1 << bit) >= v.size()) continue;
if(v[i1 + (1 << bit)] >= V) continue;
i1 += (1 << bit);
}
i1++;
if(v[i1] != V) return 0;
for(int bit = 18; bit >= 0; bit--)
{
if(i2 + (1 << bit) >= v.size()) continue;
if(v[i2 + (1 << bit)] > V) continue;
i2 += (1 << bit);
}
return i2-i1+1;
}
else
{
return left->count(L, R, V) + right->count(L, R, V);
}
}
};
vector<int> newEdge[1+maxN];
vector<int> newEdgeRev[1+maxN];
vector<int> scc_dfs_order;
vector<int> visit(1+maxN, 0);
void scc_dfs_1(int u)
{
for(int v: newEdge[u])
{
if(visit[v]) continue;
visit[v] = 1;
scc_dfs_1(v);
}
scc_dfs_order.push_back(u);
}
int curr_ct;
vector<int> scc_size(1, 0);
vector<int> scc(1+maxN, 0);
int scc_count = 0;
void scc_dfs_2(int u)
{
scc_size[scc_count]++;
scc[u] = scc_count;
for(int v: newEdgeRev[u])
{
if(visit[v]) continue;
visit[v] = 1;
curr_ct++;
scc_dfs_2(v);
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> N >> K;
for(int i = 1; i <= N-1; i++)
{
int A, B;
cin >> A >> B;
edge[A].push_back(B);
edge[B].push_back(A);
}
for(int i = 1; i <= N; i++)
{
cin >> col[i];
col_count[ col[i] ]++;
}
parent[1] = 0;
dfs(1);
segtree S(1, N);
// for(int i = 1; i <= N; i++) cerr << order[i] << ' ';
// cerr << '\n';
// for(int i = 1; i <= N; i++) cerr << order_index[i] << ' ';
// cerr << '\n';
for(int i = 2; i <= N; i++)
{
if(col[i] == col[ parent[i] ]) continue;
int ct;
ct = S.count(order_index[i], order_index[i] + subtree[i] - 1, col[parent[i]]);
if(ct != col_count[ col[parent[i]] ] && ct != 0)
{
newEdge[ col[ parent[i] ] ].push_back(col[i]);
newEdgeRev[ col[i] ].push_back(col[ parent[i] ]);
}
ct = S.count(order_index[i], order_index[i] + subtree[i] - 1, col[i]);
if(ct != col_count[ col[i] ] && ct != 0)
{
newEdge[ col[i] ].push_back(col[ parent[i] ]);
newEdgeRev[ col[parent[i]] ].push_back(col[i]);
}
}
for(int j = 1; j <= K; j++)
{
if(visit[j]) continue;
visit[j] = 1;
scc_dfs_1(j);
}
reverse(scc_dfs_order.begin(), scc_dfs_order.end());
// for(int j: scc_dfs_order) cerr << j << ' ';
// cerr << '\n';
visit = vector<int>(K+1, 0);
for(int j: scc_dfs_order)
{
if(visit[j]) continue;
scc_count++;
scc_size.push_back(0);
visit[j] = 1;
curr_ct = 1;
scc_dfs_2(j);
// cerr << j << ' ' << curr_ct << '\n';
}
vector<int> scc_outdegree(scc_count+1, 0);
for(int u = 1; u <= K; u++)
for(int v: newEdge[u])
if(scc[u] != scc[v])
scc_outdegree[ scc[u] ]++;
int res = 1e9;
for(int x = 1; x <= scc_count; x++)
if(scc_outdegree[x] == 0)
res = min(res, scc_size[x] - 1);
// for(int i = 1; i <= K; i++) cerr << scc[i] << ' ' << scc_outdegree[scc[i]] << ' ' << scc_size[scc[i]] << '\n';
// cerr << '\n';
cout << res << '\n';
}
Compilation message
capital_city.cpp: In member function 'int segtree::count(int, int, int)':
capital_city.cpp:91:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
91 | if(i1 + (1 << bit) >= v.size()) continue;
| ~~~~~~~~~~~~~~~~^~~~~~~~~~~
capital_city.cpp:101:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
101 | if(i2 + (1 << bit) >= v.size()) continue;
| ~~~~~~~~~~~~~~~~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
18252 KB |
Output is correct |
2 |
Correct |
13 ms |
18252 KB |
Output is correct |
3 |
Correct |
12 ms |
18252 KB |
Output is correct |
4 |
Correct |
12 ms |
18296 KB |
Output is correct |
5 |
Correct |
12 ms |
18288 KB |
Output is correct |
6 |
Correct |
14 ms |
18252 KB |
Output is correct |
7 |
Correct |
11 ms |
18264 KB |
Output is correct |
8 |
Correct |
11 ms |
18252 KB |
Output is correct |
9 |
Correct |
13 ms |
18296 KB |
Output is correct |
10 |
Correct |
11 ms |
18252 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
18252 KB |
Output is correct |
2 |
Correct |
13 ms |
18252 KB |
Output is correct |
3 |
Correct |
12 ms |
18252 KB |
Output is correct |
4 |
Correct |
12 ms |
18296 KB |
Output is correct |
5 |
Correct |
12 ms |
18288 KB |
Output is correct |
6 |
Correct |
14 ms |
18252 KB |
Output is correct |
7 |
Correct |
11 ms |
18264 KB |
Output is correct |
8 |
Correct |
11 ms |
18252 KB |
Output is correct |
9 |
Correct |
13 ms |
18296 KB |
Output is correct |
10 |
Correct |
11 ms |
18252 KB |
Output is correct |
11 |
Correct |
15 ms |
18864 KB |
Output is correct |
12 |
Correct |
15 ms |
18828 KB |
Output is correct |
13 |
Correct |
15 ms |
18892 KB |
Output is correct |
14 |
Correct |
16 ms |
18892 KB |
Output is correct |
15 |
Correct |
16 ms |
18896 KB |
Output is correct |
16 |
Correct |
15 ms |
18936 KB |
Output is correct |
17 |
Correct |
14 ms |
18892 KB |
Output is correct |
18 |
Correct |
26 ms |
18892 KB |
Output is correct |
19 |
Correct |
14 ms |
18892 KB |
Output is correct |
20 |
Correct |
14 ms |
18892 KB |
Output is correct |
21 |
Correct |
14 ms |
18892 KB |
Output is correct |
22 |
Correct |
16 ms |
19036 KB |
Output is correct |
23 |
Correct |
17 ms |
18960 KB |
Output is correct |
24 |
Correct |
21 ms |
18864 KB |
Output is correct |
25 |
Correct |
17 ms |
18952 KB |
Output is correct |
26 |
Correct |
17 ms |
18960 KB |
Output is correct |
27 |
Correct |
17 ms |
19020 KB |
Output is correct |
28 |
Correct |
16 ms |
18972 KB |
Output is correct |
29 |
Correct |
16 ms |
18888 KB |
Output is correct |
30 |
Correct |
16 ms |
18892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1774 ms |
97916 KB |
Output is correct |
2 |
Correct |
485 ms |
98344 KB |
Output is correct |
3 |
Correct |
1731 ms |
97696 KB |
Output is correct |
4 |
Correct |
497 ms |
98472 KB |
Output is correct |
5 |
Correct |
1290 ms |
94928 KB |
Output is correct |
6 |
Correct |
493 ms |
98248 KB |
Output is correct |
7 |
Correct |
1262 ms |
94900 KB |
Output is correct |
8 |
Correct |
440 ms |
97104 KB |
Output is correct |
9 |
Correct |
1676 ms |
93304 KB |
Output is correct |
10 |
Correct |
1563 ms |
91140 KB |
Output is correct |
11 |
Correct |
1393 ms |
93448 KB |
Output is correct |
12 |
Correct |
1411 ms |
95616 KB |
Output is correct |
13 |
Correct |
1563 ms |
90636 KB |
Output is correct |
14 |
Correct |
1324 ms |
95860 KB |
Output is correct |
15 |
Correct |
1525 ms |
95872 KB |
Output is correct |
16 |
Correct |
1500 ms |
91760 KB |
Output is correct |
17 |
Correct |
1508 ms |
92192 KB |
Output is correct |
18 |
Correct |
1537 ms |
92372 KB |
Output is correct |
19 |
Correct |
1583 ms |
94896 KB |
Output is correct |
20 |
Correct |
1468 ms |
96464 KB |
Output is correct |
21 |
Correct |
12 ms |
18264 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
18252 KB |
Output is correct |
2 |
Correct |
13 ms |
18252 KB |
Output is correct |
3 |
Correct |
12 ms |
18252 KB |
Output is correct |
4 |
Correct |
12 ms |
18296 KB |
Output is correct |
5 |
Correct |
12 ms |
18288 KB |
Output is correct |
6 |
Correct |
14 ms |
18252 KB |
Output is correct |
7 |
Correct |
11 ms |
18264 KB |
Output is correct |
8 |
Correct |
11 ms |
18252 KB |
Output is correct |
9 |
Correct |
13 ms |
18296 KB |
Output is correct |
10 |
Correct |
11 ms |
18252 KB |
Output is correct |
11 |
Correct |
15 ms |
18864 KB |
Output is correct |
12 |
Correct |
15 ms |
18828 KB |
Output is correct |
13 |
Correct |
15 ms |
18892 KB |
Output is correct |
14 |
Correct |
16 ms |
18892 KB |
Output is correct |
15 |
Correct |
16 ms |
18896 KB |
Output is correct |
16 |
Correct |
15 ms |
18936 KB |
Output is correct |
17 |
Correct |
14 ms |
18892 KB |
Output is correct |
18 |
Correct |
26 ms |
18892 KB |
Output is correct |
19 |
Correct |
14 ms |
18892 KB |
Output is correct |
20 |
Correct |
14 ms |
18892 KB |
Output is correct |
21 |
Correct |
14 ms |
18892 KB |
Output is correct |
22 |
Correct |
16 ms |
19036 KB |
Output is correct |
23 |
Correct |
17 ms |
18960 KB |
Output is correct |
24 |
Correct |
21 ms |
18864 KB |
Output is correct |
25 |
Correct |
17 ms |
18952 KB |
Output is correct |
26 |
Correct |
17 ms |
18960 KB |
Output is correct |
27 |
Correct |
17 ms |
19020 KB |
Output is correct |
28 |
Correct |
16 ms |
18972 KB |
Output is correct |
29 |
Correct |
16 ms |
18888 KB |
Output is correct |
30 |
Correct |
16 ms |
18892 KB |
Output is correct |
31 |
Correct |
1774 ms |
97916 KB |
Output is correct |
32 |
Correct |
485 ms |
98344 KB |
Output is correct |
33 |
Correct |
1731 ms |
97696 KB |
Output is correct |
34 |
Correct |
497 ms |
98472 KB |
Output is correct |
35 |
Correct |
1290 ms |
94928 KB |
Output is correct |
36 |
Correct |
493 ms |
98248 KB |
Output is correct |
37 |
Correct |
1262 ms |
94900 KB |
Output is correct |
38 |
Correct |
440 ms |
97104 KB |
Output is correct |
39 |
Correct |
1676 ms |
93304 KB |
Output is correct |
40 |
Correct |
1563 ms |
91140 KB |
Output is correct |
41 |
Correct |
1393 ms |
93448 KB |
Output is correct |
42 |
Correct |
1411 ms |
95616 KB |
Output is correct |
43 |
Correct |
1563 ms |
90636 KB |
Output is correct |
44 |
Correct |
1324 ms |
95860 KB |
Output is correct |
45 |
Correct |
1525 ms |
95872 KB |
Output is correct |
46 |
Correct |
1500 ms |
91760 KB |
Output is correct |
47 |
Correct |
1508 ms |
92192 KB |
Output is correct |
48 |
Correct |
1537 ms |
92372 KB |
Output is correct |
49 |
Correct |
1583 ms |
94896 KB |
Output is correct |
50 |
Correct |
1468 ms |
96464 KB |
Output is correct |
51 |
Correct |
12 ms |
18264 KB |
Output is correct |
52 |
Correct |
773 ms |
82788 KB |
Output is correct |
53 |
Correct |
726 ms |
82884 KB |
Output is correct |
54 |
Correct |
776 ms |
82904 KB |
Output is correct |
55 |
Correct |
755 ms |
82812 KB |
Output is correct |
56 |
Correct |
721 ms |
83004 KB |
Output is correct |
57 |
Correct |
720 ms |
82844 KB |
Output is correct |
58 |
Correct |
1135 ms |
86184 KB |
Output is correct |
59 |
Correct |
1234 ms |
86704 KB |
Output is correct |
60 |
Correct |
1085 ms |
85688 KB |
Output is correct |
61 |
Correct |
1063 ms |
85516 KB |
Output is correct |
62 |
Correct |
505 ms |
98336 KB |
Output is correct |
63 |
Correct |
497 ms |
98340 KB |
Output is correct |
64 |
Correct |
472 ms |
97376 KB |
Output is correct |
65 |
Correct |
492 ms |
98240 KB |
Output is correct |
66 |
Correct |
433 ms |
87940 KB |
Output is correct |
67 |
Correct |
441 ms |
87932 KB |
Output is correct |
68 |
Correct |
440 ms |
87900 KB |
Output is correct |
69 |
Correct |
436 ms |
87868 KB |
Output is correct |
70 |
Correct |
434 ms |
87864 KB |
Output is correct |
71 |
Correct |
449 ms |
87992 KB |
Output is correct |
72 |
Correct |
440 ms |
87880 KB |
Output is correct |
73 |
Correct |
437 ms |
87296 KB |
Output is correct |
74 |
Correct |
432 ms |
87860 KB |
Output is correct |
75 |
Correct |
440 ms |
87992 KB |
Output is correct |
76 |
Correct |
1408 ms |
90336 KB |
Output is correct |
77 |
Correct |
1342 ms |
88924 KB |
Output is correct |
78 |
Correct |
1403 ms |
92296 KB |
Output is correct |
79 |
Correct |
1386 ms |
90380 KB |
Output is correct |
80 |
Correct |
1367 ms |
96200 KB |
Output is correct |
81 |
Correct |
1567 ms |
93460 KB |
Output is correct |
82 |
Correct |
1477 ms |
93716 KB |
Output is correct |
83 |
Correct |
1425 ms |
90812 KB |
Output is correct |
84 |
Correct |
1665 ms |
95408 KB |
Output is correct |
85 |
Correct |
1396 ms |
94268 KB |
Output is correct |
86 |
Correct |
1539 ms |
90184 KB |
Output is correct |
87 |
Correct |
1518 ms |
91844 KB |
Output is correct |
88 |
Correct |
1616 ms |
92500 KB |
Output is correct |
89 |
Correct |
1498 ms |
88868 KB |
Output is correct |
90 |
Correct |
1535 ms |
88888 KB |
Output is correct |
91 |
Correct |
1547 ms |
91116 KB |
Output is correct |
92 |
Correct |
1547 ms |
90236 KB |
Output is correct |
93 |
Correct |
1596 ms |
89372 KB |
Output is correct |
94 |
Correct |
1510 ms |
88868 KB |
Output is correct |
95 |
Correct |
1501 ms |
90164 KB |
Output is correct |
96 |
Correct |
1498 ms |
89780 KB |
Output is correct |
97 |
Correct |
1515 ms |
91108 KB |
Output is correct |