#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pcc pair<char,char>
#define vi vector <int>
#define vl vector <ll>
#define sd(x) scanf("%d",&x)
#define slld(x) scanf("%lld",&x)
#define pd(x) printf("%d",x)
#define plld(x) printf("%lld",x)
#define pds(x) printf("%d ",x)
#define pllds(x) printf("%lld ",x)
#define pdn(x) printf("%d\n",x)
#define plldn(x) printf("%lld\n",x)
using namespace std;
ll powmod(ll base,ll exponent,ll mod){
ll ans=1;
if(base<0) base+=mod;
while(exponent){
if(exponent&1)ans=(ans*base)%mod;
base=(base*base)%mod;
exponent/=2;
}
return ans;
}
ll gcd(ll a, ll b){
if(b==0) return a;
else return gcd(b,a%b);
}
const int INF = 2e9;
const ll INFLL = 4e18;
const int upperlimit = 2e5+1;
const int mod = 1e9+7;
struct DirectedGraph{
int n;
vector<vector<int>> adj;
vector<vector<int>> compladj;
vector<int> topsort;
vector<bool> visited;
vector<bool> marked;
DirectedGraph(int _n){
n = _n;
adj.resize(n);
compladj.resize(n);
visited.assign(n,false);
marked.assign(n,false);
}
void add_edge(int &a,int &b){
adj[a].pb(b);
compladj[b].pb(a);
}
void topsort_dfs(int node){
visited[node] = true;
for(int i : compladj[node]) if(! visited[i]) topsort_dfs(i);
topsort.pb(node);
}
void scc_dfs(int node, int &sz, bool &is_end, vi &node_list){
sz++;
node_list.pb(node);
visited[node] = true;
for(int i : adj[node]){
if(marked[i]) is_end = false;
else if(! visited[i]) scc_dfs(i,sz,is_end,node_list);
}
}
int calculate_ans(){
for(int i = 0; i < n; i++) if(! visited[i]) topsort_dfs(i);
reverse(topsort.begin(),topsort.end());
visited.assign(n,false);
int ans = n,sz;
bool is_end;
vi node_list;
for(int i : topsort){
if(! marked[i]){
sz = 0;
is_end = true;
node_list.clear();
scc_dfs(i,sz,is_end,node_list);
for(int j : node_list) marked[j] = true;
if(is_end) ans = min(ans,sz);
}
}
return ans;
}
};
vi adj[upperlimit];
int par[upperlimit];
vi indices[upperlimit];
int etl[upperlimit];
int etr[upperlimit];
int c[upperlimit];
int node_cnt = 0;
void dfs(int node,int p){
par[node] = p;
indices[c[node]].pb(etl[node] = ++node_cnt);
for(int i : adj[node]) if(i != p) dfs(i,node);
etr[node] = node_cnt;
}
int main() {
int n,k,a,b;
cin >> n >> k;
for(int i = 1; i < n; i++){
cin >> a >> b;
a--;b--;
adj[a].pb(b);
adj[b].pb(a);
}
for(int i = 0; i < n; i++){
cin >> c[i];
c[i]--;
}
DirectedGraph dg(k);
dfs(0,-1);
for(int i = 1; i < n; i++){
if((indices[c[i]][0] < etl[i]) || (indices[c[i]].back() > etr[i])) dg.add_edge(c[i],c[par[i]]);
if(lower_bound(indices[c[par[i]]].begin(),indices[c[par[i]]].end(),etl[i]) != upper_bound(indices[c[par[i]]].begin(),indices[c[par[i]]].end(),etr[i])) dg.add_edge(c[par[i]],c[i]);
}
cout << dg.calculate_ans() - 1;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
4 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9720 KB |
Output is correct |
5 |
Correct |
6 ms |
9684 KB |
Output is correct |
6 |
Correct |
6 ms |
9604 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
6 ms |
9684 KB |
Output is correct |
9 |
Correct |
7 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
4 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9720 KB |
Output is correct |
5 |
Correct |
6 ms |
9684 KB |
Output is correct |
6 |
Correct |
6 ms |
9604 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
6 ms |
9684 KB |
Output is correct |
9 |
Correct |
7 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9684 KB |
Output is correct |
11 |
Correct |
8 ms |
9812 KB |
Output is correct |
12 |
Correct |
7 ms |
9772 KB |
Output is correct |
13 |
Correct |
6 ms |
9812 KB |
Output is correct |
14 |
Correct |
7 ms |
9860 KB |
Output is correct |
15 |
Correct |
8 ms |
9940 KB |
Output is correct |
16 |
Correct |
8 ms |
9844 KB |
Output is correct |
17 |
Correct |
7 ms |
9940 KB |
Output is correct |
18 |
Correct |
8 ms |
9904 KB |
Output is correct |
19 |
Correct |
7 ms |
9940 KB |
Output is correct |
20 |
Correct |
7 ms |
9940 KB |
Output is correct |
21 |
Correct |
8 ms |
9896 KB |
Output is correct |
22 |
Correct |
8 ms |
10008 KB |
Output is correct |
23 |
Correct |
9 ms |
10068 KB |
Output is correct |
24 |
Correct |
7 ms |
9940 KB |
Output is correct |
25 |
Correct |
9 ms |
9940 KB |
Output is correct |
26 |
Correct |
7 ms |
9940 KB |
Output is correct |
27 |
Correct |
10 ms |
9928 KB |
Output is correct |
28 |
Correct |
7 ms |
9868 KB |
Output is correct |
29 |
Correct |
7 ms |
9940 KB |
Output is correct |
30 |
Correct |
7 ms |
9940 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
342 ms |
42916 KB |
Output is correct |
2 |
Correct |
239 ms |
43224 KB |
Output is correct |
3 |
Correct |
375 ms |
42724 KB |
Output is correct |
4 |
Correct |
219 ms |
43180 KB |
Output is correct |
5 |
Correct |
374 ms |
40256 KB |
Output is correct |
6 |
Correct |
217 ms |
43184 KB |
Output is correct |
7 |
Correct |
394 ms |
39836 KB |
Output is correct |
8 |
Correct |
219 ms |
41500 KB |
Output is correct |
9 |
Correct |
358 ms |
35648 KB |
Output is correct |
10 |
Correct |
352 ms |
33864 KB |
Output is correct |
11 |
Correct |
368 ms |
35600 KB |
Output is correct |
12 |
Correct |
360 ms |
37332 KB |
Output is correct |
13 |
Correct |
357 ms |
33716 KB |
Output is correct |
14 |
Correct |
341 ms |
37556 KB |
Output is correct |
15 |
Correct |
300 ms |
37636 KB |
Output is correct |
16 |
Correct |
348 ms |
34316 KB |
Output is correct |
17 |
Correct |
349 ms |
34732 KB |
Output is correct |
18 |
Correct |
324 ms |
35088 KB |
Output is correct |
19 |
Correct |
318 ms |
36784 KB |
Output is correct |
20 |
Correct |
310 ms |
37752 KB |
Output is correct |
21 |
Correct |
6 ms |
9684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
4 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9720 KB |
Output is correct |
5 |
Correct |
6 ms |
9684 KB |
Output is correct |
6 |
Correct |
6 ms |
9604 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
6 ms |
9684 KB |
Output is correct |
9 |
Correct |
7 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9684 KB |
Output is correct |
11 |
Correct |
8 ms |
9812 KB |
Output is correct |
12 |
Correct |
7 ms |
9772 KB |
Output is correct |
13 |
Correct |
6 ms |
9812 KB |
Output is correct |
14 |
Correct |
7 ms |
9860 KB |
Output is correct |
15 |
Correct |
8 ms |
9940 KB |
Output is correct |
16 |
Correct |
8 ms |
9844 KB |
Output is correct |
17 |
Correct |
7 ms |
9940 KB |
Output is correct |
18 |
Correct |
8 ms |
9904 KB |
Output is correct |
19 |
Correct |
7 ms |
9940 KB |
Output is correct |
20 |
Correct |
7 ms |
9940 KB |
Output is correct |
21 |
Correct |
8 ms |
9896 KB |
Output is correct |
22 |
Correct |
8 ms |
10008 KB |
Output is correct |
23 |
Correct |
9 ms |
10068 KB |
Output is correct |
24 |
Correct |
7 ms |
9940 KB |
Output is correct |
25 |
Correct |
9 ms |
9940 KB |
Output is correct |
26 |
Correct |
7 ms |
9940 KB |
Output is correct |
27 |
Correct |
10 ms |
9928 KB |
Output is correct |
28 |
Correct |
7 ms |
9868 KB |
Output is correct |
29 |
Correct |
7 ms |
9940 KB |
Output is correct |
30 |
Correct |
7 ms |
9940 KB |
Output is correct |
31 |
Correct |
342 ms |
42916 KB |
Output is correct |
32 |
Correct |
239 ms |
43224 KB |
Output is correct |
33 |
Correct |
375 ms |
42724 KB |
Output is correct |
34 |
Correct |
219 ms |
43180 KB |
Output is correct |
35 |
Correct |
374 ms |
40256 KB |
Output is correct |
36 |
Correct |
217 ms |
43184 KB |
Output is correct |
37 |
Correct |
394 ms |
39836 KB |
Output is correct |
38 |
Correct |
219 ms |
41500 KB |
Output is correct |
39 |
Correct |
358 ms |
35648 KB |
Output is correct |
40 |
Correct |
352 ms |
33864 KB |
Output is correct |
41 |
Correct |
368 ms |
35600 KB |
Output is correct |
42 |
Correct |
360 ms |
37332 KB |
Output is correct |
43 |
Correct |
357 ms |
33716 KB |
Output is correct |
44 |
Correct |
341 ms |
37556 KB |
Output is correct |
45 |
Correct |
300 ms |
37636 KB |
Output is correct |
46 |
Correct |
348 ms |
34316 KB |
Output is correct |
47 |
Correct |
349 ms |
34732 KB |
Output is correct |
48 |
Correct |
324 ms |
35088 KB |
Output is correct |
49 |
Correct |
318 ms |
36784 KB |
Output is correct |
50 |
Correct |
310 ms |
37752 KB |
Output is correct |
51 |
Correct |
6 ms |
9684 KB |
Output is correct |
52 |
Correct |
303 ms |
25544 KB |
Output is correct |
53 |
Correct |
296 ms |
25364 KB |
Output is correct |
54 |
Correct |
297 ms |
25340 KB |
Output is correct |
55 |
Correct |
305 ms |
28820 KB |
Output is correct |
56 |
Correct |
314 ms |
28876 KB |
Output is correct |
57 |
Correct |
308 ms |
28868 KB |
Output is correct |
58 |
Correct |
332 ms |
35572 KB |
Output is correct |
59 |
Correct |
343 ms |
35872 KB |
Output is correct |
60 |
Correct |
378 ms |
36000 KB |
Output is correct |
61 |
Correct |
387 ms |
35800 KB |
Output is correct |
62 |
Correct |
229 ms |
46868 KB |
Output is correct |
63 |
Correct |
211 ms |
47076 KB |
Output is correct |
64 |
Correct |
260 ms |
45688 KB |
Output is correct |
65 |
Correct |
227 ms |
46880 KB |
Output is correct |
66 |
Correct |
303 ms |
36472 KB |
Output is correct |
67 |
Correct |
297 ms |
36392 KB |
Output is correct |
68 |
Correct |
302 ms |
36452 KB |
Output is correct |
69 |
Correct |
286 ms |
36460 KB |
Output is correct |
70 |
Correct |
290 ms |
36472 KB |
Output is correct |
71 |
Correct |
278 ms |
36396 KB |
Output is correct |
72 |
Correct |
287 ms |
36408 KB |
Output is correct |
73 |
Correct |
313 ms |
35832 KB |
Output is correct |
74 |
Correct |
293 ms |
36408 KB |
Output is correct |
75 |
Correct |
265 ms |
36444 KB |
Output is correct |
76 |
Correct |
357 ms |
40800 KB |
Output is correct |
77 |
Correct |
393 ms |
39800 KB |
Output is correct |
78 |
Correct |
358 ms |
38516 KB |
Output is correct |
79 |
Correct |
375 ms |
36964 KB |
Output is correct |
80 |
Correct |
365 ms |
41188 KB |
Output is correct |
81 |
Correct |
367 ms |
39268 KB |
Output is correct |
82 |
Correct |
323 ms |
39568 KB |
Output is correct |
83 |
Correct |
338 ms |
37344 KB |
Output is correct |
84 |
Correct |
321 ms |
41032 KB |
Output is correct |
85 |
Correct |
337 ms |
39888 KB |
Output is correct |
86 |
Correct |
317 ms |
36940 KB |
Output is correct |
87 |
Correct |
338 ms |
37968 KB |
Output is correct |
88 |
Correct |
357 ms |
38592 KB |
Output is correct |
89 |
Correct |
293 ms |
35748 KB |
Output is correct |
90 |
Correct |
306 ms |
35596 KB |
Output is correct |
91 |
Correct |
299 ms |
37068 KB |
Output is correct |
92 |
Correct |
367 ms |
36932 KB |
Output is correct |
93 |
Correct |
322 ms |
36144 KB |
Output is correct |
94 |
Correct |
318 ms |
35812 KB |
Output is correct |
95 |
Correct |
334 ms |
36584 KB |
Output is correct |
96 |
Correct |
323 ms |
36448 KB |
Output is correct |
97 |
Correct |
298 ms |
37352 KB |
Output is correct |