이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <algorithm>
#include <vector>
#include <cassert>
#include <cmath>
#include <set>
#include <map>
#include <deque>
#include <queue>
#include <iomanip>
using namespace std;
#define pb push_back
#define mp make_pair
#define sz(a) ((int) (a).size())
#define all(a) (a).begin(), (a).end()
using ll = long long;
const int N = 120121;
const int K = 20;
int n;
vector<int> g[N];
int m;
int s[N], t[N];
int up[N][K];
int tin[N], tout[N], ttt;
vector<int> g1[N];
int used[N];
bool bad;
void dfs(int v, int p) {
up[v][0] = p;
for (int i = 1; i < K; ++i) {
up[v][i] = up[up[v][i - 1]][i - 1];
}
tin[v] = ttt++;
for (int u : g[v]) {
if (u != p) dfs(u, v);
}
tout[v] = ttt;
}
bool anc(int v, int u) {
return tin[v] <= tin[u] && tout[v] >= tout[u];
}
int get_lca(int v, int u) {
if (anc(v, u)) return v;
if (anc(u, v)) return u;
for (int i = K - 1; i >= 0; --i) {
if (!anc(up[v][i], u)) {
v = up[v][i];
}
}
return up[v][0];
}
bool on_path(int a, int b, int v) {
int c = get_lca(a, b);
return anc(c, v) && (anc(v, a) || anc(v, b));
}
void dfs1(int v) {
used[v] = 1;
for (int u : g1[v]) {
if (used[u] == 1) bad = 1;
if (used[u] == 0) dfs1(u);
}
used[v] = 2;
}
void solve() {
cin >> n;
for (int i = 1; i <= n; ++i) {
g[i].clear();
}
for (int i = 0; i < n - 1; ++i) {
int a, b;
cin >> a >> b;
g[a].pb(b);
g[b].pb(a);
}
cin >> m;
for (int i = 0; i < m; ++i) {
cin >> s[i] >> t[i];
}
ttt = 0;
dfs(1, 1);
for (int i = 0; i < m; ++i) {
g1[i].clear();
}
for (int i = 0; i < m; ++i) {
for (int j = 0; j < m; ++j) {
if (i == j) continue;
if (on_path(s[i], t[i], s[j])) g1[j].pb(i);
if (on_path(s[i], t[i], t[j])) g1[i].pb(j);
}
}
for (int i = 0; i < m; ++i) {
used[i] = 0;
}
bad = 0;
for (int i = 0; i < m; ++i) {
if (used[i] == 0) dfs1(i);
}
cout << (bad ? "No" : "Yes") << "\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int q;
cin >> q;
while (q--) {
solve();
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |