답안 #376732

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
376732 2021-03-12T02:51:14 Z daniel920712 Mergers (JOI19_mergers) C++14
0 / 100
73 ms 32228 KB
#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];
int F(int here,int fa,int x)
{
    if(x==0) a.insert(color[here]);
    else b.insert(color[here]);
    for(auto i:Next[here])
    {
        if(i==fa) continue;
        F(i,here,x);
    }
}
int F2(int here,int fa,int xx)
{
    //printf("%d %d\n",here,xx);
    for(auto i:Next[here])
    {
        if(i==fa) continue;
        if(road.find(make_pair(here,i))!=road.end())
        {
            now++;
            deg[xx]++;
            deg[now]++;
            F2(i,here,now);
        }
        else F2(i,here,xx);
    }
}
int main()
{
    int N,M,i,x,y,con=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]);
    for(i=1;i<=N;i++)
    {
        for(auto j:Next[i])
        {
            if(i<j)
            {
                ok=1;
                a.clear();
                b.clear();
                F(i,j,0);
                F(j,i,1);
                for(auto k:a) if(b.find(k)!=b.end()) ok=0;
                for(auto k:b) if(a.find(k)!=a.end()) ok=0;
                //for(auto k:a) printf("aa %d\n",k);
                //for(auto k:b) printf("bb %d\n",k);
                if(ok)
                {
                    //printf("%d %d\n",i,j);
                    road.insert(make_pair(i,j));
                    road.insert(make_pair(j,i));
                }
            }

        }
    }
    now=0;
    F2(1,-1,0);
    for(i=0;i<=now;i++)
    {
        if(deg[i]==1) con++;
    }
    if(con==1) printf("0\n");
    else printf("%d\n",(con+1)/2);
    return 0;
}

Compilation message

mergers.cpp: In function 'int F(int, int, int)':
mergers.cpp:23:1: warning: no return statement in function returning non-void [-Wreturn-type]
   23 | }
      | ^
mergers.cpp: In function 'int F2(int, int, int)':
mergers.cpp:39:1: warning: no return statement in function returning non-void [-Wreturn-type]
   39 | }
      | ^
mergers.cpp: In function 'int main()':
mergers.cpp:43:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   43 |     scanf("%d %d",&N,&M);
      |     ~~~~~^~~~~~~~~~~~~~~
mergers.cpp:46:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   46 |         scanf("%d %d",&x,&y);
      |         ~~~~~^~~~~~~~~~~~~~~
mergers.cpp:50:28: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   50 |     for(i=1;i<=N;i++) scanf("%d",&color[i]);
      |                       ~~~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Runtime error 25 ms 24300 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 25 ms 24300 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 25 ms 24300 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 73 ms 32228 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 25 ms 24300 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -