#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
#define all(x) (x).begin(),(x).end()
#define X first
#define Y second
#define sep ' '
#define endl '\n'
#define debug(x) cerr << #x << ": " << x << endl;
const ll MAXN = 1e6 + 10;
const ll LOG = 20;
int n, m, t, tin[MAXN], tout[MAXN], S[MAXN], T[MAXN], Par[MAXN][LOG];
vector<int> adj[MAXN];
void dfs(int v, int p) {
tin[v] = ++t;
Par[v][0] = p;
for (int u : adj[v])
if (u != p)
dfs(u, v);
tout[v] = t;
}
inline bool par(int u, int v) {
return tin[u] <= tin[v] && tout[u] >= tout[v];
}
inline int LCA(int u, int v) {
if (par(u, v)) return u;
if (par(v, u)) return v;
for (int i = LOG - 1; i >= 0; i--)
if (Par[u][i] != 0 && !par(Par[u][i], v))
u = Par[u][i];
return Par[u][0];
}
inline bool in_path(int v, int a, int b) {
int lca = LCA(a, b);
if (!par(lca, v)) return false;
return par(v, a) || par(v, b);
}
inline int solve() {
t = 0;
for (int i = 0; i < n + 10; i++) adj[i].clear();
cin >> n;
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(1, 0);
for (int i = 1; i < LOG; i++)
for (int v = 1; v <= n; v++)
Par[v][i] = Par[Par[v][i - 1]][i - 1];
cin >> m;
for (int i = 1; i <= m; i++)
cin >> S[i] >> T[i];
vector<int> vec;
for (int i = 1; i <= m; i++)
vec.push_back(i);
do {
bool flag = true;
for (int i = 0; i < m; i++) {
for (int j = 0; j < i; j++)
flag &= (!in_path(T[vec[j]], S[vec[i]], T[vec[i]]));
for (int j = i + 1; j < m; j++)
flag &= (!in_path(S[vec[j]], S[vec[i]], T[vec[i]]));
}
if (flag) return cout << "Yes" << endl, 0;
} while (next_permutation(all(vec)));
cout << "No" << endl;
return 0;
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int q;
cin >> q;
while (q--) solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23764 KB |
Output is correct |
3 |
Correct |
12 ms |
23764 KB |
Output is correct |
4 |
Correct |
19 ms |
23780 KB |
Output is correct |
5 |
Correct |
28 ms |
23764 KB |
Output is correct |
6 |
Correct |
13 ms |
23888 KB |
Output is correct |
7 |
Correct |
15 ms |
23764 KB |
Output is correct |
8 |
Execution timed out |
5059 ms |
23764 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23836 KB |
Output is correct |
2 |
Correct |
11 ms |
23836 KB |
Output is correct |
3 |
Correct |
12 ms |
23764 KB |
Output is correct |
4 |
Correct |
12 ms |
23788 KB |
Output is correct |
5 |
Correct |
12 ms |
23764 KB |
Output is correct |
6 |
Correct |
12 ms |
23764 KB |
Output is correct |
7 |
Correct |
13 ms |
23812 KB |
Output is correct |
8 |
Correct |
13 ms |
23880 KB |
Output is correct |
9 |
Correct |
13 ms |
23796 KB |
Output is correct |
10 |
Correct |
12 ms |
23816 KB |
Output is correct |
11 |
Correct |
12 ms |
23844 KB |
Output is correct |
12 |
Correct |
12 ms |
23812 KB |
Output is correct |
13 |
Correct |
13 ms |
23824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23836 KB |
Output is correct |
2 |
Correct |
11 ms |
23836 KB |
Output is correct |
3 |
Correct |
12 ms |
23764 KB |
Output is correct |
4 |
Correct |
12 ms |
23788 KB |
Output is correct |
5 |
Correct |
12 ms |
23764 KB |
Output is correct |
6 |
Correct |
12 ms |
23764 KB |
Output is correct |
7 |
Correct |
13 ms |
23812 KB |
Output is correct |
8 |
Correct |
13 ms |
23880 KB |
Output is correct |
9 |
Correct |
13 ms |
23796 KB |
Output is correct |
10 |
Correct |
12 ms |
23816 KB |
Output is correct |
11 |
Correct |
12 ms |
23844 KB |
Output is correct |
12 |
Correct |
12 ms |
23812 KB |
Output is correct |
13 |
Correct |
13 ms |
23824 KB |
Output is correct |
14 |
Correct |
12 ms |
23780 KB |
Output is correct |
15 |
Correct |
12 ms |
23856 KB |
Output is correct |
16 |
Correct |
15 ms |
23824 KB |
Output is correct |
17 |
Correct |
14 ms |
23916 KB |
Output is correct |
18 |
Correct |
26 ms |
23892 KB |
Output is correct |
19 |
Correct |
12 ms |
23808 KB |
Output is correct |
20 |
Correct |
21 ms |
23892 KB |
Output is correct |
21 |
Correct |
18 ms |
23892 KB |
Output is correct |
22 |
Correct |
21 ms |
23892 KB |
Output is correct |
23 |
Correct |
24 ms |
23780 KB |
Output is correct |
24 |
Correct |
24 ms |
23840 KB |
Output is correct |
25 |
Correct |
33 ms |
23892 KB |
Output is correct |
26 |
Correct |
14 ms |
23884 KB |
Output is correct |
27 |
Correct |
20 ms |
23892 KB |
Output is correct |
28 |
Correct |
44 ms |
23808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23836 KB |
Output is correct |
2 |
Correct |
11 ms |
23836 KB |
Output is correct |
3 |
Correct |
12 ms |
23764 KB |
Output is correct |
4 |
Correct |
12 ms |
23788 KB |
Output is correct |
5 |
Correct |
12 ms |
23764 KB |
Output is correct |
6 |
Correct |
12 ms |
23764 KB |
Output is correct |
7 |
Correct |
13 ms |
23812 KB |
Output is correct |
8 |
Correct |
13 ms |
23880 KB |
Output is correct |
9 |
Correct |
13 ms |
23796 KB |
Output is correct |
10 |
Correct |
12 ms |
23816 KB |
Output is correct |
11 |
Correct |
12 ms |
23844 KB |
Output is correct |
12 |
Correct |
12 ms |
23812 KB |
Output is correct |
13 |
Correct |
13 ms |
23824 KB |
Output is correct |
14 |
Correct |
12 ms |
23780 KB |
Output is correct |
15 |
Correct |
12 ms |
23856 KB |
Output is correct |
16 |
Correct |
15 ms |
23824 KB |
Output is correct |
17 |
Correct |
14 ms |
23916 KB |
Output is correct |
18 |
Correct |
26 ms |
23892 KB |
Output is correct |
19 |
Correct |
12 ms |
23808 KB |
Output is correct |
20 |
Correct |
21 ms |
23892 KB |
Output is correct |
21 |
Correct |
18 ms |
23892 KB |
Output is correct |
22 |
Correct |
21 ms |
23892 KB |
Output is correct |
23 |
Correct |
24 ms |
23780 KB |
Output is correct |
24 |
Correct |
24 ms |
23840 KB |
Output is correct |
25 |
Correct |
33 ms |
23892 KB |
Output is correct |
26 |
Correct |
14 ms |
23884 KB |
Output is correct |
27 |
Correct |
20 ms |
23892 KB |
Output is correct |
28 |
Correct |
44 ms |
23808 KB |
Output is correct |
29 |
Execution timed out |
5061 ms |
23892 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23836 KB |
Output is correct |
2 |
Correct |
11 ms |
23836 KB |
Output is correct |
3 |
Correct |
12 ms |
23764 KB |
Output is correct |
4 |
Correct |
12 ms |
23788 KB |
Output is correct |
5 |
Correct |
12 ms |
23764 KB |
Output is correct |
6 |
Correct |
12 ms |
23764 KB |
Output is correct |
7 |
Correct |
13 ms |
23812 KB |
Output is correct |
8 |
Correct |
13 ms |
23880 KB |
Output is correct |
9 |
Correct |
13 ms |
23796 KB |
Output is correct |
10 |
Correct |
12 ms |
23816 KB |
Output is correct |
11 |
Correct |
12 ms |
23844 KB |
Output is correct |
12 |
Correct |
12 ms |
23812 KB |
Output is correct |
13 |
Correct |
13 ms |
23824 KB |
Output is correct |
14 |
Correct |
12 ms |
23780 KB |
Output is correct |
15 |
Correct |
12 ms |
23856 KB |
Output is correct |
16 |
Correct |
15 ms |
23824 KB |
Output is correct |
17 |
Correct |
14 ms |
23916 KB |
Output is correct |
18 |
Correct |
26 ms |
23892 KB |
Output is correct |
19 |
Correct |
12 ms |
23808 KB |
Output is correct |
20 |
Correct |
21 ms |
23892 KB |
Output is correct |
21 |
Correct |
18 ms |
23892 KB |
Output is correct |
22 |
Correct |
21 ms |
23892 KB |
Output is correct |
23 |
Correct |
24 ms |
23780 KB |
Output is correct |
24 |
Correct |
24 ms |
23840 KB |
Output is correct |
25 |
Correct |
33 ms |
23892 KB |
Output is correct |
26 |
Correct |
14 ms |
23884 KB |
Output is correct |
27 |
Correct |
20 ms |
23892 KB |
Output is correct |
28 |
Correct |
44 ms |
23808 KB |
Output is correct |
29 |
Execution timed out |
5061 ms |
23892 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
11 ms |
23732 KB |
Output is correct |
3 |
Correct |
12 ms |
23828 KB |
Output is correct |
4 |
Correct |
13 ms |
23764 KB |
Output is correct |
5 |
Execution timed out |
5062 ms |
23824 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23764 KB |
Output is correct |
3 |
Correct |
12 ms |
23764 KB |
Output is correct |
4 |
Correct |
19 ms |
23780 KB |
Output is correct |
5 |
Correct |
28 ms |
23764 KB |
Output is correct |
6 |
Correct |
13 ms |
23888 KB |
Output is correct |
7 |
Correct |
15 ms |
23764 KB |
Output is correct |
8 |
Execution timed out |
5059 ms |
23764 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |