#include <bits/stdc++.h>
//#define int ll
#define MAX 1000001
#define INF INT_MAX
#define MOD 1000000007
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define ins insert
#define ff first
#define ss second
#define all(a) a.begin(),a.end()
#define lb(a,b) lower_bound(all(a),b)
#define ub(a,b) upper_bound(all(a),b)
#define sortv(a) sort(all(a))
#define outputar(a,b){\
for(int i=0;i<b;i++){\
cout << a[i] << " ";\
}\
cout << "\n";\
}
#define outputvec(a){\
for(auto x:a){\
cout << (int)x << " ";\
}\
cout << endl;\
}
#define reset(a,n,v){\
for(int i=0;i<n;i++){\
a[i]=v;\
}\
}
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef tuple<ll,ll,ll> tll;
typedef pair<ll,ll> pll;
typedef pair<int,int> pii;
typedef double db;
typedef long double ldb;
inline void USACO(string filename){
freopen((filename+".in").c_str(),"r",stdin);
freopen((filename+".out").c_str(),"w",stdout);
}
int n,q,t=1,m,k,x,y,z,x2,y2,z2,a[MAX],b[MAX],c[MAX],ans[MAX];
ll res;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
string s[MAX],str[1];
//int e[1001][1001];
//bool e1[1001][1001];
string s1,s2,s3;
const int mod = 998244353;
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
vector<vector<int>> g;
void dfs(int v,int prev){
c[v]=(g[v].size()==1);
for(auto x:g[v]){
if(x==prev){
continue;
}
dfs(x,v);
c[v]+=c[x];
}
res+=1-c[v]%2;
}
void solve(){
cin >> n >> q;
for(int i=0;i<n-1;i++){
cin >> a[i] >> b[i];
}
for(int i=0;i<q;i++){
cin >> m;
g.clear();
g.resize(n+m+1);
for(int j=1;j<=m;j++){
cin >> x;
g[x].pb(n+j);
g[n+j].pb(x);
}
for(int j=0;j<n-1;j++){
g[a[j]].pb(b[j]);
g[b[j]].pb(a[j]);
}
for(int j=1;j<=n+m;j++){
c[j]=0;
}
res=n+m-1;
dfs(1,-1);
if(c[1]%2){
cout << -1 << "\n";
}
else{
cout << res-1 << "\n";
}
}
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
//cin >> t;
ll cnt1=1;
while(t--){
solve();
}
}
Compilation message
cleaning.cpp: In function 'int main()':
cleaning.cpp:103:8: warning: unused variable 'cnt1' [-Wunused-variable]
103 | ll cnt1=1;
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
39512 KB |
Output is correct |
2 |
Execution timed out |
1059 ms |
43644 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
44888 KB |
Output is correct |
2 |
Correct |
20 ms |
44892 KB |
Output is correct |
3 |
Correct |
32 ms |
50560 KB |
Output is correct |
4 |
Correct |
38 ms |
53704 KB |
Output is correct |
5 |
Correct |
44 ms |
55676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
45724 KB |
Output is correct |
2 |
Correct |
21 ms |
45624 KB |
Output is correct |
3 |
Correct |
37 ms |
57936 KB |
Output is correct |
4 |
Correct |
52 ms |
62436 KB |
Output is correct |
5 |
Correct |
35 ms |
56656 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
612 ms |
44244 KB |
Output is correct |
2 |
Correct |
404 ms |
43628 KB |
Output is correct |
3 |
Correct |
571 ms |
42844 KB |
Output is correct |
4 |
Correct |
628 ms |
43352 KB |
Output is correct |
5 |
Correct |
612 ms |
44124 KB |
Output is correct |
6 |
Correct |
639 ms |
44672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1045 ms |
47952 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1012 ms |
50512 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
39512 KB |
Output is correct |
2 |
Execution timed out |
1059 ms |
43644 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |