제출 #379677

#제출 시각아이디문제언어결과실행 시간메모리
379677daniel920712Mergers (JOI19_mergers)C++14
100 / 100
943 ms110052 KiB
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <set>
#include <vector>

using namespace std;
set < int > a,b;
//set < pair < int , int > > road;
int now=0;
vector < int > Next[500005];
int color[500005];
int deg[500005]={0};
int tt[500005]={0};
int Father[500005]={0};
vector < int > con[500005];
vector < int > use[500005];
int sum[500005]={0};
int M;
int root[500005];
int Find(int here)
{
    if(root[here]==here) return root[here];
    root[here]=Find(root[here]);
    return root[here];
}
void F2(int here,int fa,int xx)
{
    for(auto i:Next[here])
    {
        if(i==fa) continue;
        if(Find(here)!=Find(i))
        {
            now++;
            deg[xx]++;
            deg[now]++;
            //printf("%d %d %d %d\n",here,i,deg[xx],deg[now]);
            F2(i,here,now);
        }
        else F2(i,here,xx);
    }
}
void F4(int here,int fa,int deg)
{
    tt[here]=deg;
    Father[here]=fa;
    for(auto i:Next[here])
    {
        if(i!=fa)
        {
            //road.insert(make_pair(here,i));
            F4(i,here,deg+1);
        }
    }
}
int LCA(int a,int b)
{
    a=Find(a);
    b=Find(b);
    if(a==b) return a;
    int x,y,t;
    if(tt[a]==tt[b])
    {
        x=Father[a];
        y=Father[b];
        t=LCA(x,y);
        root[Find(a)]=t;
        root[Find(b)]=t;
        return t;
    }
    else
    {
        if(tt[a]<tt[b]) swap(a,b);
        x=Father[a];
        //if(road.find(make_pair(x,a))!=road.end()) road.erase(make_pair(x,a));
        t=LCA(x,b);
        root[Find(a)]=t;
        return t;
    }
}
int main()
{
    //freopen("01-04.txt","rt",stdin);
    int N,K,i,j,x,y,xx=0,ok=1;
    scanf("%d %d",&N,&M);
    for(i=1;i<N;i++)
    {
        scanf("%d %d",&x,&y);
        Next[x].push_back(y);
        Next[y].push_back(x);
    }
    for(i=1;i<=N;i++)
    {
        scanf("%d",&color[i]);
        root[i]=i;
        use[color[i]].push_back(i);
    }

    F4(1,-1,0);
    for(i=1;i<=M;i++)
    {
        K=use[i].size();
        if(K>=2)
        {
            x=use[i][0];
            for(j=1;j<K;j++) x=LCA(x,use[i][j]);
        }
    }
    now=0;
    F2(1,-1,0);
    for(i=0;i<=now;i++) if(deg[i]==1) xx++;

    if(xx==1) printf("0\n");
    else printf("%d\n",(xx+1)/2);
    return 0;
}

컴파일 시 표준 에러 (stderr) 메시지

mergers.cpp: In function 'int main()':
mergers.cpp:84:26: warning: unused variable 'ok' [-Wunused-variable]
   84 |     int N,K,i,j,x,y,xx=0,ok=1;
      |                          ^~
mergers.cpp:85:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   85 |     scanf("%d %d",&N,&M);
      |     ~~~~~^~~~~~~~~~~~~~~
mergers.cpp:88:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   88 |         scanf("%d %d",&x,&y);
      |         ~~~~~^~~~~~~~~~~~~~~
mergers.cpp:94:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   94 |         scanf("%d",&color[i]);
      |         ~~~~~^~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...