#include <bits/stdc++.h>
using namespace std;
#define fileio() freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout)
#define fastio() cin.tie(0), ios_base::sync_with_stdio(0)
#define pb push_back
#define sp " "
#define endl "\n"
#define pii pair<int, int>
#define st first
#define nd second
#define N 100005
vector<int> adj[N];
int val[N], par[N], deg[N];
int ans;
int dfs(int node, int root)
{
int flag = 0;
par[node] = root;
for (auto i : adj[node])
{
if (i != root) flag ^= dfs(i, node);
}
if (adj[node].size() == 1) flag ^= 1;
val[node] = flag;
if (flag == 0) val[node] = 2;
ans += val[node];
return flag;
}
int32_t main()
{
//fastio();
int n, q;
cin>>n>>q;
for (int i = 1; i < n; i++)
{
int u, v;
cin>>u>>v;
adj[u].pb(v);
adj[v].pb(u);
}
int flag = dfs(1, 0);
for (int i = 1; i <= n; i++)
deg[i] = adj[i].size();
ans -= val[1];
//cout<<flag<<sp<<ans<<endl;
while(q--)
{
int d;
cin>>d;
vector<int> v;
for (int i = 0; i < d; i++)
{
int node;
cin>>node;
if(deg[node] != 1)
{
flag ^= 1;
int curr = node;
while(curr != 1)
{
ans -= val[curr];
val[curr] = 3 - val[curr];
ans += val[curr];
curr = par[curr];
}
}
deg[node]++;
v.pb(node);
}
if (flag) cout<<-1<<endl;
else cout<<ans + d<<endl;
for (auto node : v)
{
deg[node]--;
if(deg[node] != 1)
{
flag ^= 1;
int curr = node;
while(curr != 1)
{
ans -= val[curr];
val[curr] = 3 - val[curr];
ans += val[curr];
curr = par[curr];
}
}
}
}
cerr<<"time taken : "<<(float)clock() / CLOCKS_PER_SEC<<" seconds\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
54 ms |
3580 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
3444 KB |
Output is correct |
2 |
Correct |
22 ms |
3416 KB |
Output is correct |
3 |
Correct |
53 ms |
7388 KB |
Output is correct |
4 |
Correct |
58 ms |
6612 KB |
Output is correct |
5 |
Correct |
82 ms |
8088 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
978 ms |
3788 KB |
Output is correct |
2 |
Correct |
959 ms |
4160 KB |
Output is correct |
3 |
Correct |
558 ms |
14252 KB |
Output is correct |
4 |
Execution timed out |
1091 ms |
13388 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1100 ms |
4052 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
164 ms |
5920 KB |
Output is correct |
2 |
Correct |
94 ms |
6764 KB |
Output is correct |
3 |
Correct |
65 ms |
4988 KB |
Output is correct |
4 |
Correct |
91 ms |
6788 KB |
Output is correct |
5 |
Correct |
93 ms |
6744 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
261 ms |
8212 KB |
Output is correct |
2 |
Execution timed out |
1083 ms |
10580 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
54 ms |
3580 KB |
Output is correct |
3 |
Correct |
24 ms |
3444 KB |
Output is correct |
4 |
Correct |
22 ms |
3416 KB |
Output is correct |
5 |
Correct |
53 ms |
7388 KB |
Output is correct |
6 |
Correct |
58 ms |
6612 KB |
Output is correct |
7 |
Correct |
82 ms |
8088 KB |
Output is correct |
8 |
Correct |
978 ms |
3788 KB |
Output is correct |
9 |
Correct |
959 ms |
4160 KB |
Output is correct |
10 |
Correct |
558 ms |
14252 KB |
Output is correct |
11 |
Execution timed out |
1091 ms |
13388 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |