This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*************************************
* author: marvinthang *
* created: 07.07.2023 17:53:07 *
*************************************/
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define left ___left
#define right ___right
#define TIME (1.0 * clock() / CLOCKS_PER_SEC)
#define MASK(i) (1LL << (i))
#define BIT(x, i) ((x) >> (i) & 1)
#define __builtin_popcount __builtin_popcountll
#define ALL(v) (v).begin(), (v).end()
#define REP(i, n) for (int i = 0, _n = (n); i < _n; ++i)
#define REPD(i, n) for (int i = (n); i--; )
#define FOR(i, a, b) for (int i = (a), _b = (b); i < _b; ++i)
#define FORD(i, b, a) for (int i = (b), _a = (a); --i >= _a; )
#define FORE(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORDE(i, b, a) for (int i = (b), _a = (a); i >= _a; --i)
#define scan_op(...) istream & operator >> (istream &in, __VA_ARGS__ &u)
#define print_op(...) ostream & operator << (ostream &out, const __VA_ARGS__ &u)
#ifdef LOCAL
#include "debug.h"
#else
#define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
#define DB(...) 23
#define db(...) 23
#define debug(...) 23
#endif
template <class U, class V> scan_op(pair <U, V>) { return in >> u.first >> u.second; }
template <class T> scan_op(vector <T>) { for (size_t i = 0; i < u.size(); ++i) in >> u[i]; return in; }
template <class U, class V> print_op(pair <U, V>) { return out << '(' << u.first << ", " << u.second << ')'; }
template <size_t i, class T> ostream & print_tuple_utils(ostream &out, const T &tup) { if constexpr(i == tuple_size<T>::value) return out << ")"; else return print_tuple_utils<i + 1, T>(out << (i ? ", " : "(") << get<i>(tup), tup); }
template <class ...U> print_op(tuple<U...>) { return print_tuple_utils<0, tuple<U...>>(out, u); }
template <class Con, class = decltype(begin(declval<Con>()))> typename enable_if <!is_same<Con, string>::value, ostream&>::type operator << (ostream &out, const Con &con) { out << '{'; for (__typeof(con.begin()) it = con.begin(); it != con.end(); ++it) out << (it == con.begin() ? "" : ", ") << *it; return out << '}'; }
template <class A, class B> bool minimize(A &a, B b) { if (a > b) { a = b; return true; } return false; }
template <class A, class B> bool maximize(A &a, B b) { if (a < b) { a = b; return true; } return false; }
// end of template
const int MAX = 1.2e5 + 5;
const int LOG = 20;
int N, M;
vector <int> tree_adj[MAX];
int depth[MAX], anc[MAX][LOG];
void init(void) {
cin >> N;
FORE(i, 1, N) tree_adj[i].clear();
FOR(i, 1, N) {
int u, v; cin >> u >> v;
tree_adj[u].push_back(v);
tree_adj[v].push_back(u);
}
cin >> M;
}
void dfs(int u) {
depth[u] = depth[anc[u][0]] + 1;
FOR(i, 1, LOG) anc[u][i] = anc[anc[u][i - 1]][i - 1];
for (int v: tree_adj[u]) if (v != anc[u][0]) {
anc[v][0] = u;
dfs(v);
}
}
int in[MAX][LOG], out[MAX][LOG];
vector <int> adj[MAX * LOG * 2];
int low[MAX * LOG * 2], num[MAX * LOG * 2];
bool imp[MAX * LOG * 2];
int getParent(int u, int h) {
REP(i, LOG) if (BIT(h, i)) u = anc[u][i];
return u;
}
int __lca(int u, int v) {
if (depth[u] < depth[v]) swap(u, v);
u = getParent(u, depth[u] - depth[v]);
if (u == v) return u;
REPD(i, LOG) if (anc[u][i] != anc[v][i]) {
u = anc[u][i];
v = anc[v][i];
}
return anc[u][0];
}
stack <int> st;
int timeDfs;
bool dfs2(int u) {
st.push(u);
low[u] = num[u] = ++timeDfs;
for (int v: adj[u]) {
if (!num[v]) {
if (dfs2(v)) return true;
minimize(low[u], low[v]);
} else minimize(low[u], num[v]);
}
if (low[u] == num[u]) {
int v, cnt = 0;
do {
v = st.top(); st.pop();
cnt += imp[v];
low[v] = num[v] = 1e9;
} while (v != u);
if (cnt > 1) return true;
}
return false;
}
void process(void) {
dfs(1);
int cnt = 0;
REP(j, LOG) FORE(i, 1, N) if (depth[i] >= MASK(j)) {
low[cnt] = num[cnt] = 0;
adj[cnt].clear();
imp[cnt] = !j;
in[i][j] = cnt++;
low[cnt] = num[cnt] = 0;
adj[cnt].clear();
imp[cnt] = false;
out[i][j] = cnt++;
if (j) {
int p = anc[i][j - 1];
adj[in[i][j - 1]].push_back(in[i][j]);
adj[in[p][j - 1]].push_back(in[i][j]);
adj[out[i][j]].push_back(out[i][j - 1]);
adj[out[i][j]].push_back(out[p][j - 1]);
}
}
while (M--) {
int u, v; cin >> u >> v;
adj[in[u][0]].push_back(out[v][0]);
adj[out[v][0]].push_back(in[u][0]);
int lca = __lca(u, v);
auto addEdge = [&] (int a, int h) {
adj[in[a][h]].push_back(in[u][0]);
adj[out[v][0]].push_back(out[a][h]);
};
int h = __lg(depth[u] - depth[lca] + 1);
addEdge(u, h);
addEdge(getParent(u, depth[u] - depth[lca] + 1 - MASK(h)), h);
if (v != lca) {
h = __lg(depth[v] - depth[lca]);
addEdge(v, h);
addEdge(getParent(v, depth[v] - depth[lca] - MASK(h)), h);
}
}
while (!st.empty()) st.pop();
timeDfs = 0;
REP(i, cnt) if (!num[i] && dfs2(i)) return void(cout << "No\n");
cout << "Yes\n";
}
int main(void) {
ios_base::sync_with_stdio(false); cin.tie(nullptr); // cout.tie(nullptr);
file("jail");
int t; cin >> t;
while (t--) {
init();
process();
}
cerr << "Time elapsed: " << TIME << " s.\n";
return (0^0);
}
Compilation message (stderr)
jail.cpp: In function 'int main()':
jail.cpp:30:61: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
30 | #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
jail.cpp:168:2: note: in expansion of macro 'file'
168 | file("jail");
| ^~~~
jail.cpp:30:94: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
30 | #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
jail.cpp:168:2: note: in expansion of macro 'file'
168 | file("jail");
| ^~~~
# | 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... |