1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
|
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int mod = 1e9 + 7; const int N = 2e5 + 10; int n,a,b,d[N]; bool vis[N]; ll dist1[N],dist2[N]; vector<int> e[N]; bool vis2[N]; int tot,l[N]; void toposort() { queue<int> q; tot = 0; for(int i = 1;i <= n; i++) vis2[i] = 0; for(int i = 1; i <= n; i++) if(d[i] == 1) q.push(i),vis2[i] = true; while(!q.empty()) { int u = q.front(); q.pop(); for(auto v : e[u]) if(--d[v] <= 1 && !vis2[v]) q.push(v),vis2[v] = true; }
}
int main() { ios::sync_with_stdio(false); cin.tie(nullptr), cout.tie(nullptr); int t; cin>>t; while(t--) { cin>>n>>a>>b;
for(int i = 1;i <= n; i++) e[i].clear(),dist1[i] = 0,dist2[i] = 0,d[i] = 0; for(int i = 1; i <= n; i++) { int x , y; cin>>x>>y; e[x].push_back(y); e[y].push_back(x); d[y]++; d[x]++; } toposort(); queue<pair<int,ll>>q; q.push({a,0}); dist1[a] = 0; memset(vis,false,sizeof(vis)); vis[a] = true;
while(!q.empty()) { auto i = q.front(); q.pop(); int u = i.first,dis = i.second; for(auto v : e[u]) { if(!vis[v]) { vis[v] = true; dist1[v] = dis + 1; q.push({v,dist1[v]}); } } } q.push({b,0}); dist2[b] = 0; memset(vis,false,sizeof(vis)); vis[b] = true;
while(!q.empty()) { auto i = q.front(); q.pop(); int u = i.first,dis = i.second; for(auto v : e[u]) { if(!vis[v]) { vis[v] = true; dist2[v] = dis + 1; q.push({v,dist2[v]}); } } } bool ok = false; for(int i = 1;i <= n;i++) { if(d[i]>1&&dist1[i]>dist2[i])ok = true; } if(ok)cout<<"YES\n"; else cout<<"NO\n"; } return 0; }
|