#include <bits/stdc++.h>
// #ifndef ONLINE_JUDGE
// #include </home/vamh/Documents/cp/idkman/debug.h>
// #endif //ONLINE_JUDGE
#define ll long long
#define all(x) x.begin(), x.end()
#define Neco "Neco"
#define resp(x) sort(all(x)), x.resize(unique(all(x)) - x.begin())
#define getbit(x,i) ((x >> i)&1)
#define _left id * 2, l, mid
#define _right id * 2 + 1, mid + 1, r
#define cntbit(x) __builtin_popcountll(x)
#define fi(i, a, b) for(int i = a; i <= b; i++)
#define fid(i, a, b) for(int i = a; i >= b; i--)
#define maxn (int) (2e5 + 7)
using namespace std;
const ll mod = 1e9 + 7; //972663749
const ll base = 911382323;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll GetRandom(ll l, ll r) {
return uniform_int_distribution<ll> (l, r)(rng);
}
void Input();
void reset();
int n, m;
vector<int> edges[maxn], g[maxn];
int cha[maxn][18], In[maxn], Out[maxn], Time;
int S[maxn], T[maxn], h[maxn];
void pre_dfs(int u, int par) {
In[u] = ++Time;
for(int v : edges[u]) if(v != par) {
cha[v][0] = u, h[v] = h[u] + 1;
fi(i, 1, 17) cha[v][i] = cha[cha[v][i - 1]][i - 1];
pre_dfs(v, u);
}
Out[u] = Time;
}
int lca(int u, int v) {
if(h[u] < h[v]) swap(u, v);
fid(i, 17, 0) if(h[u] - (1 << i) >= h[v]) u = cha[u][i];
if(u == v) return u;
fid(i, 17, 0) if(cha[u][i] != cha[v][i]) {
u = cha[u][i], v = cha[v][i];
}
return cha[u][0];
}
bool isInPath(int k, int u, int v) {
auto isInSubtree = [](int u, int r) {
return In[r] <= In[u] && In[u] <= Out[r];
};
int p = lca(u, v);
return (isInSubtree(k, p) && isInSubtree(u, k)) || (isInSubtree(k, p) && isInSubtree(v, k));
}
bool topoSort() {
vector<int> deg(n + 1, 0);
fi(i, 1, n) for(int v : g[i]) deg[v] ++;
queue<int> q;
fi(i, 1, n) if(!deg[i]) q.push(i);
while(!q.empty()) {
int u = q.front(); q.pop();
for(int v : g[u]) if(--deg[v] == 0) {
q.push(v);
}
}
return *max_element(all(deg)) == 0;
}
void solve()
{
Input();
if(m > 500) return;
Time = 0;
pre_dfs(1, 0);
fi(i, 1, m) fi(j, 1, m) if(i != j) {
if(isInPath(S[j], S[i], T[i])) g[j].push_back(i);
if(isInPath(T[j], S[i], T[i])) g[i].push_back(j);
}
if(topoSort()) cout << "Yes\n";
else cout << "No\n";
reset();
}
void Input() {
cin >> n;
fi(i, 1, n - 1) {
int x, y; cin >> x >> y;
edges[x].push_back(y);
edges[y].push_back(x);
}
cin >> m;
fi(i, 1, m) cin >> S[i] >> T[i];
}
void reset() {
fi(i, 1, n) edges[i].clear(), g[i].clear();
fi(i, 1, n) fi(j, 0, 17) cha[i][j] = 0;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
if(fopen(Neco".inp", "r")) {
freopen(Neco".inp", "r", stdin);
freopen(Neco".out", "w", stdout);
}
int nTest = 1;
cin >> nTest;
while(nTest--) {
solve();
}
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
jail.cpp: In function 'int main()':
jail.cpp:127:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
127 | freopen(Neco".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
jail.cpp:128:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
128 | freopen(Neco".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~| # | 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... |