#include <bits/stdc++.h>
using namespace std;
const int nx=1e5+5;
int n, q, deg[nx], lvl[nx], u, v, t, res, rt, cnt[nx], lf;
vector<int> d[nx];
void dfs(int u, int p)
{
lvl[u]=lvl[p]+1;
if (d[u].size()==1) deg[u]++, lf++;
for (auto v:d[u]) if (v!=p) dfs(v, u), deg[u]+=deg[v];
if (deg[u]>2) deg[u]=((deg[u]%2)==1)?1:2;
if (u!=rt) res+=deg[u];
if (deg[u]==2&&u!=rt) cnt[u]=1;
}
void dfs2(int u, int p)
{
for (auto v:d[u]) if (v!=p) cnt[v]+=cnt[u], dfs2(v, u);
}
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>n>>q;
for (int i=1; i<n; i++) cin>>u>>v, d[u].push_back(v), d[v].push_back(u);
for (int i=1; i<=n; i++) if (d[i].size()>1) rt=i;
dfs(rt, rt);
dfs2(rt, rt);
while (q--)
{
cin>>t>>u;
if ((lf+(d[u].size()!=1))%2==1) cout<<-1<<'\n';
else cout<<res+(lvl[u]-2*cnt[u])<<'\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2652 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2908 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
3420 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
4188 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
30 ms |
6016 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
50 ms |
8020 KB |
Output is correct |
2 |
Incorrect |
60 ms |
12400 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2652 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |