- 题目链接 : HDU 1269
- 题意: 给定有向边,问是否两两可达,即整个图是个强连通图(强连通分量=1)
- 解法: 直接上tarjan板子求解
#include<bits/stdc++.h>
#define rep(i, a, b) for(int i=a; i<=b; ++i)
#define repp(i, a, b) for(int i=b; i>=a; --i)
using namespace std;
const int maxn = 1e4+5;
struct edge{
int to, next;
}node[maxn*10];
int first[maxn], len;
void adde(int u, int v){
node[len].to = v;
node[len].next = first[u];
first[u] = len++;
return ;
}
bool ins[maxn];
int n, m, ans, cnt;
int dfn[maxn], low[maxn];
int s[maxn], top;
void tarjan(int u){
s[++top] = u;
ins[u] = 1;
low[u] = dfn[u] = ++cnt;
for(int i=first[u]; ~i; i=node[i].next){
int v = node[i].to;
if(!dfn[v]) {
tarjan(v);
low[u] = min(low[u], low[v]);
} else if(ins[v]){
low[u] = min(low[u], dfn[v]);
}
}
if(dfn[u] == low[u]){
++ans;
int j =-1;
while(j!=u && top){
j = s[top--];
ins[j] = 0;
}
}
}
int main(){
while(~scanf("%d %d", &n, &m), n||m){
int u, v;
len = 0, ans=0, cnt=0, top=0;
memset(ins, false, sizeof(ins));
memset(dfn, 0, sizeof(dfn));
memset(first, -1, sizeof(first));
rep(i, 1, m){
scanf("%d %d", &u, &v);
adde(u, v);
}
rep(i, 1, n) {
if(dfn[i]==0) tarjan(i);
}
if(ans==1) printf("Yes\n");
else printf("No\n");
}
return 0;
}