This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h> // pA
#define IO ios::sync_with_stdio(false);cin.tie(0);
#define debug(x) cerr << #x << " = " << (x) << ' '
#define endl cerr << '\n'
#define all(v) (v).begin(), (v).end()
#define SZ(v) (ll)(v).size()
#define lowbit(x) (x)&-(x)
#define pb emplace_back
#define F first
#define S second
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
const int N = 2e5+5;
//const int INF = 2e9;
int s[N], t[N], tin[2][N], timer, tout[2][N];
vector<int> g[N];
inline void dfs(int x, int pre, int lev)
{
tin[lev][x] = ++timer;
for (int v : g[x]) if (v != pre) dfs(v, x, lev);
tout[lev][x] = ++timer;
}
inline bool is_anc(int x, int y, int lev)
{ return tin[lev][x] <= tin[lev][y] && tout[lev][y] <= tout[lev][x]; }
int main(void)
{ IO
ll T, n, i, m, x, y, j;
bool bad;
cin >> T;
do {
cin >> n;
for (i=1; i <= n; ++i) g[i].clear();
for (i=0; i < n-1; ++i) cin >> x >> y, g[x].pb(y), g[y].pb(x);
cin >> m;
for (i=1; i <= m; ++i) cin >> s[i] >> t[i];
bad = 0;
for (i=1; i <= m; ++i)
{
for (int dd=0; dd < 2; ++dd)
for (j=1; j <= n; ++j) tin[dd][j] = tout[dd][j] = 0;
timer = 0;
dfs(s[i], 0, 0), dfs(t[i], 0, 1);
for (j=1; j <= m; ++j)
{
if (i == j) continue;
if (is_anc(t[j], t[i], 0) && is_anc(t[i], s[j], 0)) bad = 1;
if (is_anc(s[j], s[i], 1) && is_anc(s[i], t[j], 1)) bad = 1;
if (is_anc(s[j], t[i], 0) && is_anc(t[j], t[i], 0)) bad = 1;
}
if (bad) break;
}
if (bad) cout << "No\n";
else cout << "Yes\n";
} while (--T);
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... |