#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pdd pair<double, double>
#define ALL(x) x.begin(), x.end()
#define vi vector<int>
#define vl vector<ll>
#define SZ(x) (int)x.size()
#define CASE int t;cin>>t;for(int ca=1;ca<=t;ca++)
#define IOS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
const int MAX = 1 << 20, MOD = 1e9 + 7;
vi edge[MAX], p[MAX], e[MAX];
int cnt[MAX], u[MAX], v[MAX], from[MAX], vis[MAX];
bool dfs(int now){
vis[now] = 1;
for(int i : e[now]){
if(vis[i]){
return 1;
}
if(dfs(i))
return 1;
}
return 0;
}
void solve(){
int n;
cin >> n;
for(int i = 1;i <= n;i++){
p[i].clear();
edge[i].clear();
e[i].clear();
}
for(int i = 1;i < n;i++){
int u, v;
cin >> u >> v;
edge[u].PB(v);
edge[v].PB(u);
}
int m;
cin >> m;
for(int i = 1;i <= m;i++){
cin >> u[i] >> v[i];
p[u[i]].PB(i);
p[v[i]].PB(-i);
}
map<pii, int> mp;
for(int i = 1;i <= m;i++){
for(int j = 1;j <= n;j++)
vis[j] = cnt[j] = 0;
queue<int> q;
q.push(u[i]);
while(!q.empty()){
int now = q.front();
q.pop();
for(int i : edge[now]){
if(!vis[i]){
vis[i] = 1;
from[i] = now;
q.push(i);
}
}
}
int tmp = v[i];
while(tmp != u[i]){
for(int j : p[tmp]){
if(abs(j) == i)
continue;
if(j < 0 && !mp.count(MP(-j, i))){
e[-j].PB(i);
mp[MP(-j, i)] = 1;
}else if(j > 0 && !mp.count(MP(i, j))){
e[i].PB(j);
mp[MP(i, j)] = 1;
}
}
tmp = from[tmp];
}
for(int j : p[tmp]){
if(abs(j) == i)
continue;
if(j < 0 && !mp.count(MP(-j, i))){
e[-j].PB(i);
mp[MP(-j, i)] = 1;
}else if(j > 0 && !mp.count(MP(i, j))){
e[i].PB(j);
mp[MP(i, j)] = 1;
}
}
}
for(int i = 1;i <= m;i++)
for(int j : e[i])
cout << i << " " << j << '\n';
for(int i = 1;i <= m;i++){
for(int j = 1;j <= m;j++)
vis[j] = 0;
if(dfs(i)){
cout << "No\n";
return;
}
}
cout << "Yes\n";
}
int main(){
IOS
CASE
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
35 ms |
74156 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
36 ms |
74172 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
36 ms |
74172 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
36 ms |
74172 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
36 ms |
74172 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
35 ms |
74088 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
35 ms |
74156 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |