#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<int> comp;
vector<int> topsort;
vector<bool> visited;
vector<bool> marked;
DirectedGraph(int _n){
n = _n;
adj.resize(n);
comp.resize(n);
visited.assign(n,false);
marked.assign(n,false);
}
void add_edge(int &a,int &b){
adj[a].pb(b);
}
void topsort_dfs(int node){
visited[node] = true;
for(int i : adj[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);
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 |
6 ms |
9684 KB |
Output is correct |
2 |
Correct |
6 ms |
9696 KB |
Output is correct |
3 |
Correct |
5 ms |
9708 KB |
Output is correct |
4 |
Correct |
8 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
7 ms |
9684 KB |
Output is correct |
7 |
Correct |
8 ms |
9620 KB |
Output is correct |
8 |
Correct |
5 ms |
9708 KB |
Output is correct |
9 |
Correct |
7 ms |
9652 KB |
Output is correct |
10 |
Correct |
7 ms |
9624 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9684 KB |
Output is correct |
2 |
Correct |
6 ms |
9696 KB |
Output is correct |
3 |
Correct |
5 ms |
9708 KB |
Output is correct |
4 |
Correct |
8 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
7 ms |
9684 KB |
Output is correct |
7 |
Correct |
8 ms |
9620 KB |
Output is correct |
8 |
Correct |
5 ms |
9708 KB |
Output is correct |
9 |
Correct |
7 ms |
9652 KB |
Output is correct |
10 |
Correct |
7 ms |
9624 KB |
Output is correct |
11 |
Correct |
9 ms |
9812 KB |
Output is correct |
12 |
Correct |
7 ms |
9796 KB |
Output is correct |
13 |
Correct |
7 ms |
9844 KB |
Output is correct |
14 |
Correct |
8 ms |
9840 KB |
Output is correct |
15 |
Correct |
7 ms |
9888 KB |
Output is correct |
16 |
Correct |
7 ms |
9812 KB |
Output is correct |
17 |
Correct |
7 ms |
9812 KB |
Output is correct |
18 |
Correct |
7 ms |
9924 KB |
Output is correct |
19 |
Correct |
9 ms |
9900 KB |
Output is correct |
20 |
Correct |
6 ms |
9940 KB |
Output is correct |
21 |
Correct |
7 ms |
9844 KB |
Output is correct |
22 |
Correct |
7 ms |
9940 KB |
Output is correct |
23 |
Correct |
7 ms |
9940 KB |
Output is correct |
24 |
Correct |
7 ms |
9864 KB |
Output is correct |
25 |
Correct |
7 ms |
9940 KB |
Output is correct |
26 |
Correct |
10 ms |
9940 KB |
Output is correct |
27 |
Correct |
7 ms |
9976 KB |
Output is correct |
28 |
Correct |
9 ms |
9940 KB |
Output is correct |
29 |
Correct |
11 ms |
9880 KB |
Output is correct |
30 |
Correct |
10 ms |
9844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
322 ms |
41448 KB |
Output is correct |
2 |
Correct |
212 ms |
41800 KB |
Output is correct |
3 |
Correct |
381 ms |
41444 KB |
Output is correct |
4 |
Correct |
220 ms |
41732 KB |
Output is correct |
5 |
Correct |
322 ms |
39036 KB |
Output is correct |
6 |
Correct |
202 ms |
41664 KB |
Output is correct |
7 |
Correct |
319 ms |
38872 KB |
Output is correct |
8 |
Correct |
203 ms |
40720 KB |
Output is correct |
9 |
Correct |
278 ms |
36040 KB |
Output is correct |
10 |
Correct |
356 ms |
34444 KB |
Output is correct |
11 |
Correct |
367 ms |
36344 KB |
Output is correct |
12 |
Correct |
372 ms |
37848 KB |
Output is correct |
13 |
Correct |
283 ms |
34108 KB |
Output is correct |
14 |
Correct |
293 ms |
38092 KB |
Output is correct |
15 |
Correct |
348 ms |
38108 KB |
Output is correct |
16 |
Correct |
296 ms |
34932 KB |
Output is correct |
17 |
Correct |
280 ms |
35336 KB |
Output is correct |
18 |
Correct |
291 ms |
35684 KB |
Output is correct |
19 |
Correct |
349 ms |
37428 KB |
Output is correct |
20 |
Correct |
282 ms |
38220 KB |
Output is correct |
21 |
Correct |
5 ms |
9684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9684 KB |
Output is correct |
2 |
Correct |
6 ms |
9696 KB |
Output is correct |
3 |
Correct |
5 ms |
9708 KB |
Output is correct |
4 |
Correct |
8 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
7 ms |
9684 KB |
Output is correct |
7 |
Correct |
8 ms |
9620 KB |
Output is correct |
8 |
Correct |
5 ms |
9708 KB |
Output is correct |
9 |
Correct |
7 ms |
9652 KB |
Output is correct |
10 |
Correct |
7 ms |
9624 KB |
Output is correct |
11 |
Correct |
9 ms |
9812 KB |
Output is correct |
12 |
Correct |
7 ms |
9796 KB |
Output is correct |
13 |
Correct |
7 ms |
9844 KB |
Output is correct |
14 |
Correct |
8 ms |
9840 KB |
Output is correct |
15 |
Correct |
7 ms |
9888 KB |
Output is correct |
16 |
Correct |
7 ms |
9812 KB |
Output is correct |
17 |
Correct |
7 ms |
9812 KB |
Output is correct |
18 |
Correct |
7 ms |
9924 KB |
Output is correct |
19 |
Correct |
9 ms |
9900 KB |
Output is correct |
20 |
Correct |
6 ms |
9940 KB |
Output is correct |
21 |
Correct |
7 ms |
9844 KB |
Output is correct |
22 |
Correct |
7 ms |
9940 KB |
Output is correct |
23 |
Correct |
7 ms |
9940 KB |
Output is correct |
24 |
Correct |
7 ms |
9864 KB |
Output is correct |
25 |
Correct |
7 ms |
9940 KB |
Output is correct |
26 |
Correct |
10 ms |
9940 KB |
Output is correct |
27 |
Correct |
7 ms |
9976 KB |
Output is correct |
28 |
Correct |
9 ms |
9940 KB |
Output is correct |
29 |
Correct |
11 ms |
9880 KB |
Output is correct |
30 |
Correct |
10 ms |
9844 KB |
Output is correct |
31 |
Correct |
322 ms |
41448 KB |
Output is correct |
32 |
Correct |
212 ms |
41800 KB |
Output is correct |
33 |
Correct |
381 ms |
41444 KB |
Output is correct |
34 |
Correct |
220 ms |
41732 KB |
Output is correct |
35 |
Correct |
322 ms |
39036 KB |
Output is correct |
36 |
Correct |
202 ms |
41664 KB |
Output is correct |
37 |
Correct |
319 ms |
38872 KB |
Output is correct |
38 |
Correct |
203 ms |
40720 KB |
Output is correct |
39 |
Correct |
278 ms |
36040 KB |
Output is correct |
40 |
Correct |
356 ms |
34444 KB |
Output is correct |
41 |
Correct |
367 ms |
36344 KB |
Output is correct |
42 |
Correct |
372 ms |
37848 KB |
Output is correct |
43 |
Correct |
283 ms |
34108 KB |
Output is correct |
44 |
Correct |
293 ms |
38092 KB |
Output is correct |
45 |
Correct |
348 ms |
38108 KB |
Output is correct |
46 |
Correct |
296 ms |
34932 KB |
Output is correct |
47 |
Correct |
280 ms |
35336 KB |
Output is correct |
48 |
Correct |
291 ms |
35684 KB |
Output is correct |
49 |
Correct |
349 ms |
37428 KB |
Output is correct |
50 |
Correct |
282 ms |
38220 KB |
Output is correct |
51 |
Correct |
5 ms |
9684 KB |
Output is correct |
52 |
Correct |
291 ms |
27132 KB |
Output is correct |
53 |
Correct |
287 ms |
26956 KB |
Output is correct |
54 |
Incorrect |
324 ms |
27040 KB |
Output isn't correct |
55 |
Halted |
0 ms |
0 KB |
- |