// CF template, version 3.0
#include <bits/stdc++.h>
using namespace std;
#define improvePerformance ios_base::sync_with_stdio(false); cin.tie(0)
#define getTest int t; cin >> t
#define eachTest for (int _var=0;_var<t;_var++)
#define get(name) int (name); cin >> (name)
#define out(o) cout << (o)
#define getList(cnt, name) vector<int> (name); for (int _=0;_<(cnt);_++) { get(a); (name).push_back(a); }
#define sortl(name) sort((name).begin(), (name).end())
#define rev(name) reverse((name).begin(), (name).end())
#define forto(name, var) for (int (var) = 0; (var) < (name); (var)++)
#define decision(b) if (b){out("YES");}else{out("NO");}
//#define int long long int
template <typename T, typename I>
struct segtree {
int n;
vector<T> tree;
vector<I> initial;
T id;
segtree(int i_n, vector<I> i_initial, T i_id): n(i_n), initial(i_initial), id(i_id) {
tree.resize(4 * n);
}
T conquer(T left, T right) {
// write your conquer function
}
T value(I inp) {
// write your value function
}
void build(int v, int l, int r) {
if (l == r) tree[v] = value(initial[l]);
else {
int middle = (l + r) / 2;
build(2 * v, l, middle);
build(2 * v + 1, middle + 1, r);
tree[v] = conquer(tree[2 * v], tree[2 * v + 1]);
}
}
void upd(int v, int l, int r, int i, I x) {
if (l == r) tree[v] = value(x);
else {
int middle = (l + r) / 2;
if (middle >= i) upd(2 * v, l, middle, i, x);
else upd(2 * v + 1, middle + 1, r, i, x);
tree[v] = conquer(tree[2 * v], tree[2 * v + 1]);
}
}
T query(int v, int l, int r, int ql, int qr) {
if (ql <= l && r <= qr) return tree[v];
else if (r < ql || qr < l) return id;
int middle = (l + r) / 2;
T left = query(2 * v, l, middle, ql, qr);
T right = query(2 * v + 1, middle + 1, r, ql, qr);
return conquer(left, right);
}
};
// vector<int>
vector<vector<int>> adjList;
vector<int> parity;
vector<int> height;
vector<int> upto;
vector<int> children;
int tot = 0;
void dfs1(int v, int p, int h) {
height[v] = h;
for (int con: adjList[v]) {
if (con == p) continue;
children[v]++;
dfs1(con, v, h + 1);
if (parity[con] == 0) parity[v] = 1 - parity[v];
}
if (!children[v]) parity[v] = 1 - parity[v];
}
void dfs2(int v, int p, int sum) {
sum += parity[v];
tot += parity[v];
upto[v] = sum;
for (int con: adjList[v]) {
if (con == p) continue;
dfs2(con, v, sum);
}
}
signed main() {
improvePerformance;
//getTest;
//eachTest {
get(n);
get(q);
adjList.resize(n);
parity.resize(n, 1);
height.resize(n);
children.resize(n);
upto.resize(n);
forto(n - 1, i) {
get(a);
get(b);
a--;
b--;
adjList[a].push_back(b);
adjList[b].push_back(a);
}
int root = -1;
forto(n, i) {
if (adjList[i].size() != 1) {
dfs1(i, -1, 1);
dfs2(i, -1, 0);
root = i;
break;
}
}
forto(q, i) {
// assume d_i = 1
get(d);
get(x); // using the above assumption
x--;
int ans;
int par = parity[root];
if (children[x]) {
par = 1 - par;
ans = n - 1 + tot + height[x] - 2 * upto[x];
} else {
ans = n - 1 + tot;
}
if (par == 1) out(ans);
else out(-1);
out("\n");
}
//}
}
Compilation message
cleaning.cpp: In function 'int main()':
cleaning.cpp:10:23: warning: unnecessary parentheses in declaration of 'n' [-Wparentheses]
10 | #define get(name) int (name); cin >> (name)
| ^
cleaning.cpp:104:9: note: in expansion of macro 'get'
104 | get(n);
| ^~~
cleaning.cpp:10:23: warning: unnecessary parentheses in declaration of 'q' [-Wparentheses]
10 | #define get(name) int (name); cin >> (name)
| ^
cleaning.cpp:105:9: note: in expansion of macro 'get'
105 | get(q);
| ^~~
cleaning.cpp:15:35: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
15 | #define forto(name, var) for (int (var) = 0; (var) < (name); (var)++)
| ^
cleaning.cpp:111:9: note: in expansion of macro 'forto'
111 | forto(n - 1, i) {
| ^~~~~
cleaning.cpp:10:23: warning: unnecessary parentheses in declaration of 'a' [-Wparentheses]
10 | #define get(name) int (name); cin >> (name)
| ^
cleaning.cpp:112:13: note: in expansion of macro 'get'
112 | get(a);
| ^~~
cleaning.cpp:10:23: warning: unnecessary parentheses in declaration of 'b' [-Wparentheses]
10 | #define get(name) int (name); cin >> (name)
| ^
cleaning.cpp:113:13: note: in expansion of macro 'get'
113 | get(b);
| ^~~
cleaning.cpp:15:35: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
15 | #define forto(name, var) for (int (var) = 0; (var) < (name); (var)++)
| ^
cleaning.cpp:120:9: note: in expansion of macro 'forto'
120 | forto(n, i) {
| ^~~~~
cleaning.cpp:15:35: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
15 | #define forto(name, var) for (int (var) = 0; (var) < (name); (var)++)
| ^
cleaning.cpp:128:9: note: in expansion of macro 'forto'
128 | forto(q, i) {
| ^~~~~
cleaning.cpp:10:23: warning: unnecessary parentheses in declaration of 'd' [-Wparentheses]
10 | #define get(name) int (name); cin >> (name)
| ^
cleaning.cpp:130:13: note: in expansion of macro 'get'
130 | get(d);
| ^~~
cleaning.cpp:10:23: warning: unnecessary parentheses in declaration of 'x' [-Wparentheses]
10 | #define get(name) int (name); cin >> (name)
| ^
cleaning.cpp:131:13: note: in expansion of macro 'get'
131 | get(x); // using the above assumption
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
604 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
1116 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
2908 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
5900 KB |
Output is correct |
2 |
Incorrect |
19 ms |
5720 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
41 ms |
9232 KB |
Output is correct |
2 |
Correct |
43 ms |
12628 KB |
Output is correct |
3 |
Correct |
54 ms |
12624 KB |
Output is correct |
4 |
Correct |
48 ms |
12112 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |