http://www.hankcs.com/program/algorithm/poj-3713-transferring-sylla.html
思路和解答可以参考上面这一篇文章,很详细。这个题一开始tle了若干发,结果把临接矩阵换成临接表a了。。。。。。本来图论题有时候就会出现卡常熟的情况,如果不是需要删边删点什么的临接矩阵还是慎用。。。。。。。
#include<iostream>
#include<string>
#include<cstring>
#include<iomanip>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cmath>
#include<set>
#include<cctype>
#include<memory>
#include<cstdlib>
#include<map>
#include<list>
#include<queue>
#include<stack>
#include<climits>
#include<complex>
#include<utility>
#include<functional>
using namespace std;
#define INF 0x7fffffff
#define RE cerr<<"REdebuge"<<endl;
#define M7 1000000007
#define M9 1000000009
#define set0(a) memset((a),0,sizeof (a))
#define pb push_back
#define ifor(s,n) for(long long i=(s);i<(n);i++)
#define rep(rep_val) for(int REP_i=0;REP_i<(rep_val);REP_i++)
#define eps 1e-7
typedef long long ll;
typedef pair<int, int> Poi;
const int maxv=505;
int tarjan_clo=0;
vector<int> G[maxv];
int pos[maxv];时间戳标注的该点的位置
int low[maxv];/从该点能回到的位置最小的节点
bool cut[maxv];
bool fd=0;
bool mark[maxv];
int n,m;
int tarjan_dfs(int u,int fa){
if(fd) return INF;
int child=0;
int lowu=pos[u]=++tarjan_clo;
for(int i=0;i<G[u].size();i++){
int &v=G[u][i];
if(!pos[v]&&pos[v]!=-1){
child++;
int lowv=tarjan_dfs(v,u);
lowu=min(lowu,lowv);
if(lowv>=pos[u])
cut[u]=1;
}
else if(pos[v]<pos[u]&&pos[v]!=-1&&pos[v]!=0&&v!=fa){
lowu=min(lowu,pos[v]);
}
}
if(fa<0&&child==1) cut[u]=0;
if(cut[u]) fd=1;
return low[u]=lowu;
}
void dfs0(int u){
mark[u]=1;
for(int i=0;i<G[u].size();i++){
if(!mark[G[u][i]]){
dfs0(G[u][i]);
}
}
return;
}
bool is_connected(){
set0(mark);
dfs0(0);
bool f=1;
for(int j=0;j<n;j++) if(mark[j]==0) f=0;
return f;
}
bool solve(){
for(int rm=0;rm<n;rm++){
set0(pos);
set0(low);
set0(cut);
pos[rm]=-1;
int s=rm==0?1:0;
tarjan_clo=0;
fd=0;
tarjan_dfs(s,-1);
if(fd)
return false;
}
return true;
}
int main(){
// freopen("in","r",stdin);
while(cin>>n>>m,n){
ifor(0,maxv) G[i].clear();
rep(m){
int a,b;
scanf("%d%d",&a,&b);
G[a].pb(b);G[b].pb(a);
}
if(!is_connected()){
puts("NO");
continue;
}
if(solve())
puts("YES");
else
puts("NO");
}
return 0;
}