#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <string>
#include <cmath>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <fstream>
#include <iomanip>
#include <iterator>
#include <stack>
#include <deque>
using namespace std;
using ll = long long;
vector<int> v[120005];
int ar[120005], col[120005];
vector<pair<int, int>> c;
stack<int> s;
int st = 0;
void gum(int arj)
{
s.pop();
while (!s.empty())
{
ar[s.top()] += arj;
s.pop();
}
}
void Dfs(int g, int p, int kang, int gu)
{
if (st)
return;
s.push(g);
if (g == kang)
{
gum(gu);
st = 1;
}
if (st)
return;
for (auto to : v[g])
{
if (to != p)
Dfs(to, g, kang, gu);
if (st)
return;
}
if (st)
return;
s.pop();
}
void solve()
{
int n, i, j, x, m, y, qan;
cin >> n;
for ( i = 0; i < n - 1; i++)
{
cin >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
}
cin >> m;
for ( i = 0; i < m; i++)
{
cin >> x >> y;
c.push_back({ x, y });
st = 0;
Dfs(x, -1, y, 1);
}
int ans = 1;
for ( i = 0; i < m; i++)
{
for ( j = 0; j < m; j++)
{
if (ar[c[j].second] == 0 && col[j] == 0)
{
st = 0;
Dfs(c[j].first, -1, c[j].second, -1);
col[j] = 1;
break;
}
}
if (j == m)
{
ans = 0;
break;
}
}
if (ans)
cout << "Yes\n";
else
cout << "No\n";
col[0] = 0;
for ( i = 1; i <= n; i++)
{
v[i].clear();
col[i] = 0;
ar[i] = 0;
}
c.clear();
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int tt = 1;
cin >> tt;
while (tt--)
{
solve();
}
return 0;
}
Compilation message
jail.cpp: In function 'void solve()':
jail.cpp:60:24: warning: unused variable 'qan' [-Wunused-variable]
60 | int n, i, j, x, m, y, qan;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3028 KB |
Output is correct |
2 |
Correct |
3 ms |
3028 KB |
Output is correct |
3 |
Correct |
2 ms |
3144 KB |
Output is correct |
4 |
Incorrect |
9 ms |
3412 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3028 KB |
Output is correct |
2 |
Correct |
2 ms |
3028 KB |
Output is correct |
3 |
Incorrect |
3 ms |
3156 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3028 KB |
Output is correct |
2 |
Correct |
2 ms |
3028 KB |
Output is correct |
3 |
Incorrect |
3 ms |
3156 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3028 KB |
Output is correct |
2 |
Correct |
2 ms |
3028 KB |
Output is correct |
3 |
Incorrect |
3 ms |
3156 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3028 KB |
Output is correct |
2 |
Correct |
2 ms |
3028 KB |
Output is correct |
3 |
Incorrect |
3 ms |
3156 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
3028 KB |
Output is correct |
2 |
Correct |
2 ms |
3028 KB |
Output is correct |
3 |
Correct |
2 ms |
3028 KB |
Output is correct |
4 |
Correct |
2 ms |
3028 KB |
Output is correct |
5 |
Incorrect |
7 ms |
3284 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3028 KB |
Output is correct |
2 |
Correct |
3 ms |
3028 KB |
Output is correct |
3 |
Correct |
2 ms |
3144 KB |
Output is correct |
4 |
Incorrect |
9 ms |
3412 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |