Submission #210981

# Submission time Handle Problem Language Result Execution time Memory
210981 2020-03-19T04:52:58 Z KoalaMuch Mergers (JOI19_mergers) C++14
0 / 100
113 ms 33400 KB
#include<bits/stdc++.h>
using namespace std;
const int N = 5e5+5;
int p[N];
int belong[N];
int sz[N];
vector< int > g[N];
vector< int > state[N];
int fin(int i)  {return p[i]==i ? i : p[i]=fin(p[i]);}
int main()
{
    int n,k,u,v;
    scanf("%d %d",&n,&k);
    for(int i=1;i<n;i++)    scanf("%d %d",&u,&v),g[u].push_back(v),g[v].push_back(u);
    for(int i=1;i<=n;i++)   scanf("%d",&belong[i]),p[i] = i,sz[i] = 1;
    for(int i=1;i<=n;i++)
    {
        state[belong[i]].push_back(i);
        for(auto &x:g[i])
        {
            u = fin(i),v = fin(x);
            if(belong[u]==belong[v]&&u!=v)  sz[u]+=sz[v],p[v] = u;
        }
    }
    int ans = 0;
    for(int i=1;i<=k;i++)
    {
        if(state[i].size()==sz[fin(state[i][0])])
        {
            int cnt = 0;
            for(auto &x:state[i])
            {
                for(auto &y:g[x])
                {
                    cnt+=belong[y]!=i;
                }
            }
            if(cnt==1)  ++ans;
        }
    }
    printf("%d\n",(ans+1)>>1);
    return 0;
}

Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:28:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         if(state[i].size()==sz[fin(state[i][0])])
            ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
mergers.cpp:13:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d",&n,&k);
     ~~~~~^~~~~~~~~~~~~~~
mergers.cpp:14:67: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     for(int i=1;i<n;i++)    scanf("%d %d",&u,&v),g[u].push_back(v),g[v].push_back(u);
                             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~
mergers.cpp:15:60: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     for(int i=1;i<=n;i++)   scanf("%d",&belong[i]),p[i] = i,sz[i] = 1;
                             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 17 ms 23808 KB Output is correct
2 Incorrect 18 ms 23808 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 17 ms 23808 KB Output is correct
2 Incorrect 18 ms 23808 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 17 ms 23808 KB Output is correct
2 Incorrect 18 ms 23808 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 76 ms 30540 KB Output is correct
2 Correct 113 ms 33400 KB Output is correct
3 Incorrect 21 ms 24040 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 17 ms 23808 KB Output is correct
2 Incorrect 18 ms 23808 KB Output isn't correct
3 Halted 0 ms 0 KB -