#include<bits/stdc++.h>
//#pragma GCC optimize("O3")
//#pragma GCC target ("sse4")
using namespace std;
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define MEM(x,a) memset((x),a,sizeof((x)))
#define F first
#define S second
#define imx INT_MAX
const long long MOD = (long long)(1e9+7);
const long long MMX = (long long)(1e18);
typedef long long LL;
typedef pair<int,int> pii;
int n,k,a,b,t[200005],nn[200005],st,ed,arr[200005],mx[200005],ans,t1,t2;
map<int,int>mp;
vector<int>v[200005];
void gen(int N,int pr,int cc)
{
arr[cc]=t[N];
for(int i=0;i<v[N].size();i++)
{
if(v[N][i]!=pr)
{
gen(v[N][i],N,cc+1);
}
}
return;
}
int main()
{
scanf("%d %d",&n,&k);
ans=k-1;
for(int i=1;i<n;i++)
{
scanf("%d %d",&a,&b);
v[a].pb(b);
v[b].pb(a);
}
for(int i=1;i<=n;i++)
{
scanf("%d",&t[i]);
if(v[i].size()==1)st=i;
}
gen(st,0,1);
for(int i=1;i<=n;i++)
{
//printf("[%d]\n",arr[i]);
nn[arr[i]]++;
mx[arr[i]]=i;
}
for(int i=1;i<=n;i++)
{
if(mx[arr[i]]==i)
{
t1=0,t2=0;
mp.clear();
for(int j=i;j>ed;j--)
{
if(mx[arr[j]]>j&&mp[arr[j]]==0)
{
break;
}
mp[arr[j]]++;
if(mp[arr[j]]==1)
{
t1++;
}
if(nn[arr[j]]==mp[arr[j]])
{
t2++;
}
//printf("[%d %d %d]\n",j,t1,t2);
if(t1==t2)
{
//printf("=%d %d\n",i,j,t1);
ans=min(ans,t1-1);
ed=i;
break;
}
}
}
}
printf("%d",ans);
}
Compilation message
capital_city.cpp: In function 'void gen(int, int, int)':
capital_city.cpp:21:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
21 | for(int i=0;i<v[N].size();i++)
| ~^~~~~~~~~~~~
capital_city.cpp: In function 'int main()':
capital_city.cpp:32:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
32 | scanf("%d %d",&n,&k);
| ~~~~~^~~~~~~~~~~~~~~
capital_city.cpp:36:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
36 | scanf("%d %d",&a,&b);
| ~~~~~^~~~~~~~~~~~~~~
capital_city.cpp:42:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
42 | scanf("%d",&t[i]);
| ~~~~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
4972 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
4972 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3063 ms |
23600 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
4972 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |