//
#ifndef __SIZEOF_INT128__
#define __SIZEOF_INT128__
#endif
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace chrono;
using namespace __gnu_pbds;
template <typename T> using oset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define rep(i, p, k) for(int i(p); i < (k); ++i)
#define per(i, p, k) for(int i(p); i > (k); --i)
#define sz(x) (int)(x).size()
#define sc static_cast
typedef long long ll;
typedef long double ld;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef __int128_t lll;
//#define int ll
template <typename T = int> using par = std::pair <T, T>;
#define fi first
#define se second
#define test int _number_of_tests(in()); while(_number_of_tests--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pb emplace_back
struct Timer {
string name{""};
time_point<high_resolution_clock> end, start{high_resolution_clock::now()};
duration<float, std::milli> dur;
Timer() = default;
Timer(string nm): name(nm) {}
~Timer() {
end = high_resolution_clock::now(); dur= end - start;
cout << "@" << name << "> " << dur.count() << " ms" << '\n';
}
};
template <typename T = int> inline T in()
{
static T x;
std::cin >> x;
return x;
}
std::string yn(bool b)
{
if(b) return "YES\n";
else return "NO\n";
}
template <typename F, typename S> std::ostream& operator<<(std::ostream& out, const std::pair <F, S>& par);
template <typename T> std::ostream& operator<< (std::ostream& out, const std::vector <T>& wek)
{
for(const auto& i : wek)out << i << ' ';
return out;
}
template <typename F, typename S> std::ostream& operator<<(std::ostream& out, const std::pair <F, S>& par)
{
out << '{'<<par.first<<", "<<par.second<<"}";
return out;
}
#define show(x) cerr << #x << " = " << x << '\n';
std::int32_t main()
{
std::cout.tie(nullptr); //for luck
std::cin.tie(nullptr); std::ios_base::sync_with_stdio(0);
test{
int n(in());
vector <vector<int>> g(n);
rep(i, 1, n){
int a(in()-1), b(in()-1);
g[a].pb(b);
g[b].pb(a);
}
int m(in());
vector <int> s(m), t(m);
vector <vector <int>> vs(n), vt(n);
rep(i, 0, m){
cin >> s[i] >> t[i];
vs[--s[i]].pb(i);
vt[--t[i]].pb(i);
}
vector <int> p(n), o(n);
stack <pair<int, int>> dfs;
dfs.push({0,0});
while(sz(dfs)){
auto [w, q] = dfs.top(); dfs.pop();
p[w] = q;
for(auto i: g[w])if(i != o[w]){
o[i] = w;
dfs.push({i, q+1});
}
}
vector <vector <int>> d(m);
vector <array<int, 20>> ojc(n), gor(n), dol(n);
rep(i, 0, n){
ojc[i][0] = o[i];
gor[i][0] = sz(d);
d.pb();
dol[i][0] = sz(d);
d.pb();
for(auto j: vt[i])d[gor[i][0]].pb(j);
for(auto j: vs[i])d[j].pb(dol[i][0]);
}
rep(k, 1, 20){
rep(i, 0, n){
ojc[i][k] = ojc[ojc[i][k-1]][k-1];
gor[i][k] = sz(d);
d.pb(gor[i][k-1], gor[ojc[i][k-1]][k-1]);
dol[i][k] = sz(d);
d.pb();
d[dol[i][k-1]].pb(dol[i][k]);
d[dol[ojc[i][k-1]][k-1]].pb(dol[i][k]);
}
}
auto lca = [&](int a, int b){
if(p[a] < p[b])swap(a, b);
int k(19);
while(p[a] > p[b]){
while(p[ojc[a][k]] < p[b] && k)--k;
a = ojc[a][k];
}
k = 19;
while(a != b){
while(ojc[a][k] == ojc[b][k] && k)--k;
a = ojc[a][k];
b = ojc[b][k];
}
return a;
};
rep(i, 0, m){
int a(s[i]), b(t[i]);
if(lca(a, b) == a){
for(auto i: g[a])if(i != o[a] && lca(i, b) == i){
a = i;
break;
}
}
else a = o[a];
if(p[a] < p[b])swap(a, b);
int k(19);
while(p[a] > p[b]){
while(p[ojc[a][k]] < p[b] && k)--k;
d[dol[a][k]].pb(i);
a = ojc[a][k];
}
k = 19;
while(a != b){
while(ojc[a][k] == ojc[b][k] && k)--k;
d[dol[a][k]].pb(i);
d[dol[b][k]].pb(i);
a = ojc[a][k];
b = ojc[b][k];
}
d[dol[a][0]].pb(i);
}
rep(i, 0, m){
int a(s[i]), b(t[i]);
swap(a, b);
if(lca(a, b) == a){
for(auto i: g[a])if(i != o[a] && lca(i, b) == i){
a = i;
break;
}
}
else a = o[a];
if(p[a] < p[b])swap(a, b);
int k(19);
while(p[a] > p[b]){
while(p[ojc[a][k]] < p[b] && k)--k;
d[i].pb(gor[a][k]);
a = ojc[a][k];
}
k = 19;
while(a != b){
while(ojc[a][k] == ojc[b][k] && k)--k;
d[i].pb(gor[a][k]);
d[i].pb(gor[b][k]);
a = ojc[a][k];
b = ojc[b][k];
}
d[i].pb(gor[a][0]);
}
vector <int> dg(sz(d));
rep(i, 0, sz(d))for(auto j: d[i])++dg[j];
// rep(i, 0, sz(d))for(auto j: d[i])cout << i << " -> " << j << '\n';
queue <int> kul;
rep(i, 0, sz(d))if(!dg[i])kul.push(i);
int cnt(0);
while(!kul.empty()){
auto w(kul.front()); kul.pop();
++cnt;
dg[w] = -1;
for(auto i: d[w])if(!--dg[i])kul.push(i);
}
cout << (cnt == sz(d) ? "Yes\n" : "No\n");
}
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... |