답안 #361518

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
361518 2021-01-30T10:57:13 Z inwbear 수도 (JOI20_capital_city) C++14
0 / 100
148 ms 16108 KB
#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

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 Correct 3 ms 4972 KB Output is correct
2 Correct 4 ms 4972 KB Output is correct
3 Correct 3 ms 4972 KB Output is correct
4 Incorrect 3 ms 4972 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4972 KB Output is correct
2 Correct 4 ms 4972 KB Output is correct
3 Correct 3 ms 4972 KB Output is correct
4 Incorrect 3 ms 4972 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 148 ms 16108 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4972 KB Output is correct
2 Correct 4 ms 4972 KB Output is correct
3 Correct 3 ms 4972 KB Output is correct
4 Incorrect 3 ms 4972 KB Output isn't correct
5 Halted 0 ms 0 KB -