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>
using namespace std;
const int inf = 1e9 + 7;
int n, m;
vector<vector<int>> g, p;
vector<int> tin, tout;
vector<int> s, f;
vector<int> col;
vector<int> parent;
vector<int> st, ft;
int cnt = 0;
void dfs(int v, int pr)
{
tin[v] = cnt++;
parent[v] = pr;
for (int to : g[v])
{
if (to == pr)
continue;
dfs(to, v);
}
tout[v] = cnt;
}
bool child(int a, int b)
{
return tin[a] <= tin[b] && tout[a] >= tout[b];
}
void build(int v)
{
//cout << v << endl;
int pnt = s[v];
while (!child(pnt, f[v]))
{
//cout << '-' << pnt << endl;
if (st[pnt] >= 0)
{
p[v].push_back(st[pnt]);
}
if (ft[pnt] >= 0)
{
p[ft[pnt]].push_back(v);
}
pnt = parent[pnt];
}
pnt = f[v];
while (!child(pnt, s[v]))
{
//cout << '-' << pnt << endl;
if (st[pnt] >= 0)
{
p[v].push_back(st[pnt]);
}
if (ft[pnt] >= 0)
{
p[ft[pnt]].push_back(v);
}
pnt = parent[pnt];
}
if (st[pnt] >= 0)
{
p[v].push_back(st[pnt]);
}
if (ft[pnt] >= 0)
{
p[ft[pnt]].push_back(v);
}
}
bool cicle(int v)
{
col[v] = 1;
bool ans = true;
for (int to : p[v])
{
if (to == v)
continue;
if (col[to] == 1)
return false;
if (col[to] == 0)
ans &= cicle(to);
}
col[v] = 2;
return ans;
}
void solve()
{
cin >> n;
g.resize(n + 1);
for (int i = 1; i < n; i++)
{
int a, b;
cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
cin >> m;
p.resize(m);
s.resize(m);
f.resize(m);
st.resize(n + 1, -1);
ft.resize(n + 1, -1);
for (int i = 0; i < m; i++)
{
cin >> s[i] >> f[i];
st[s[i]] = i;
ft[f[i]] = i;
}
tin.resize(n + 1);
tout.resize(n + 1);
parent.resize(n + 1);
dfs(1, 0);
col.resize(m, 0);
for (int i = 0; i < m; i++)
{
build(i);
}
bool ans = true;
for (int i = 0; i < m; i++)
{
if (col[i] == 0)
ans &= cicle(i);
}
cout << (ans ? "Yes" : "No") << endl;
g.clear();
p.clear();
tin.clear();
tout.clear();
s.clear();
f.clear();
col.clear();
parent.clear();
st.clear();
ft.clear();
}
int main()
{
int q;
cin >> q;
while (q--)
solve();
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... |