#include <iostream>
#include <vector>
#include <functional>
using namespace std;
int main(){
cin.tie(0)->sync_with_stdio(0);
int t; cin >> t;
while (t--){
int n; cin >> n;
vector<vector<int>> g(n);
for (int i = 0; i < n - 1; i++){
int u, v; cin >> u >> v;
u--, v--;
g[u].push_back(v);
g[v].push_back(u);
}
vector<bool> vis(n);
vector<int> dep(n);
vector<vector<int>> up(n, vector<int>(__lg(n) + 1));
function<void(int)> dfs = [&](int u){
vis[u] = 1;
for (auto &v: g[u]){
if (vis[v]) continue;
up[v][0] = u;
dep[v] = dep[u] + 1;
dfs(v);
}
vis[u] = 0;
};
dfs(0);
for (int j = 1; j <= __lg(n); j++){
for (int i = 0; i < n; i++){
up[i][j] = up[up[i][j - 1]][j - 1];
}
}
auto lca = [&](int u, int v){
if (dep[u] < dep[v]) swap(u, v);
for (int i = __lg(n); i >= 0; i--){
if (dep[up[u][i]] >= dep[v]){
u = up[u][i];
}
}
if (u == v) return u;
for (int i = __lg(n); i >= 0; i--){
if (up[u][i] != up[v][i]){
u = up[u][i];
v = up[v][i];
}
}
return up[u][0];
};
vector<pair<int, int>> tag(n);
for (int i = 0; i < n; i++){
tag[i] = make_pair(i, i);
}
int q; cin >> q;
vector<pair<int, int>> qry(q);
for (int i = 0; i < q; i++){
int u, v; cin >> u >> v;
u--, v--;
qry[i] = make_pair(u, v);
}
auto dis = [&](int u, int v){
return dep[u] + dep[v] - 2 * dep[lca(u, v)];
};
bool flag = 0;
for (int i = 0; i < q; i++){
auto &[u, v] = qry[i];
for (int j = i + 1; j < q; j++){
auto &[uu, vv] = qry[j];
if (dis(u, uu) + dis(uu, v) == dis(u, v) && dis(u, vv) + dis(vv, v) == dis(u, v))
flag = 1;
if (dis(uu, u) + dis(u, vv) == dis(uu, vv) && dis(uu, v) + dis(v, vv) == dis(uu, vv))
flag = 1;
if (dis(u, uu) + dis(uu, v) == dis(u, v) && dis(uu, u) + dis(u, vv) == dis(uu, vv))
flag = 1;
if (dis(u, vv) + dis(vv, v) == dis(u, v) && dis(uu, v) + dis(v, vv) == dis(uu, vv))
flag = 1;
}
}
cout << (!flag? "Yes": "No") << '\n';
/*
while (q--){
int u, v; cin >> u >> v;
u--, v--;
int l = lca(u, v);
if (dep[l] < dep[tag[u].first]){
tag[u].first = l;
}
if (dep[l] < dep[tag[v].second]){
tag[v].second = l;
}
}
// cout << endl;
// for (int i = 0; i < n; i++){
// cout << tag[i].first << ' ' << tag[i].second << endl;
// }
// cout << endl;
bool flag = 0;
function<void(int)> solve = [&](int u){
vis[u] = 1;
pair<int, int> res{u, u};
for (auto &v: g[u]){
if (vis[v]) continue;
solve(v);
if (dep[tag[v].first] <= dep[u] && dep[tag[v].second] <= dep[u])
flag = 1;
if ((tag[u].first != u && dep[tag[v].first] < dep[tag[u].first]) \
|| (tag[u].second != u && dep[tag[v].second] < dep[tag[u].second]))
flag = 1;
if (dep[tag[v].first] <= dep[res.first])
res.first = tag[v].first;
if (dep[tag[v].second] <= dep[res.second])
res.second = tag[v].second;
}
if (dep[res.first] <= dep[tag[u].first])
tag[u].first = res.first;
if (dep[res.second] <= dep[tag[u].second])
tag[u].second = res.second;
vis[u] = 0;
};
solve(0);
cout << (!flag? "Yes": "No") << '\n';
*/
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
17 ms |
336 KB |
Output is correct |
5 |
Correct |
37 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
23 ms |
376 KB |
Output is correct |
9 |
Correct |
1286 ms |
1948 KB |
Output is correct |
10 |
Correct |
173 ms |
31756 KB |
Output is correct |
11 |
Correct |
10 ms |
328 KB |
Output is correct |
12 |
Correct |
260 ms |
352 KB |
Output is correct |
13 |
Execution timed out |
5035 ms |
32140 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
3 ms |
340 KB |
Output is correct |
4 |
Correct |
3 ms |
340 KB |
Output is correct |
5 |
Correct |
3 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
332 KB |
Output is correct |
7 |
Correct |
2 ms |
328 KB |
Output is correct |
8 |
Correct |
2 ms |
340 KB |
Output is correct |
9 |
Correct |
3 ms |
396 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
11 |
Correct |
3 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
3 ms |
340 KB |
Output is correct |
4 |
Correct |
3 ms |
340 KB |
Output is correct |
5 |
Correct |
3 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
332 KB |
Output is correct |
7 |
Correct |
2 ms |
328 KB |
Output is correct |
8 |
Correct |
2 ms |
340 KB |
Output is correct |
9 |
Correct |
3 ms |
396 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
11 |
Correct |
3 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Incorrect |
1 ms |
324 KB |
Output isn't correct |
15 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
3 ms |
340 KB |
Output is correct |
4 |
Correct |
3 ms |
340 KB |
Output is correct |
5 |
Correct |
3 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
332 KB |
Output is correct |
7 |
Correct |
2 ms |
328 KB |
Output is correct |
8 |
Correct |
2 ms |
340 KB |
Output is correct |
9 |
Correct |
3 ms |
396 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
11 |
Correct |
3 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Incorrect |
1 ms |
324 KB |
Output isn't correct |
15 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
3 ms |
340 KB |
Output is correct |
4 |
Correct |
3 ms |
340 KB |
Output is correct |
5 |
Correct |
3 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
332 KB |
Output is correct |
7 |
Correct |
2 ms |
328 KB |
Output is correct |
8 |
Correct |
2 ms |
340 KB |
Output is correct |
9 |
Correct |
3 ms |
396 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
11 |
Correct |
3 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Incorrect |
1 ms |
324 KB |
Output isn't correct |
15 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
17 ms |
336 KB |
Output is correct |
5 |
Correct |
37 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
23 ms |
376 KB |
Output is correct |
9 |
Correct |
1286 ms |
1948 KB |
Output is correct |
10 |
Correct |
173 ms |
31756 KB |
Output is correct |
11 |
Correct |
10 ms |
328 KB |
Output is correct |
12 |
Correct |
260 ms |
352 KB |
Output is correct |
13 |
Execution timed out |
5035 ms |
32140 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |