#include <bits/stdc++.h>
#define int long long
#define pii pair<int,int>
#define all(x) x.begin(),x.end()
#define vi vector<int>
#define vii vector<pii>
#define siz(x) (int)x.size()
#define pb push_back
#define rep(i,x) for(int i=0; i<x; i++)
#define rep1(i,x) for(int i=1; i<=x; i++)
using namespace std;
const int inf = 1e9, maxn = 1e5+5, mod = 1e9+7;
int n,q,ileparzystych,ilelisci,korzen = 1;
int liscie[maxn];
bool parzyste[maxn];
int parent[maxn];
int aktsiz[maxn];
vi graf[maxn];
void DFS(int v, int p){
parent[v] = p;
if(siz(graf[v])==1){liscie[v] = 1;ilelisci++;}
for(auto u:graf[v])if(u!=p){
DFS(u,v);
liscie[v]+=liscie[u];
}
parzyste[v] = !(liscie[v]%2);
if(parzyste[v] && v!=korzen)ileparzystych++;
}
int32_t main(){
ios_base::sync_with_stdio(0);cin.tie(0);
cin>>n>>q;
rep(i,n-1){
int a,b;
cin>>a>>b;
graf[a].pb(b);
graf[b].pb(a);
aktsiz[a]++;
aktsiz[b]++;
}
DFS(korzen,0);
// cout<<"pr ileparzystych: "<<ileparzystych<<" ilelisci: "<<ilelisci<<" "; for(int i=1; i<=n; i++)cout<<aktsiz[i]<<' ';cout<<'\n';
rep(i,q){
int d;
cin>>d;
vi a(d);
rep(j,d){
cin>>a[j];
int x = a[j];
if(aktsiz[x] > 1)ilelisci++;
if(aktsiz[x] > 1)
while(x!=korzen){
parzyste[x] = !parzyste[x];
if(parzyste[x])ileparzystych++;
else ileparzystych--;
x = parent[x];
}
aktsiz[a[j]]++;
}
// cout<<"in ileparzystych: "<<ileparzystych<<" ilelisci: "<<ilelisci<<" "; for(int i=1; i<=n; i++)cout<<aktsiz[i]<<' ';cout<<'\n';
if((ilelisci)&1)cout<<-1<<'\n';
else cout<<n-1+d+ileparzystych<<'\n';
rep(j,d){
int x = a[j];
aktsiz[x]--;
if(aktsiz[x] > 1)ilelisci--;
if(aktsiz[x] > 1)
while(x!=korzen){
parzyste[x] = !parzyste[x];
if(parzyste[x])ileparzystych++;
else ileparzystych--;
x = parent[x];
}
}
// cout<<"po ileparzystych: "<<ileparzystych<<" ilelisci: "<<ilelisci<<" "; for(int i=1; i<=n; i++)cout<<aktsiz[i]<<' ';cout<<'\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4700 KB |
Output is correct |
2 |
Correct |
20 ms |
5556 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
5468 KB |
Output is correct |
2 |
Correct |
6 ms |
5476 KB |
Output is correct |
3 |
Correct |
17 ms |
9168 KB |
Output is correct |
4 |
Correct |
18 ms |
8400 KB |
Output is correct |
5 |
Correct |
32 ms |
9828 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
741 ms |
5724 KB |
Output is correct |
2 |
Correct |
744 ms |
5724 KB |
Output is correct |
3 |
Correct |
402 ms |
14532 KB |
Output is correct |
4 |
Execution timed out |
1042 ms |
14160 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1049 ms |
5976 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
7904 KB |
Output is correct |
2 |
Correct |
30 ms |
7772 KB |
Output is correct |
3 |
Correct |
22 ms |
7136 KB |
Output is correct |
4 |
Correct |
32 ms |
9040 KB |
Output is correct |
5 |
Correct |
30 ms |
9120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
9352 KB |
Output is correct |
2 |
Execution timed out |
1075 ms |
13148 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4700 KB |
Output is correct |
2 |
Correct |
20 ms |
5556 KB |
Output is correct |
3 |
Correct |
7 ms |
5468 KB |
Output is correct |
4 |
Correct |
6 ms |
5476 KB |
Output is correct |
5 |
Correct |
17 ms |
9168 KB |
Output is correct |
6 |
Correct |
18 ms |
8400 KB |
Output is correct |
7 |
Correct |
32 ms |
9828 KB |
Output is correct |
8 |
Correct |
741 ms |
5724 KB |
Output is correct |
9 |
Correct |
744 ms |
5724 KB |
Output is correct |
10 |
Correct |
402 ms |
14532 KB |
Output is correct |
11 |
Execution timed out |
1042 ms |
14160 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |