Submission #342520

# Submission time Handle Problem Language Result Execution time Memory
342520 2021-01-02T10:07:43 Z leinad2 Spring cleaning (CEOI20_cleaning) C++17
0 / 100
116 ms 9964 KB
#include<bits/stdc++.h>
#define int long long
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)
{
    sum[v]=sum[par];
    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]+=sum[p];
    }
    sz[v]%=2;
    sum[v]+=sz[v];
}
main()
{
    for(scanf("%lld %lld", &n, &q);++i<n;)
    {
        scanf("%lld %lld", &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("%lld %lld", &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("%lld\n", ans+1);
        else
        {
            printf("%lld", 2*sum[a]-depth[a]+ans);
        }
    }
}

Compilation message

cleaning.cpp: In function 'void dfs(long long int, long long int, long long int)':
cleaning.cpp:18:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   18 |     for(int i=0;i<adj[v].size();i++)
      |                 ~^~~~~~~~~~~~~~
cleaning.cpp: At global scope:
cleaning.cpp:29:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   29 | main()
      |      ^
cleaning.cpp: In function 'int main()':
cleaning.cpp:31:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   31 |     for(scanf("%lld %lld", &n, &q);++i<n;)
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~
cleaning.cpp:33:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   33 |         scanf("%lld %lld", &a, &b);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~
cleaning.cpp:57:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   57 |         scanf("%lld %lld", &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 2924 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 3436 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 13 ms 4972 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 63 ms 7916 KB Output is correct
2 Incorrect 44 ms 7532 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 116 ms 9964 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 -