#include <bits/stdc++.h>
#define MAX 500005
#define MOD (ll)(1e9+7)
#define INF (ll)(1e18)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
int n, m, tt;
int parent[120005][25];
int s[120005], t[120005], dep[120005], visited[120005];
vector<int> graph[120005];
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> tt;
while(tt--) {
cin >> n;
for(int i = 1; i <= n; ++i) graph[i].clear();
for(int i = 1; i <= n; ++i) for(int j = 0; j <= 20; ++j) parent[i][j] = 0;
for(int i = 1; i <= n - 1; ++i) {
int a, b; cin >> a >> b;
graph[a].push_back(b);
graph[b].push_back(a);
}
cin >> m;
for(int i = 1; i <= m; ++i) cin >> s[i] >> t[i];
auto dfs = [&](auto&& self, int node, int par) -> void {
parent[node][0] = par; dep[node] = dep[par] + 1;
for(auto v : graph[node]) if(v != par) self(self, v, node);
};
dfs(dfs, 1, 0);
for(int j = 1; j < 20; ++j) for(int i = 1; i <= n; ++i) parent[i][j] = parent[parent[i][j - 1]][j - 1];
auto LCA = [&](int u, int v) -> int {
if(dep[u] < dep[v]) swap(u, v);
int diff = dep[u] - dep[v], j = 0;
while(diff) {
if(diff & 1) u = parent[u][j];
diff >>= 1; ++j;
}
if(u == v) return u;
for(int i = 19; i >= 0; --i) {
if(parent[u][i] != parent[v][i]) {
u = parent[u][i];
v = parent[v][i];
}
}
return parent[u][0];
};
auto path = [&](int s, int t) -> bool {
while(s != t) {
if(visited[s]) return false;
s = parent[s][0];
}
return !visited[s];
};
bool flag = false;
vector<int> v(m);
iota(v.begin(), v.end(), 1);
do {
bool tmp = true;
for(int i = 1; i <= n; ++i) visited[i] = 0;
for(auto i : v) visited[s[i]]++;
for(auto i : v) {
visited[s[i]]--;
int p = LCA(s[i], t[i]);
if(!path(s[i], p) || !path(t[i], p)) tmp = false;
visited[t[i]]++;
}
flag |= tmp;
} while(next_permutation(v.begin(), v.end()));
if(flag) cout << "Yes\n";
else cout << "No\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3156 KB |
Output is correct |
2 |
Correct |
2 ms |
3156 KB |
Output is correct |
3 |
Correct |
1 ms |
3156 KB |
Output is correct |
4 |
Correct |
9 ms |
3184 KB |
Output is correct |
5 |
Correct |
19 ms |
3916 KB |
Output is correct |
6 |
Correct |
2 ms |
3156 KB |
Output is correct |
7 |
Correct |
12 ms |
3160 KB |
Output is correct |
8 |
Execution timed out |
5034 ms |
3156 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3156 KB |
Output is correct |
2 |
Correct |
2 ms |
3156 KB |
Output is correct |
3 |
Correct |
3 ms |
3084 KB |
Output is correct |
4 |
Correct |
2 ms |
3156 KB |
Output is correct |
5 |
Correct |
3 ms |
3156 KB |
Output is correct |
6 |
Correct |
2 ms |
3156 KB |
Output is correct |
7 |
Correct |
3 ms |
3156 KB |
Output is correct |
8 |
Correct |
3 ms |
3156 KB |
Output is correct |
9 |
Correct |
2 ms |
3156 KB |
Output is correct |
10 |
Correct |
2 ms |
3156 KB |
Output is correct |
11 |
Correct |
2 ms |
3156 KB |
Output is correct |
12 |
Correct |
2 ms |
3156 KB |
Output is correct |
13 |
Correct |
2 ms |
3156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3156 KB |
Output is correct |
2 |
Correct |
2 ms |
3156 KB |
Output is correct |
3 |
Correct |
3 ms |
3084 KB |
Output is correct |
4 |
Correct |
2 ms |
3156 KB |
Output is correct |
5 |
Correct |
3 ms |
3156 KB |
Output is correct |
6 |
Correct |
2 ms |
3156 KB |
Output is correct |
7 |
Correct |
3 ms |
3156 KB |
Output is correct |
8 |
Correct |
3 ms |
3156 KB |
Output is correct |
9 |
Correct |
2 ms |
3156 KB |
Output is correct |
10 |
Correct |
2 ms |
3156 KB |
Output is correct |
11 |
Correct |
2 ms |
3156 KB |
Output is correct |
12 |
Correct |
2 ms |
3156 KB |
Output is correct |
13 |
Correct |
2 ms |
3156 KB |
Output is correct |
14 |
Correct |
1 ms |
3156 KB |
Output is correct |
15 |
Correct |
2 ms |
3156 KB |
Output is correct |
16 |
Correct |
13 ms |
3156 KB |
Output is correct |
17 |
Correct |
5 ms |
3156 KB |
Output is correct |
18 |
Correct |
15 ms |
3160 KB |
Output is correct |
19 |
Correct |
2 ms |
3156 KB |
Output is correct |
20 |
Correct |
14 ms |
3160 KB |
Output is correct |
21 |
Correct |
6 ms |
3156 KB |
Output is correct |
22 |
Correct |
8 ms |
3156 KB |
Output is correct |
23 |
Correct |
5 ms |
3156 KB |
Output is correct |
24 |
Correct |
4 ms |
3188 KB |
Output is correct |
25 |
Correct |
20 ms |
3220 KB |
Output is correct |
26 |
Correct |
7 ms |
3180 KB |
Output is correct |
27 |
Correct |
7 ms |
3156 KB |
Output is correct |
28 |
Correct |
6 ms |
3156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3156 KB |
Output is correct |
2 |
Correct |
2 ms |
3156 KB |
Output is correct |
3 |
Correct |
3 ms |
3084 KB |
Output is correct |
4 |
Correct |
2 ms |
3156 KB |
Output is correct |
5 |
Correct |
3 ms |
3156 KB |
Output is correct |
6 |
Correct |
2 ms |
3156 KB |
Output is correct |
7 |
Correct |
3 ms |
3156 KB |
Output is correct |
8 |
Correct |
3 ms |
3156 KB |
Output is correct |
9 |
Correct |
2 ms |
3156 KB |
Output is correct |
10 |
Correct |
2 ms |
3156 KB |
Output is correct |
11 |
Correct |
2 ms |
3156 KB |
Output is correct |
12 |
Correct |
2 ms |
3156 KB |
Output is correct |
13 |
Correct |
2 ms |
3156 KB |
Output is correct |
14 |
Correct |
1 ms |
3156 KB |
Output is correct |
15 |
Correct |
2 ms |
3156 KB |
Output is correct |
16 |
Correct |
13 ms |
3156 KB |
Output is correct |
17 |
Correct |
5 ms |
3156 KB |
Output is correct |
18 |
Correct |
15 ms |
3160 KB |
Output is correct |
19 |
Correct |
2 ms |
3156 KB |
Output is correct |
20 |
Correct |
14 ms |
3160 KB |
Output is correct |
21 |
Correct |
6 ms |
3156 KB |
Output is correct |
22 |
Correct |
8 ms |
3156 KB |
Output is correct |
23 |
Correct |
5 ms |
3156 KB |
Output is correct |
24 |
Correct |
4 ms |
3188 KB |
Output is correct |
25 |
Correct |
20 ms |
3220 KB |
Output is correct |
26 |
Correct |
7 ms |
3180 KB |
Output is correct |
27 |
Correct |
7 ms |
3156 KB |
Output is correct |
28 |
Correct |
6 ms |
3156 KB |
Output is correct |
29 |
Execution timed out |
5033 ms |
3160 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3156 KB |
Output is correct |
2 |
Correct |
2 ms |
3156 KB |
Output is correct |
3 |
Correct |
3 ms |
3084 KB |
Output is correct |
4 |
Correct |
2 ms |
3156 KB |
Output is correct |
5 |
Correct |
3 ms |
3156 KB |
Output is correct |
6 |
Correct |
2 ms |
3156 KB |
Output is correct |
7 |
Correct |
3 ms |
3156 KB |
Output is correct |
8 |
Correct |
3 ms |
3156 KB |
Output is correct |
9 |
Correct |
2 ms |
3156 KB |
Output is correct |
10 |
Correct |
2 ms |
3156 KB |
Output is correct |
11 |
Correct |
2 ms |
3156 KB |
Output is correct |
12 |
Correct |
2 ms |
3156 KB |
Output is correct |
13 |
Correct |
2 ms |
3156 KB |
Output is correct |
14 |
Correct |
1 ms |
3156 KB |
Output is correct |
15 |
Correct |
2 ms |
3156 KB |
Output is correct |
16 |
Correct |
13 ms |
3156 KB |
Output is correct |
17 |
Correct |
5 ms |
3156 KB |
Output is correct |
18 |
Correct |
15 ms |
3160 KB |
Output is correct |
19 |
Correct |
2 ms |
3156 KB |
Output is correct |
20 |
Correct |
14 ms |
3160 KB |
Output is correct |
21 |
Correct |
6 ms |
3156 KB |
Output is correct |
22 |
Correct |
8 ms |
3156 KB |
Output is correct |
23 |
Correct |
5 ms |
3156 KB |
Output is correct |
24 |
Correct |
4 ms |
3188 KB |
Output is correct |
25 |
Correct |
20 ms |
3220 KB |
Output is correct |
26 |
Correct |
7 ms |
3180 KB |
Output is correct |
27 |
Correct |
7 ms |
3156 KB |
Output is correct |
28 |
Correct |
6 ms |
3156 KB |
Output is correct |
29 |
Execution timed out |
5033 ms |
3160 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3156 KB |
Output is correct |
2 |
Correct |
1 ms |
3156 KB |
Output is correct |
3 |
Correct |
2 ms |
3156 KB |
Output is correct |
4 |
Correct |
2 ms |
3156 KB |
Output is correct |
5 |
Execution timed out |
5066 ms |
3296 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3156 KB |
Output is correct |
2 |
Correct |
2 ms |
3156 KB |
Output is correct |
3 |
Correct |
1 ms |
3156 KB |
Output is correct |
4 |
Correct |
9 ms |
3184 KB |
Output is correct |
5 |
Correct |
19 ms |
3916 KB |
Output is correct |
6 |
Correct |
2 ms |
3156 KB |
Output is correct |
7 |
Correct |
12 ms |
3160 KB |
Output is correct |
8 |
Execution timed out |
5034 ms |
3156 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |