// In the name of God
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define mp make_pair
typedef long long ll;
const int N = 2e5 + 5, L = 20;
int n, m, par[N], deg[N];
vector<pair<int, int> > S[N], T[N];
set<int> st[N];
pair<int, int> P[N];
vector<int> G[N], adj[N];
int up[N][L], Tp, tin[N], tout[N];
void dfs(int v, int p) {
up[v][0] = p;
for (int l = 1; l < L; l++)
up[v][l] = up[up[v][l - 1]][l - 1];
tin[v] = Tp++;
for (auto u : adj[v]) {
if (u != p)
dfs(u, v);
}
tout[v] = Tp++;
}
vector<int> tmp;
void _merge(int u, int v) {
if (st[u].size() > st[v].size())
st[u].swap(st[v]);
for (auto x : st[u]) {
if (st[v].count(x))
tmp.pb(x);
st[v].insert(x);
}
}
void dfs2(int v, int p) {
int s = -1, t = -1;
if (!S[v].empty()) {
s = S[v].back().se;
}
if (!T[v].empty())
t = T[v].back().se;
tmp.clear();
for (auto u : adj[v]) {
if (u == p)
continue;
dfs2(u, v);
_merge(u, v);
}
if (s != -1) {
for (auto u : st[v]) {
if (u != s)
G[s].pb(u);
}
if (st[v].count(s)) {
st[v].erase(s);
}
else
st[v].insert(s);
}
if (t != -1) {
for (auto u : st[v])
if (u != t)
G[u].pb(t);
if (st[v].count(t))
st[v].erase(t);
else
st[v].insert(t);
}
for (auto x : tmp)
st[v].erase(x);
}
bool anc(int v, int u) {
return tin[v] <= tin[u] && tout[v] >= tout[u];
}
int lca(int v, int u) {
if (tin[v] > tin[u])
swap(v, u);
if (anc(v, u))
return v;
for (int i = L - 1; i >= 0; i--) {
if (!anc(up[u][i], v))
u = up[u][i];
}
return up[u][0];
}
void solve2() {
sort(P, P +m);
for (int i = 0; i < m - 1; i++) {
if (P[i].se > P[i + 1].se) {
cout << "No" << '\n';
return;
}
}
cout << "Yes";
}
void solve() {
cin >> n;
for (int i = 1; i <= n; i++)
S[i].clear(), T[i].clear(), adj[i].clear();
for (int i = 0; i < m; i++)
G[i].clear(), deg[i] = 0;
for (int i = 0; i < n - 1; i++) {
int v, u; cin >> v >> u;
adj[v].pb(u), adj[u].pb(v);
}
cin >> m;
vector<pair<pair<int, int>, int> > vec;
for (int i = 0; i < m; i++) {
int s, t; cin >> s >> t;
P[i] = mp(s, t);
vec.pb(mp(mp(s, t), i));
S[s].pb(mp(t, i));
T[t].pb(mp(s, i));
}
if (m > 500) {
solve2();
return;
}
for (int i = 0; i < vec.size(); i++) {
int s = vec[i].fi.fi, t = vec[i].fi.se, v = vec[i].se;
}
dfs2(1, 0);
for (int i = 0; i < m; i++) {
for (auto u : G[i]) {
deg[u]++;
}
}
queue<int> Q;
for (int i = 0; i < m; i++)
if (deg[i] == 0)
Q.push(i);
while (!Q.empty()) {
int v = Q.front();
Q.pop();
for (auto u : G[v]) {
deg[u]--;
if (!deg[u])
Q.push(u);
}
}
cout << (*max_element(deg, deg + m) == 0 ? "Yes" : "No") << '\n';
}
int32_t main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int tc; cin >> tc;
while (tc--) {
solve();
}
return 0;
}
Compilation message
jail.cpp: In function 'void solve()':
jail.cpp:128:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<std::pair<int, int>, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
128 | for (int i = 0; i < vec.size(); i++) {
| ~~^~~~~~~~~~~~
jail.cpp:129:13: warning: unused variable 's' [-Wunused-variable]
129 | int s = vec[i].fi.fi, t = vec[i].fi.se, v = vec[i].se;
| ^
jail.cpp:129:31: warning: unused variable 't' [-Wunused-variable]
129 | int s = vec[i].fi.fi, t = vec[i].fi.se, v = vec[i].se;
| ^
jail.cpp:129:49: warning: unused variable 'v' [-Wunused-variable]
129 | int s = vec[i].fi.fi, t = vec[i].fi.se, v = vec[i].se;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
28440 KB |
Output is correct |
2 |
Correct |
18 ms |
28500 KB |
Output is correct |
3 |
Correct |
19 ms |
28500 KB |
Output is correct |
4 |
Correct |
27 ms |
28820 KB |
Output is correct |
5 |
Correct |
31 ms |
29168 KB |
Output is correct |
6 |
Correct |
15 ms |
28532 KB |
Output is correct |
7 |
Correct |
17 ms |
28592 KB |
Output is correct |
8 |
Correct |
18 ms |
28628 KB |
Output is correct |
9 |
Correct |
66 ms |
32704 KB |
Output is correct |
10 |
Correct |
59 ms |
48552 KB |
Output is correct |
11 |
Correct |
22 ms |
28628 KB |
Output is correct |
12 |
Correct |
59 ms |
29556 KB |
Output is correct |
13 |
Correct |
85 ms |
38280 KB |
Output is correct |
14 |
Correct |
59 ms |
38284 KB |
Output is correct |
15 |
Correct |
70 ms |
36256 KB |
Output is correct |
16 |
Correct |
133 ms |
44860 KB |
Output is correct |
17 |
Correct |
60 ms |
39308 KB |
Output is correct |
18 |
Correct |
79 ms |
44860 KB |
Output is correct |
19 |
Correct |
61 ms |
39292 KB |
Output is correct |
20 |
Correct |
70 ms |
39288 KB |
Output is correct |
21 |
Correct |
62 ms |
39228 KB |
Output is correct |
22 |
Correct |
64 ms |
39332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
28516 KB |
Output is correct |
2 |
Correct |
16 ms |
28508 KB |
Output is correct |
3 |
Correct |
17 ms |
28500 KB |
Output is correct |
4 |
Incorrect |
16 ms |
28500 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
28516 KB |
Output is correct |
2 |
Correct |
16 ms |
28508 KB |
Output is correct |
3 |
Correct |
17 ms |
28500 KB |
Output is correct |
4 |
Incorrect |
16 ms |
28500 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
28516 KB |
Output is correct |
2 |
Correct |
16 ms |
28508 KB |
Output is correct |
3 |
Correct |
17 ms |
28500 KB |
Output is correct |
4 |
Incorrect |
16 ms |
28500 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
28516 KB |
Output is correct |
2 |
Correct |
16 ms |
28508 KB |
Output is correct |
3 |
Correct |
17 ms |
28500 KB |
Output is correct |
4 |
Incorrect |
16 ms |
28500 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
28428 KB |
Output is correct |
2 |
Correct |
13 ms |
28500 KB |
Output is correct |
3 |
Correct |
13 ms |
28500 KB |
Output is correct |
4 |
Correct |
14 ms |
28500 KB |
Output is correct |
5 |
Correct |
21 ms |
28540 KB |
Output is correct |
6 |
Incorrect |
15 ms |
28500 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
28440 KB |
Output is correct |
2 |
Correct |
18 ms |
28500 KB |
Output is correct |
3 |
Correct |
19 ms |
28500 KB |
Output is correct |
4 |
Correct |
27 ms |
28820 KB |
Output is correct |
5 |
Correct |
31 ms |
29168 KB |
Output is correct |
6 |
Correct |
15 ms |
28532 KB |
Output is correct |
7 |
Correct |
17 ms |
28592 KB |
Output is correct |
8 |
Correct |
18 ms |
28628 KB |
Output is correct |
9 |
Correct |
66 ms |
32704 KB |
Output is correct |
10 |
Correct |
59 ms |
48552 KB |
Output is correct |
11 |
Correct |
22 ms |
28628 KB |
Output is correct |
12 |
Correct |
59 ms |
29556 KB |
Output is correct |
13 |
Correct |
85 ms |
38280 KB |
Output is correct |
14 |
Correct |
59 ms |
38284 KB |
Output is correct |
15 |
Correct |
70 ms |
36256 KB |
Output is correct |
16 |
Correct |
133 ms |
44860 KB |
Output is correct |
17 |
Correct |
60 ms |
39308 KB |
Output is correct |
18 |
Correct |
79 ms |
44860 KB |
Output is correct |
19 |
Correct |
61 ms |
39292 KB |
Output is correct |
20 |
Correct |
70 ms |
39288 KB |
Output is correct |
21 |
Correct |
62 ms |
39228 KB |
Output is correct |
22 |
Correct |
64 ms |
39332 KB |
Output is correct |
23 |
Correct |
15 ms |
28516 KB |
Output is correct |
24 |
Correct |
16 ms |
28508 KB |
Output is correct |
25 |
Correct |
17 ms |
28500 KB |
Output is correct |
26 |
Incorrect |
16 ms |
28500 KB |
Output isn't correct |
27 |
Halted |
0 ms |
0 KB |
- |