//もう布団の中から出たくない
//布団の外は寒すぎるから
//布団の中から出たくない
//布団の中はあたたかすぎるから
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define ii pair<ll,ll>
#define iii pair<ii,ll>
#define fi first
#define se second
#define endl '\n'
#define debug(x) cout << #x << ": " << x << endl
#define pub push_back
#define pob pop_back
#define puf push_front
#define pof pop_front
#define lb lower_bound
#define ub upper_bound
#define rep(x,start,end) for(int x=(start)-((start)>(end));x!=(end)-((start)>(end));((start)<(end)?x++:x--))
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
int n,k;
vector<int> al[200005];
int arr[200005];
vector<int> pos[200005];
int pp[200005];
void dfs(int i,int p){
pp[i]=p;
for (auto it:al[i]){
if (it==p) continue;
dfs(it,i);
}
}
bool vis[200005];
bool vis2[200005];
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin.exceptions(ios::badbit | ios::failbit);
cin>>n>>k;
int a,b;
rep(x,1,n){
cin>>a>>b;
al[a].pub(b);
al[b].pub(a);
}
rep(x,1,n+1) cin>>arr[x];
rep(x,1,n+1) pos[arr[x]].pub(x);
int ans=1e9;
rep(x,1,n+1){
dfs(x,-1);
rep(x,1,n+1) vis[x]=vis2[x]=false;
vis[x]=true;
vector<int> stk={x};
int curr=0;
while (!stk.empty()){
int u=stk.back(); stk.pob();
if (!vis2[arr[u]]){
vis2[arr[u]]=true;
curr++;
for (auto it:pos[arr[u]]) if (!vis[it]){
vis[it]=true;
stk.pub(it);
}
}
if (pp[u]!=-1 && !vis[pp[u]]){
vis[pp[u]]=true;
stk.pub(pp[u]);
}
}
//cout<<x<<" "<<curr<<endl;
ans=min(ans,curr);
}
cout<<ans-1<<endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9684 KB |
Output is correct |
2 |
Correct |
6 ms |
9740 KB |
Output is correct |
3 |
Correct |
5 ms |
9744 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
6 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9668 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9732 KB |
Output is correct |
9 |
Correct |
5 ms |
9732 KB |
Output is correct |
10 |
Correct |
5 ms |
9716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9684 KB |
Output is correct |
2 |
Correct |
6 ms |
9740 KB |
Output is correct |
3 |
Correct |
5 ms |
9744 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
6 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9668 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9732 KB |
Output is correct |
9 |
Correct |
5 ms |
9732 KB |
Output is correct |
10 |
Correct |
5 ms |
9716 KB |
Output is correct |
11 |
Correct |
128 ms |
9812 KB |
Output is correct |
12 |
Correct |
126 ms |
9908 KB |
Output is correct |
13 |
Correct |
123 ms |
9912 KB |
Output is correct |
14 |
Correct |
123 ms |
9912 KB |
Output is correct |
15 |
Correct |
55 ms |
9912 KB |
Output is correct |
16 |
Correct |
74 ms |
9920 KB |
Output is correct |
17 |
Correct |
57 ms |
9912 KB |
Output is correct |
18 |
Correct |
59 ms |
9924 KB |
Output is correct |
19 |
Correct |
58 ms |
9812 KB |
Output is correct |
20 |
Correct |
58 ms |
9916 KB |
Output is correct |
21 |
Correct |
60 ms |
9940 KB |
Output is correct |
22 |
Correct |
59 ms |
9908 KB |
Output is correct |
23 |
Correct |
85 ms |
9940 KB |
Output is correct |
24 |
Correct |
63 ms |
9908 KB |
Output is correct |
25 |
Correct |
105 ms |
9948 KB |
Output is correct |
26 |
Correct |
103 ms |
9952 KB |
Output is correct |
27 |
Correct |
102 ms |
10028 KB |
Output is correct |
28 |
Correct |
89 ms |
9936 KB |
Output is correct |
29 |
Correct |
91 ms |
9940 KB |
Output is correct |
30 |
Correct |
90 ms |
9940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3086 ms |
30028 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9684 KB |
Output is correct |
2 |
Correct |
6 ms |
9740 KB |
Output is correct |
3 |
Correct |
5 ms |
9744 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
6 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9668 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9732 KB |
Output is correct |
9 |
Correct |
5 ms |
9732 KB |
Output is correct |
10 |
Correct |
5 ms |
9716 KB |
Output is correct |
11 |
Correct |
128 ms |
9812 KB |
Output is correct |
12 |
Correct |
126 ms |
9908 KB |
Output is correct |
13 |
Correct |
123 ms |
9912 KB |
Output is correct |
14 |
Correct |
123 ms |
9912 KB |
Output is correct |
15 |
Correct |
55 ms |
9912 KB |
Output is correct |
16 |
Correct |
74 ms |
9920 KB |
Output is correct |
17 |
Correct |
57 ms |
9912 KB |
Output is correct |
18 |
Correct |
59 ms |
9924 KB |
Output is correct |
19 |
Correct |
58 ms |
9812 KB |
Output is correct |
20 |
Correct |
58 ms |
9916 KB |
Output is correct |
21 |
Correct |
60 ms |
9940 KB |
Output is correct |
22 |
Correct |
59 ms |
9908 KB |
Output is correct |
23 |
Correct |
85 ms |
9940 KB |
Output is correct |
24 |
Correct |
63 ms |
9908 KB |
Output is correct |
25 |
Correct |
105 ms |
9948 KB |
Output is correct |
26 |
Correct |
103 ms |
9952 KB |
Output is correct |
27 |
Correct |
102 ms |
10028 KB |
Output is correct |
28 |
Correct |
89 ms |
9936 KB |
Output is correct |
29 |
Correct |
91 ms |
9940 KB |
Output is correct |
30 |
Correct |
90 ms |
9940 KB |
Output is correct |
31 |
Execution timed out |
3086 ms |
30028 KB |
Time limit exceeded |
32 |
Halted |
0 ms |
0 KB |
- |