Submission #342524

#TimeUsernameProblemLanguageResultExecution timeMemory
342524leinad2Spring cleaning (CEOI20_cleaning)C++17
0 / 100
108 ms9964 KiB
#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", 2*sum[a]-depth[a]+ans+1); } } }

Compilation message (stderr)

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);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...