POJ 3713 Transferring Sylla(图的三联通,tarjan算法
2015-04-01 19:54
309 查看
http://www.hankcs.com/program/algorithm/poj-3713-transferring-sylla.html
思路和解答可以参考上面这一篇文章,很详细。这个题一开始tle了若干发,结果把临接矩阵换成临接表a了。。。。。。本来图论题有时候就会出现卡常熟的情况,如果不是需要删边删点什么的临接矩阵还是慎用。。。。。。。
思路和解答可以参考上面这一篇文章,很详细。这个题一开始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; }
相关文章推荐
- POJ 3713 Transferring Sylla (三连通图)
- POJ 3713 Transferring Sylla 已翻译
- [POJ 3713] Transferring Sylla (枚举删点+Tarjan求割点)
- tarjan算法 求解强联通分量 POJ_2186_Popular Cows
- poj 3713 三联通图
- POJ 2186 Popular Cows(强联通分量缩点+tarjan算法)
- POJ 2186 popular cow 有向图的强联通问题 Tarjan算法
- POJ 2186 popular cow 有向图的强联通问题 Tarjan算法
- 【POJ 1236 Network of Schools】强联通分量问题 Tarjan算法,缩点
- poj 3177 边双联通 **
- Network of Schools+强联通分量+POJ
- POJ 2375 强联通缩点+判断
- HDU 1269 迷宫城堡 (强联通分量,Tarjan算法)
- poj2762 Going from u to v or from v to u?(强联通+拓扑排序)
- poj-1236-Network of Schools-强联通分量
- 无向图 求加几条边可以得到双联通图 poj 3177
- Tarjan算法---强联通分量
- poj Going from u to v or from v to u? 强联通缩点+拓扑排序(或搜索)
- POJ-3713-Transferring Sylla
- poj 2942 (点双联通+判断二分图)