#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define pii pair <int, int>
#define X first
#define Y second
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
void abc() {cout << endl;}
template <typename T, typename ...U> void abc(T a, U ...b) {
cout << a << ' ', abc(b...);
}
template <typename T> void printv(T l, T r) {
for (; l != r; ++l) cout << *l << " \n"[l + 1 == r];
}
template <typename A, typename B> istream& operator >> (istream& o, pair<A, B> &a) {
return o >> a.X >> a.Y;
}
template <typename A, typename B> ostream& operator << (ostream& o, pair<A, B> a) {
return o << '(' << a.X << ", " << a.Y << ')';
}
template <typename T> ostream& operator << (ostream& o, vector<T> a) {
bool is = false;
if (a.empty()) return o << "{}";
for (T i : a) {o << (is ? ' ' : '{'), is = true, o << i;}
return o << '}';
}
template <typename T> struct vv : vector <vector <T>> {
vv (int n, int m, T v) : vector <vector <T>> (n, vector <T>(m, v)) {}
vv (int n, int m) : vector <vector <T>> (n, vector <T>(m)) {}
vv () {}
};
template <typename T> struct vvv : vector <vv <T>> {
vvv (int n, int m, int k, T v) : vector <vv <T>> (n, vv <T>(m, k, v)) {}
vvv (int n, int m, int k) : vector <vv <T>> (n, vv <T>(m, k)) {}
vvv () {}
};
#ifdef Doludu
#define test(args...) abc("[" + string(#args) + "]", args)
#define owo freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout)
#else
#define test(args...) void(0)
#define owo ios::sync_with_stdio(false); cin.tie(0)
#endif
const int mod = 1e9 + 7, N = 130002;
vector <int> adj[N];
struct BinaryLifting {
// 0-index
vector <int> in, out, dep;
vector <vector <int>> jump;
int _t, n, lg;
BinaryLifting () = default;
BinaryLifting (int _n) : n(_n) {
lg = __lg(n) + 1, _t = 0;
jump.assign(n, vector <int> (lg, -1));
in.assign(n, 0), out.assign(n, 0), dep.assign(n, 0);
dfs(0, -1);
}
void dfs(int v, int pa) {
in[v] = _t++, jump[v][0] = pa;
dep[v] = ~pa ? dep[pa] + 1 : 0;
for (int i = 1; i < lg; ++i) {
int k = jump[v][i - 1];
jump[v][i] = ~k ? jump[k][i - 1] : k;
}
for (int u : adj[v]) if (u != pa) {
dfs(u, v);
}
out[v] = _t++;
}
bool anc(int u, int v) {
return in[u] <= in[v] && out[u] >= out[v];
}
int lca(int u, int v) {
if (anc(u, v))
return u;
if (anc(v, u))
return v;
for (int i = lg - 1; ~i; --i) {
int k = jump[u][i];
if (~k && !anc(k, v))
u = k;
}
return jump[u][0];
}
int lift(int u, int d) {
for (int i = 0; i < lg; ++i) if (d >> i & 1)
u = jump[u][i];
return u;
}
int dis(int u, int v) {
return dep[u] + dep[v] - dep[lca(u, v)] * 2;
}
};
void solve() {
int n, m;
cin >> n;
for (int i = 0, u, v; i < n - 1; ++i) {
cin >> u >> v, --u, --v;
adj[u].pb(v), adj[v].pb(u);
}
BinaryLifting solver(n);
cin >> m;
vector <int> u(m), v(m);
for (int i = 0; i < m; ++i) {
cin >> u[i] >> v[i], --u[i], --v[i];
}
vector <vector <int>> g(m);
for (int i = 0; i < m; ++i) for (int j = 0; j < m; ++j) if (i ^ j) {
// u[j] on (u[i], v[i]) => j -> i
// v[j] on (u[i], v[i]) => i -> j
int lca = solver.lca(u[i], v[i]);
if (lca == u[j] || (solver.anc(lca, u[j]) && solver.anc(u[j], u[i])) || (solver.anc(lca, u[j]) && solver.anc(u[j], v[i]))) {
g[i].pb(j);
}
if (lca == v[j] || (solver.anc(lca, v[j]) && solver.anc(v[j], u[i])) || (solver.anc(lca, v[j]) && solver.anc(v[j], v[i]))) {
g[j].pb(i);
}
}
vector <int> in(m);
for (int i = 0; i < m; ++i) for (int j : g[i])
in[j]++;
queue <int> q;
for (int i = 0; i < m; ++i) if (!in[i])
q.push(i);
int ans = 0;
while (!q.empty()) {
int v = q.front(); q.pop();
ans++;
for (int u : g[v]) {
in[u]--;
if (!in[u])
q.push(u);
}
}
cout << (ans == m ? "Yes\n" : "No\n");
for (int i = 0; i < n; ++i)
adj[i].clear();
}
int main () {
owo;
int t = 1;
cin >> t;
while (t--)
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3284 KB |
Output is correct |
2 |
Correct |
2 ms |
3284 KB |
Output is correct |
3 |
Correct |
2 ms |
3284 KB |
Output is correct |
4 |
Correct |
12 ms |
3524 KB |
Output is correct |
5 |
Correct |
24 ms |
3516 KB |
Output is correct |
6 |
Correct |
3 ms |
3412 KB |
Output is correct |
7 |
Correct |
3 ms |
3540 KB |
Output is correct |
8 |
Correct |
7 ms |
3412 KB |
Output is correct |
9 |
Correct |
90 ms |
5656 KB |
Output is correct |
10 |
Correct |
67 ms |
26700 KB |
Output is correct |
11 |
Correct |
9 ms |
3284 KB |
Output is correct |
12 |
Correct |
76 ms |
3484 KB |
Output is correct |
13 |
Execution timed out |
5093 ms |
34836 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3284 KB |
Output is correct |
2 |
Correct |
2 ms |
3284 KB |
Output is correct |
3 |
Correct |
3 ms |
3412 KB |
Output is correct |
4 |
Correct |
3 ms |
3284 KB |
Output is correct |
5 |
Correct |
3 ms |
3412 KB |
Output is correct |
6 |
Correct |
3 ms |
3284 KB |
Output is correct |
7 |
Correct |
3 ms |
3412 KB |
Output is correct |
8 |
Correct |
3 ms |
3284 KB |
Output is correct |
9 |
Correct |
3 ms |
3284 KB |
Output is correct |
10 |
Correct |
3 ms |
3284 KB |
Output is correct |
11 |
Correct |
3 ms |
3412 KB |
Output is correct |
12 |
Correct |
2 ms |
3284 KB |
Output is correct |
13 |
Correct |
2 ms |
3284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3284 KB |
Output is correct |
2 |
Correct |
2 ms |
3284 KB |
Output is correct |
3 |
Correct |
3 ms |
3412 KB |
Output is correct |
4 |
Correct |
3 ms |
3284 KB |
Output is correct |
5 |
Correct |
3 ms |
3412 KB |
Output is correct |
6 |
Correct |
3 ms |
3284 KB |
Output is correct |
7 |
Correct |
3 ms |
3412 KB |
Output is correct |
8 |
Correct |
3 ms |
3284 KB |
Output is correct |
9 |
Correct |
3 ms |
3284 KB |
Output is correct |
10 |
Correct |
3 ms |
3284 KB |
Output is correct |
11 |
Correct |
3 ms |
3412 KB |
Output is correct |
12 |
Correct |
2 ms |
3284 KB |
Output is correct |
13 |
Correct |
2 ms |
3284 KB |
Output is correct |
14 |
Correct |
2 ms |
3252 KB |
Output is correct |
15 |
Correct |
2 ms |
3284 KB |
Output is correct |
16 |
Correct |
3 ms |
3284 KB |
Output is correct |
17 |
Correct |
3 ms |
3412 KB |
Output is correct |
18 |
Correct |
3 ms |
3284 KB |
Output is correct |
19 |
Correct |
2 ms |
3284 KB |
Output is correct |
20 |
Correct |
3 ms |
3284 KB |
Output is correct |
21 |
Correct |
3 ms |
3284 KB |
Output is correct |
22 |
Correct |
3 ms |
3412 KB |
Output is correct |
23 |
Correct |
2 ms |
3284 KB |
Output is correct |
24 |
Correct |
2 ms |
3348 KB |
Output is correct |
25 |
Correct |
3 ms |
3404 KB |
Output is correct |
26 |
Correct |
2 ms |
3284 KB |
Output is correct |
27 |
Correct |
3 ms |
3284 KB |
Output is correct |
28 |
Correct |
2 ms |
3284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3284 KB |
Output is correct |
2 |
Correct |
2 ms |
3284 KB |
Output is correct |
3 |
Correct |
3 ms |
3412 KB |
Output is correct |
4 |
Correct |
3 ms |
3284 KB |
Output is correct |
5 |
Correct |
3 ms |
3412 KB |
Output is correct |
6 |
Correct |
3 ms |
3284 KB |
Output is correct |
7 |
Correct |
3 ms |
3412 KB |
Output is correct |
8 |
Correct |
3 ms |
3284 KB |
Output is correct |
9 |
Correct |
3 ms |
3284 KB |
Output is correct |
10 |
Correct |
3 ms |
3284 KB |
Output is correct |
11 |
Correct |
3 ms |
3412 KB |
Output is correct |
12 |
Correct |
2 ms |
3284 KB |
Output is correct |
13 |
Correct |
2 ms |
3284 KB |
Output is correct |
14 |
Correct |
2 ms |
3252 KB |
Output is correct |
15 |
Correct |
2 ms |
3284 KB |
Output is correct |
16 |
Correct |
3 ms |
3284 KB |
Output is correct |
17 |
Correct |
3 ms |
3412 KB |
Output is correct |
18 |
Correct |
3 ms |
3284 KB |
Output is correct |
19 |
Correct |
2 ms |
3284 KB |
Output is correct |
20 |
Correct |
3 ms |
3284 KB |
Output is correct |
21 |
Correct |
3 ms |
3284 KB |
Output is correct |
22 |
Correct |
3 ms |
3412 KB |
Output is correct |
23 |
Correct |
2 ms |
3284 KB |
Output is correct |
24 |
Correct |
2 ms |
3348 KB |
Output is correct |
25 |
Correct |
3 ms |
3404 KB |
Output is correct |
26 |
Correct |
2 ms |
3284 KB |
Output is correct |
27 |
Correct |
3 ms |
3284 KB |
Output is correct |
28 |
Correct |
2 ms |
3284 KB |
Output is correct |
29 |
Correct |
6 ms |
3412 KB |
Output is correct |
30 |
Correct |
6 ms |
3412 KB |
Output is correct |
31 |
Correct |
6 ms |
3428 KB |
Output is correct |
32 |
Correct |
4 ms |
3412 KB |
Output is correct |
33 |
Correct |
3 ms |
3408 KB |
Output is correct |
34 |
Correct |
7 ms |
3416 KB |
Output is correct |
35 |
Correct |
9 ms |
3448 KB |
Output is correct |
36 |
Correct |
8 ms |
3416 KB |
Output is correct |
37 |
Correct |
6 ms |
3412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3284 KB |
Output is correct |
2 |
Correct |
2 ms |
3284 KB |
Output is correct |
3 |
Correct |
3 ms |
3412 KB |
Output is correct |
4 |
Correct |
3 ms |
3284 KB |
Output is correct |
5 |
Correct |
3 ms |
3412 KB |
Output is correct |
6 |
Correct |
3 ms |
3284 KB |
Output is correct |
7 |
Correct |
3 ms |
3412 KB |
Output is correct |
8 |
Correct |
3 ms |
3284 KB |
Output is correct |
9 |
Correct |
3 ms |
3284 KB |
Output is correct |
10 |
Correct |
3 ms |
3284 KB |
Output is correct |
11 |
Correct |
3 ms |
3412 KB |
Output is correct |
12 |
Correct |
2 ms |
3284 KB |
Output is correct |
13 |
Correct |
2 ms |
3284 KB |
Output is correct |
14 |
Correct |
2 ms |
3252 KB |
Output is correct |
15 |
Correct |
2 ms |
3284 KB |
Output is correct |
16 |
Correct |
3 ms |
3284 KB |
Output is correct |
17 |
Correct |
3 ms |
3412 KB |
Output is correct |
18 |
Correct |
3 ms |
3284 KB |
Output is correct |
19 |
Correct |
2 ms |
3284 KB |
Output is correct |
20 |
Correct |
3 ms |
3284 KB |
Output is correct |
21 |
Correct |
3 ms |
3284 KB |
Output is correct |
22 |
Correct |
3 ms |
3412 KB |
Output is correct |
23 |
Correct |
2 ms |
3284 KB |
Output is correct |
24 |
Correct |
2 ms |
3348 KB |
Output is correct |
25 |
Correct |
3 ms |
3404 KB |
Output is correct |
26 |
Correct |
2 ms |
3284 KB |
Output is correct |
27 |
Correct |
3 ms |
3284 KB |
Output is correct |
28 |
Correct |
2 ms |
3284 KB |
Output is correct |
29 |
Correct |
6 ms |
3412 KB |
Output is correct |
30 |
Correct |
6 ms |
3412 KB |
Output is correct |
31 |
Correct |
6 ms |
3428 KB |
Output is correct |
32 |
Correct |
4 ms |
3412 KB |
Output is correct |
33 |
Correct |
3 ms |
3408 KB |
Output is correct |
34 |
Correct |
7 ms |
3416 KB |
Output is correct |
35 |
Correct |
9 ms |
3448 KB |
Output is correct |
36 |
Correct |
8 ms |
3416 KB |
Output is correct |
37 |
Correct |
6 ms |
3412 KB |
Output is correct |
38 |
Correct |
92 ms |
5624 KB |
Output is correct |
39 |
Correct |
53 ms |
26752 KB |
Output is correct |
40 |
Correct |
218 ms |
4736 KB |
Output is correct |
41 |
Correct |
219 ms |
4232 KB |
Output is correct |
42 |
Correct |
202 ms |
4936 KB |
Output is correct |
43 |
Correct |
32 ms |
4264 KB |
Output is correct |
44 |
Correct |
108 ms |
3540 KB |
Output is correct |
45 |
Correct |
83 ms |
20812 KB |
Output is correct |
46 |
Correct |
81 ms |
20788 KB |
Output is correct |
47 |
Correct |
71 ms |
23096 KB |
Output is correct |
48 |
Correct |
71 ms |
23088 KB |
Output is correct |
49 |
Correct |
77 ms |
20908 KB |
Output is correct |
50 |
Correct |
73 ms |
20892 KB |
Output is correct |
51 |
Correct |
59 ms |
21564 KB |
Output is correct |
52 |
Correct |
57 ms |
21684 KB |
Output is correct |
53 |
Correct |
126 ms |
4412 KB |
Output is correct |
54 |
Correct |
98 ms |
20756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3284 KB |
Output is correct |
2 |
Correct |
2 ms |
3284 KB |
Output is correct |
3 |
Correct |
2 ms |
3284 KB |
Output is correct |
4 |
Correct |
2 ms |
3284 KB |
Output is correct |
5 |
Correct |
8 ms |
3380 KB |
Output is correct |
6 |
Correct |
2 ms |
3284 KB |
Output is correct |
7 |
Correct |
2 ms |
3284 KB |
Output is correct |
8 |
Correct |
2 ms |
3284 KB |
Output is correct |
9 |
Correct |
2 ms |
3336 KB |
Output is correct |
10 |
Correct |
2 ms |
3284 KB |
Output is correct |
11 |
Correct |
2 ms |
3284 KB |
Output is correct |
12 |
Correct |
9 ms |
3412 KB |
Output is correct |
13 |
Correct |
83 ms |
3396 KB |
Output is correct |
14 |
Correct |
56 ms |
3400 KB |
Output is correct |
15 |
Correct |
59 ms |
3284 KB |
Output is correct |
16 |
Correct |
4643 ms |
21632 KB |
Output is correct |
17 |
Execution timed out |
5073 ms |
23280 KB |
Time limit exceeded |
18 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3284 KB |
Output is correct |
2 |
Correct |
2 ms |
3284 KB |
Output is correct |
3 |
Correct |
2 ms |
3284 KB |
Output is correct |
4 |
Correct |
12 ms |
3524 KB |
Output is correct |
5 |
Correct |
24 ms |
3516 KB |
Output is correct |
6 |
Correct |
3 ms |
3412 KB |
Output is correct |
7 |
Correct |
3 ms |
3540 KB |
Output is correct |
8 |
Correct |
7 ms |
3412 KB |
Output is correct |
9 |
Correct |
90 ms |
5656 KB |
Output is correct |
10 |
Correct |
67 ms |
26700 KB |
Output is correct |
11 |
Correct |
9 ms |
3284 KB |
Output is correct |
12 |
Correct |
76 ms |
3484 KB |
Output is correct |
13 |
Execution timed out |
5093 ms |
34836 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |