답안 #342525

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
342525 2021-01-02T10:16:11 Z leinad2 Spring cleaning (CEOI20_cleaning) C++17
17 / 100
131 ms 13420 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)
{
    p[v]=par;
    depth[v]=d;
    sz[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];
    }
    sz[v]%=2;
}
void dfs2(int v, int par)
{
    sum[v]=sum[par]+sz[v];
    for(int i=0;i<adj[v].size();i++)
    {
        int p=adj[v][i];
        if(p==par)continue;
        dfs2(p, 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);
    dfs2(r, 0);
    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\n", 2*sum[a]-depth[a]+ans+1);
        }
    }
}

Compilation message

cleaning.cpp: In function 'void dfs(long long int, long long int, long long int)':
cleaning.cpp:16: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]
   16 |     for(int i=0;i<adj[v].size();i++)
      |                 ~^~~~~~~~~~~~~~
cleaning.cpp: In function 'void dfs2(long long int, long long int)':
cleaning.cpp:28: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]
   28 |     for(int i=0;i<adj[v].size();i++)
      |                 ~^~~~~~~~~~~~~~
cleaning.cpp: At global scope:
cleaning.cpp:35:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   35 | main()
      |      ^
cleaning.cpp: In function 'int main()':
cleaning.cpp:37:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   37 |     for(scanf("%lld %lld", &n, &q);++i<n;)
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~
cleaning.cpp:39:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   39 |         scanf("%lld %lld", &a, &b);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~
cleaning.cpp:64:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   64 |         scanf("%lld %lld", &a, &a);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 2668 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 2924 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 4 ms 3444 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 12 ms 4972 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 70 ms 8052 KB Output is correct
2 Incorrect 60 ms 7532 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 111 ms 10104 KB Output is correct
2 Correct 122 ms 13420 KB Output is correct
3 Correct 127 ms 12652 KB Output is correct
4 Correct 131 ms 12056 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 2668 KB Output isn't correct
2 Halted 0 ms 0 KB -