#include <iostream> #include <cstdio> #include <algorithm># Include <vector> #include <stack>using namespace Std;int ufs[1005],ins[1005];int find (int a) {return a==ufs[ A]?a:find (Ufs[a]);} void Union (int a,int b) {ufs[find (a)]=find (b);} BOOL Check (int a,int b) {return find (a) ==find (b);} stack<int> s;int t[1005],lowlink[1005],t=0;vector<int> mp[1005];void tarjan (int a) {ins[a]=1;t[a]= Lowlink[a]=++t;s.push (a); for (int i=0;i<mp[a].size (); i++) if (!t[mp[a][i]) {Tarjan (mp[a][i]); Lowlink[a]=min ( Lowlink[a],lowlink[mp[a][i]]);} else if (Ins[mp[a][i]]) lowlink[a]=min (lowlink[a],t[mp[a][i]); if (Lowlink[a]==t[a]) while (S.top ()!=a) {ins[a]=0; Union (S.top (), a); S.pop ();}} int main () {int n,m;cin>>n>>m;for (int i=1;i<=m;i++) {int A,b;cin>>a>>b;mp[a].push_back (b) ;} for (int i=1;i<=n;i++) Ufs[i]=i;tarjan (1), while (1) {int a,b;cin>>a>>b;if (check (b)) puts ("Yes"); Elseputs ("No");} return 0;}
Tarjan_ Strong Unicom Component