Submission #342515

# Submission time Handle Problem Language Result Execution time Memory
342515 2021-01-02T10:01:27 Z leinad2 Spring cleaning (CEOI20_cleaning) C++17
0 / 100
103 ms 7916 KB
#include<bits/stdc++.h>
using namespace std;
int n, i, j, k, r, q, a, b, cnt, sz[100010], p[100010], ans, sum[100010], depth[100010];
vector<int>adj[100010];
void dfs(int v, int par, int d)
{
    p[v]=par;
    depth[v]=d;
    sz[v]=0;
    sum[v]=0;
    if(adj[v].size()==1)
    {
        sum[v]=sz[v]=1;
        return;
    }
    for(int i=0;i<adj[v].size();i++)
    {
        int p=adj[v][i];
        if(p==par)continue;
        dfs(p, v, d+1);
        sz[v]+=sz[p];
        sum[v]+=sz[p];
    }
    sz[v]%=2;
}
int main()
{
    for(scanf("%d %d", &n, &q);++i<n;)
    {
        scanf("%d %d", &a, &b);
        adj[a].push_back(b);
        adj[b].push_back(a);
    }
    ans=n-1;
    for(i=0;i++<n;)
    {
        if(adj[i].size()>1)
        {
            r=i;
            break;
        }
    }
    dfs(r, 0, 1);
    for(i=0;i++<n;)sz[i]%=2;
    for(i=0;i++<n;)
    {
        if(sz[i]%2==0)ans++;
    }
    ans--;
    while(q--)
    {
        cnt=n;
        vector<int>v;
        scanf("%d %d", &a, &a);
        if(sz[r]%2==0)
        {
            if(adj[a].size()!=1)
            {
                puts("-1");
                continue;
            }
        }
        else
        {
            if(adj[a].size()==1)
            {
                puts("-1");
                continue;
            }
        }
        if(adj[a].size()==1)printf("%d\n", ans+1);
        else
        {
            printf("%d", 2*sum[a]-depth[a]+ans);
        }
    }
}

Compilation message

cleaning.cpp: In function 'void dfs(int, int, int)':
cleaning.cpp:16:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   16 |     for(int i=0;i<adj[v].size();i++)
      |                 ~^~~~~~~~~~~~~~
cleaning.cpp: In function 'int main()':
cleaning.cpp:28:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   28 |     for(scanf("%d %d", &n, &q);++i<n;)
      |         ~~~~~^~~~~~~~~~~~~~~~~
cleaning.cpp:30:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   30 |         scanf("%d %d", &a, &b);
      |         ~~~~~^~~~~~~~~~~~~~~~~
cleaning.cpp:54:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   54 |         scanf("%d %d", &a, &a);
      |         ~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 2668 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 2796 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 3308 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 12 ms 4332 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 56 ms 6124 KB Output is correct
2 Incorrect 49 ms 5740 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 103 ms 7916 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 2668 KB Output isn't correct
2 Halted 0 ms 0 KB -