#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], sum[N], dist[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];
//cout<<node<<sp<<flag<<endl;
return flag;
}
void dfs2(int node, int root, int d, int dd)
{
sum[node] = d + val[node];
dist[node] = dd;
for (auto i : adj[node])
{
if (i != root) dfs2(i, node, d + val[node], dd + 1);
}
}
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);
ans -= val[1];
dfs2(1, 0, -val[1], 0);
while(q--)
{
int d;
cin>>d;
int node;
cin>>node;
if (adj[node].size() != 1) flag ^= 1;
int tmp = ans + 1;
if (adj[node].size() != 1)
{
tmp -= sum[node];
tmp += 3 * dist[node] - sum[node];
}
if (flag) cout<<-1<<endl;
else cout<<tmp<<endl;
if (adj[node].size() != 1) flag ^= 1;
}
cerr<<"time taken : "<<(float)clock() / CLOCKS_PER_SEC<<" seconds\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2644 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
2772 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
3284 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
18 ms |
4308 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
161 ms |
6076 KB |
Output is correct |
2 |
Incorrect |
67 ms |
5984 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
254 ms |
7948 KB |
Output is correct |
2 |
Correct |
268 ms |
11656 KB |
Output is correct |
3 |
Correct |
254 ms |
11800 KB |
Output is correct |
4 |
Correct |
256 ms |
12952 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2644 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |