# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
361518 | inwbear | Capital City (JOI20_capital_city) | C++14 | 148 ms | 16108 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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()==0)st=i;
nn[t[i]]++;
}
gen(st,0,1);
for(int i=1;i<=n;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)
{
break;
}
if(0==mp[arr[i]]++)
{
t1++;
}
if(nn[arr[i]]==mp[arr[i]])
{
t2++;
}
if(t1==t2)
{
ans=min(ans,t1-1);
break;
}
}
}
}
printf("%d",ans);
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |