DFS
ACcode
// #pragma GCC optimize (2)
// #pragma G++ optimize (2)
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include<unordered_map>
#include<map>
#define endl '\n'
#define int long long
#define lowbit(x) x &(-x)
#define rep(i, a, n) for (int i = a; i <= n; i++)
#define Bookerbobo() \
{ \
ios::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0); \
}
using namespace std;
typedef long long ll;
const int N=1e6+10;
typedef pair<int,int>PII;
map<PII,int>mp;
int e[N],h[N],ne[N],cnt;
void add(int a,int b)
{
e[cnt]=b,ne[cnt]=h[a],h[a]=cnt++;
}
int s,t;
bool st[N],ans;
void dfs(int s)
{
if(s==t){ans=true;return;}
for(int i=h[s];i!=-1;i=ne[i])
{
int j=e[i];
if(st[j])continue;
st[j]=true;
dfs(j);
if(ans)return;
st[j]=false;
}
return;
}
void solve()
{
int n,m1,m2;
cin>>n>>m1>>m2;
for(int i=1;i<=n;i++){h[i]=-1;}
while(m1--)
{
int a,b;
cin>>a>>b;
mp[{a,b}]++;
}
while(m2--)
{
int a,b;
cin>>a>>b;
if(mp.count({a,b}))add(a,b);
}
ans=false;
cin>>s>>t;
st[s]=true;
dfs(s);
if(ans)cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
signed main()
{
Bookerbobo();
int T;T= 1;
//int T;cin>>T;
while (T--)
solve();
return 0;
}
BFS
ACcode
#include <bits/stdc++.h>
#define endl '\n'
#define x first
#define y second
#define PI acos(-1)
#define int long long
#define SugarT ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
const int N=2e5+10;
const int INF=0x3f3f3f3f3f3f3f3f;
const int mod=1e9+7;
const double eps = 1e-6;
typedef pair<int, int> PII;
int n,m,s,t;
int dist[N];
int h[N],e[N],w[N],ne[N],idx;
bool st[N];
map<PII,int> mp;
void add(int a,int b,int c)
{
e[idx]=b,ne[idx]=h[a],w[idx]=c,h[a]=idx++;
}
bool bfs(int s)
{
stack<int>sta;
sta.push(s);
while(sta.size())
{
auto p=sta.top();
sta.pop();
st[p]=true;
if(p==t)return true;
for(int i=h[p];~i;i=ne[i])
{
int j=e[i];
if(st[j])continue;
st[j]=true;
sta.push(j);
}
}
return false;
}
void solve()
{
memset(h,-1,sizeof h);
int n,m1,m2;
cin >> n >> m1 >> m2;
while(m1--)
{
int a,b;
cin >> a >> b;
mp[{a,b}]=1;
}
while(m2--)
{
int a,b;
cin >> a >> b;
if(mp[{a,b}])
add(a,b,1LL);
}
cin >> s >> t;
if(bfs(s))cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
signed main()
{
SugarT
int T=1;
//cin >> T;
while(T--)
solve();
return 0;
}
迪杰斯特拉
ACcode
#include <bits/stdc++.h>
#define endl '\n'
#define x first
#define y second
#define PI acos(-1)
#define int long long
#define SugarT ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
const int N=2e5+10;
const int INF=0x3f3f3f3f3f3f3f3f;
const int mod=1e9+7;
const double eps = 1e-6;
typedef pair<int, int> PII;
int n,m,s,t;
int dist[N];
int h[N],e[N],w[N],ne[N],idx;
bool st[N];
map<PII,int> mp;
void add(int a,int b,int c)
{
e[idx]=b,ne[idx]=h[a],w[idx]=c,h[a]=idx++;
}
void Dijkstra(int s,int t)
{
memset(dist,0x3f,sizeof dist);
dist[s]=0;
priority_queue<PII,vector<PII>,greater<PII>> q;
q.push({0,s});
while(q.size())
{
auto t=q.top();
q.pop();
int dis=t.x,ver=t.y;
if(st[ver])continue;
st[ver]=true;
for(int i=h[ver];i!=-1;i=ne[i])
{
auto j=e[i];
if(dist[j]>dist[ver]+w[i])
{
dist[j]=dist[ver]+w[i];
q.push({dist[j],j});
}
}
}
//for(int i=1;i<=n;i++)cout << dist[i] << endl;
if(dist[t]<0x3f3f3f3f/2)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
void solve()
{
memset(h,-1,sizeof h);
int n,m1,m2;
cin >> n >> m1 >> m2;
while(m1--)
{
int a,b;
cin >> a >> b;
mp[{a,b}]=1;
}
while(m2--)
{
int a,b;
cin >> a >> b;
if(mp[{a,b}])
add(a,b,1LL);
}
cin >> s >> t;
Dijkstra(s,t);
}
signed main()
{
SugarT
int T=1;
//cin >> T;
while(T--)
solve();
return 0;
}