//#include "dungeon2.h"
#pragma GCC optimize("Ofast,unroll-loops")
#include<bits/stdc++.h>
#define int long long
#define ll long long
#define down cout<<'\n';
#define debug cout<<" cucuucucuuu",down
#define modwwe int t;cin>>t; while(t--)
#define bit(i,j) (i>>j&1)
#define sobit(a) __builtin_popcountll(a)
#define task2 "ftree"
#define task "test"
#define fin(x) freopen(x".inp","r",stdin)
#define fou(x) freopen(x".out","w",stdout)
#define pb push_back
#define mask(k) (1<<k)
#define checktime cerr << (double)clock() / CLOCKS_PER_SEC * 1000 << " ms";
using namespace std;
#define getchar_unlocked getchar
mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());
int rand(int l,int r)
{
return uniform_int_distribution<int>(l,r)(rd);
}
void phongbeo();
const int inf = 1e9;
const ll mod2 = 1e9+7;
const ll base=67;
int n, m, s1, s2, s4, s3, sf, k, s5, s6, mx, s7, s8, s9, mx2, res, dem2 = 0, dem = 0, s33, dem3, dem4, mid, l2, r2, center;
int i, s10, s12,k1,k2,k3,s11,lim,w,l,r,dem5,dem6,dem7,dem9;
int kk;
int t;
int el = 19;
main()
{
if(fopen(task2".inp","r"))
{
fin(task2);
fou(task2);
}
if(fopen(task".inp","r"))
{
fin(task);
fou(task);
}
ios_base::sync_with_stdio(0);
cout.tie(0);
cin.tie(0);
/// cin>>s1;
//int t;cin>>t; while(t--)
phongbeo();
checktime
}
int par[100001],g[100001];
int c[100001];
int dp[100001],heavy[100001],pos[100001],head[100001];
vector<int> v[100001];
int dfs(int x,int y)
{
par[x]=y;
dp[x]=(v[x].size()==1);
int sz=1,mxz=0;
for(auto f:v[x])
if(f^y)
{
int s=dfs(f,x);
dp[x]+=dp[f];
sz+=s;
if(s>mxz)
{
heavy[x]=f;
mxz=s;
}
}
return sz;
}
void des(int x,int y)
{
head[x]=y;
pos[x]=++dem;
g[dem]=x;
if(heavy[x]!=0)
{
des(heavy[x],y);
for(auto f:v[x])
if(pos[f]==0)
des(f,f);
}
}
struct segbidi
{
int t[400001][2];
int lazy[400001];
void mer(int x,int y,int z)
{
for(int j=0; j<2; j++)
t[x][j]=t[y][j]+t[z][j];
}
void build(int node,int l,int r)
{
if(l==r)
{
t[node][dp[g[l]]%2]++;
if(g[l]==1)t[node][0]=t[node][1]=0;
return;
}
int mid=l+r>>1;
build(node<<1,l,mid);
build(node<<1|1,mid+1,r);
mer(node,node<<1,node<<1|1);
}
void apply(int node)
{
swap(t[node][0],t[node][1]);
lazy[node]=1-lazy[node];
}
void ff(int x)
{
for(int i=x*2; i<=x*2+1; i++)
apply(i);
lazy[x]=0;
}
void upd(int node,int l,int r,int l1,int r1)
{
if(l>r1||r<l1) return;
if(l>=l1&&r<=r1)
{
apply(node);
return;
}
int mid=l+r>>1;
if(lazy[node]!=0)ff(node);
upd(node<<1,l,mid,l1,r1);
upd(node<<1|1,mid+1,r,l1,r1);
mer(node,node<<1,node<<1|1);
}
} st;
void upd(int x)
{
if(x==0) return;
st.upd(1,1,n,pos[head[x]],pos[x]);
upd(par[head[x]]);
}
void phongbeo()
{
cin>>n>>m;
for(int i=1; i<n; i++)
cin>>l>>r,v[l].pb(r),v[r].pb(l);
dfs(1,0);
des(1,1);
st.build(1,1,n);
while(m--)
{
vector<int> rollback;
cin>>k;
dem=0;
for(int i=1; i<=k; i++)
{
cin>>s2,rollback.pb(s2);
if(c[s2]==0&&v[s2].size()==1)
{
rollback.pb(s2);
dem++;
}
c[s2]=1;
}
if((dp[1]+k-dem)%2==1){ cout<<-1;
for(auto x:rollback)c[x]=0;
}
else
{
for(auto x:rollback)upd(x);
cout<<n-1+k+st.t[1][0];
for(auto x:rollback)upd(x),c[x]=0;
}
down
}
}
Compilation message (stderr)
cleaning.cpp:34:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
34 | main()
| ^~~~
cleaning.cpp: In function 'int main()':
cleaning.cpp:13:23: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
13 | #define fin(x) freopen(x".inp","r",stdin)
| ~~~~~~~^~~~~~~~~~~~~~~~~~~
cleaning.cpp:38:9: note: in expansion of macro 'fin'
38 | fin(task2);
| ^~~
cleaning.cpp:14:23: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
14 | #define fou(x) freopen(x".out","w",stdout)
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~
cleaning.cpp:39:9: note: in expansion of macro 'fou'
39 | fou(task2);
| ^~~
cleaning.cpp:13:23: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
13 | #define fin(x) freopen(x".inp","r",stdin)
| ~~~~~~~^~~~~~~~~~~~~~~~~~~
cleaning.cpp:43:9: note: in expansion of macro 'fin'
43 | fin(task);
| ^~~
cleaning.cpp:14:23: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
14 | #define fou(x) freopen(x".out","w",stdout)
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~
cleaning.cpp:44:9: note: in expansion of macro 'fou'
44 | fou(task);
| ^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |