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;
vector<vector<int> > g, g2;
vector<int> vis;
bool dfs(int v, int go)
{
vis[v] = 1;
if(v == go)
{//cout << v << endl;
return 1;
}
for(auto x : g[v])
{
if(vis[x] == 0 && dfs(x, go)) return 1;
}
vis[v] = 2;
return 0;
}
bool c(int v)
{
vis[v] = 1;
for(auto x : g2[v])
{
//cout << v << " " << x << endl;
if(vis[x] == 1) return 1;
if(vis[x] == 0) if(c(x)) return 1;
}
vis[v] = 2;
return 0;
}
void solve()
{
int n, m;
cin >> n;
g.assign(n + 1, {});
vis.assign(n + 1, 0);
vector<int> to(n + 2), re(n + 2);
vector<vector<int> > r(n + 1);
for(int i = 0; i < n - 1; i++)
{
int x, y;
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
cin >> m;
g2.assign(m + 1, {});
vector<int> a(m), b(m);
int maxi = 0;
for(int i = 0; i < m; i++) cin >> a[i] >> b[i];
for(int i = 0; i < m; i++)
{
if(a[i] < b[i])
{
to[a[i]]++;
to[b[i] + 1]--;
r[a[i]] .push_back(b[i]);
}
else
{
//cout << a[i] << " " << b[i] << "\n";
re[b[i]]++;
re[a[i] + 1]--;
r[b[i]].push_back(a[i]);
}
}
int fi = 0, se = 0;
for(int i = 1; i <= n; i++)
{
sort(r[i].rbegin(), r[i].rend());
for(auto x : r[i])
{
if(x <= maxi)
{
cout << "No\n";
return;
}
maxi = max(maxi, x);
}
fi += to[i];
se += re[i];
if(fi > 0 && se > 0)
{
cout << "No\n";
return;
}
}
/*for(int i = 0; i < m; i++)
{
fill(vis.begin(), vis.end(), 0);
dfs(a[i], b[i]);
for(int j = 0; j < m; j++)
{
if(i == j) continue;
if(vis[a[j]] == 1)
{
g2[i].push_back(j);
}
if(vis[b[j]] == 1)
{
//cout << i << " " << j << endl;
g2[j].push_back(i);
}
}
}
vis.assign(m + 1, 0);
for(int i = 0; i < m; i++)
{
if(vis[i] == 0 && c(i))
{
cout << "No\n";
return;
}
}
*/
cout << "Yes\n";
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int q;
cin >> q;
while(q--) solve();
}
# | 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... |