Submission #888422

#TimeUsernameProblemLanguageResultExecution timeMemory
8884228pete8Spring cleaning (CEOI20_cleaning)C++17
0 / 100
1068 ms12628 KiB
#include<iostream> #include<stack> #include<map> #include<vector> #include<string> #include<unordered_map> #include <queue> #include<cstring> #include <cassert> #include<limits.h> #include<cmath> #include<set> #include<algorithm> #include<bitset> #include <iomanip> #include <cstdio> #include <cstdlib> #include <cstring> using namespace std; #define ll long long #define f first #define endl "\n" #define s second #define pii pair<int,int> #define ppii pair<int,pii> #define vi vector<int> #define pb push_back //#define p push #define all(x) x.begin(),x.end() #define rall(x) x.rbegin(),x.rend() #define F(n) for(int i=0;i<n;i++) #define lb lower_bound #define ub upper_bound #define fastio ios::sync_with_stdio(false);cin.tie(NULL); using namespace std; #pragma GCC optimize ("03,unroll-loops") #define int long long const int mxn=1e5+5,inf=1e9,lg=30,mod=1e9+7; int n,q; vector<int>adj[mxn+10]; int val[mxn+10],ans=0,up[mxn+10],cnt=0; bool leaf[mxn+10]; void dfs(int cur,int p){ for(auto i:adj[cur]){ if(i==p)continue; dfs(i,cur); up[i]=cur; val[cur]+=val[i]; } if(val[cur]==0)leaf[cur]=true,cnt++; if(val[cur]==0||val[cur]%2)val[cur]=1; else val[cur]=2; if(cur!=1)ans+=val[cur]; } int32_t main(){ fastio cin>>n>>q; for(int i=0;i<n-1;i++){ int a,b;cin>>a>>b; adj[a].pb(b); adj[b].pb(a); } dfs(1,-1); if(adj[1].size()==1)cnt++,leaf[1]=true; while(q--){// brute force check idea int m;cin>>m; vector<int>v(m); for(int i=0;i<m;i++){ cin>>v[i]; int a=v[i]; if(!leaf[a]){ cnt++; while(a!=1){ if(val[a]==1)val[a]=2,ans++; else val[a]=1,ans--; a=up[a]; } } } cout<<((cnt%2)?-1:ans+m)<<'\n'; for(auto i:v){ int a=i; if(!leaf[a]){ cnt--; while(a!=1){ if(val[a]==1)val[a]=2,ans++; else val[a]=1,ans--; a=up[a]; } } } } return 0; }
#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...