답안 #342508

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
342508 2021-01-02T09:42:12 Z leinad2 Spring cleaning (CEOI20_cleaning) C++17
0 / 100
1000 ms 9324 KB
#include<bits/stdc++.h>
using namespace std;
int n, i, j, k, r, q, a, b, cnt, sz[100010], p[100010], ans;
vector<int>adj[100010];
void dfs(int v, int par)
{
    p[v]=par;
    sz[v]=0;
    if(adj[v].size()==1)
    {
        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);
        sz[v]+=sz[p];
    }
}
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);
    for(i=0;i++<n;)sz[i]%=2;
    for(i=0;i++<n;)
    {
        if(sz[i]%2==0)ans++;
    }
    while(q--)
    {
        vector<int>v;
        scanf("%d", &a);
        while(a--)
        {
            scanf("%d", &b);
            ans++;
            v.push_back(b);
            if(adj[b].size()==1)continue;
            while(1)
            {
                sz[b]=1-sz[b];
                if(sz[b]==0)ans++;
                else ans--;
                if(b==r)break;
                b=p[b];
            }
        }
        if(sz[r]%2)
        {
            puts("-1");
            goto w;
        }
        printf("%d\n", ans-1);
        w:;
        for(i=0;i<v.size();i++)
        {
            b=v[i];
            ans--;
            if(adj[b].size()==1)continue;
            while(1)
            {
                sz[b]=1-sz[b];
                if(sz[b]==0)ans++;
                else ans--;
                if(b==r)break;
                b=p[b];
            }
        }
    }
}

Compilation message

cleaning.cpp: In function 'void dfs(int, int)':
cleaning.cpp:14:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   14 |     for(int i=0;i<adj[v].size();i++)
      |                 ~^~~~~~~~~~~~~~
cleaning.cpp: In function 'int main()':
cleaning.cpp:71:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   71 |         for(i=0;i<v.size();i++)
      |                 ~^~~~~~~~~
cleaning.cpp:24:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   24 |     for(scanf("%d %d", &n, &q);++i<n;)
      |         ~~~~~^~~~~~~~~~~~~~~~~
cleaning.cpp:26:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   26 |         scanf("%d %d", &a, &b);
      |         ~~~~~^~~~~~~~~~~~~~~~~
cleaning.cpp:48:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   48 |         scanf("%d", &a);
      |         ~~~~~^~~~~~~~~~
cleaning.cpp:51:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   51 |             scanf("%d", &b);
      |             ~~~~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2668 KB Output is correct
2 Incorrect 35 ms 3564 KB Output isn't correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 13 ms 3440 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1089 ms 4000 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1096 ms 4204 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 63 ms 5612 KB Output is correct
2 Incorrect 77 ms 5356 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 125 ms 7148 KB Output is correct
2 Execution timed out 1071 ms 9324 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2668 KB Output is correct
2 Incorrect 35 ms 3564 KB Output isn't correct