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>
#define MAX 500005
#define MOD (ll)(1e9+7)
#define INF (ll)(1e18)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
int n, m, tt;
int parent[120005][25], in1[120005][25], in2[120005][25];
int s[120005], t[120005], dep[120005], deg[4800005];
int S[120005], T[120005];
vector<int> graph[120005];
vector<int> DAG[4800005];
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> tt;
while(tt--) {
cin >> n;
for(int i = 1; i <= n; ++i) graph[i].clear(), S[i] = T[i] = 0;
for(int i = 0; i <= 40 * n; ++i) DAG[i].clear(), deg[i] = 0;
for(int i = 1; i <= n - 1; ++i) {
int a, b; cin >> a >> b;
graph[a].push_back(b);
graph[b].push_back(a);
}
cin >> m;
for(int i = 1; i <= m; ++i) cin >> s[i] >> t[i];
auto dfs = [&](auto&& self, int node, int par) -> void {
parent[node][0] = par; dep[node] = dep[par] + 1;
for(auto v : graph[node]) if(v != par) self(self, v, node);
};
dfs(dfs, 1, 0);
for(int j = 1; j < 20; ++j) for(int i = 1; i <= n; ++i) parent[i][j] = parent[parent[i][j - 1]][j - 1];
auto LCA = [&](int u, int v) -> int {
if(dep[u] < dep[v]) swap(u, v);
int diff = dep[u] - dep[v], j = 0;
while(diff) {
if(diff & 1) u = parent[u][j];
diff >>= 1; ++j;
}
if(u == v) return u;
for(int i = 19; i >= 0; --i) {
if(parent[u][i] != parent[v][i]) {
u = parent[u][i];
v = parent[v][i];
}
}
return parent[u][0];
};
for(int i = 1; i <= m; ++i) S[s[i]] = T[t[i]] = i;
for(int i = 1; i <= n; ++i) {
in1[i][0] = S[i];
in2[i][0] = T[i];
}
int pv = m;
for(int j = 1; j < 20; ++j) for(int i = 1; i <= n; ++i) {
in1[i][j] = ++pv;
DAG[in1[i][j - 1]].push_back(in1[i][j]);
DAG[in1[parent[i][j - 1]][j - 1]].push_back(in1[i][j]);
}
for(int j = 1; j < 20; ++j) for(int i = 1; i <= n; ++i) {
in2[i][j] = ++pv;
DAG[in2[i][j]].push_back(in2[i][j - 1]);
DAG[in2[i][j]].push_back(in2[parent[i][j - 1]][j - 1]);
}
for(int i = 1; i <= m; ++i) {
int p = LCA(s[i], t[i]);
for(int tmp : {s[i], t[i]}) {
if(tmp == p) continue;
tmp = parent[tmp][0];
int diff = dep[tmp] - dep[p], j = 0;
while(diff) {
if(diff & 1) {
DAG[in1[tmp][j]].push_back(i);
DAG[i].push_back(in2[tmp][j]);
tmp = parent[tmp][j];
}
diff >>= 1; ++j;
}
}
if(S[p] != i) DAG[S[p]].push_back(i);
if(T[p] != i) DAG[i].push_back(T[p]);
if(S[t[i]] != i) DAG[S[t[i]]].push_back(i);
if(T[s[i]] != i) DAG[i].push_back(T[s[i]]);
}
queue<int> q; int cnt = 0;
for(int i = 1; i <= pv; ++i) for(auto v : DAG[i]) if(v) {
++deg[v];
//cout << i << ' ' << v << endl;
assert(i != v);
}
for(int i = 1; i <= pv; ++i) if(deg[i] == 0) q.push(i);
while(!q.empty()) {
int node = q.front(); q.pop();
if(node <= m) ++cnt;
for(auto v : DAG[node]) if(node != v && v && --deg[v] == 0) q.push(v);
}
if(cnt == m) cout << "Yes\n";
else cout << "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... |