(beginer)DFS (桥...边-双连通分量) UVA 610 - Street Directions
来源:互联网 发布:程序员帅哥 编辑:程序博客网 时间:2024/05/16 10:36
Street Directions
According to the Automobile Collision Monitor (ACM), most fatal traffic accidents occur on two-way streets. In order to reduce the number of fatalities caused by traffic accidents, the mayor wants to convert as many streets as possible into one-way streets. You have been hired to perform this conversion, so that from each intersection, it is possible for a motorist to drive to all the other intersections following some route.
You will be given a list of streets (all two-way) of the city. Each street connects two intersections, and does not go through an intersection. At most four streets meet at each intersection, and there is at most one street connecting any pair of intersections. It is possible for an intersection to be the end point of only one street. You may assume that it is possible for a motorist to drive from each destination to any other destination when every street is a two-way street.
Input
The input consists of a number of cases. The first line of each case contains two integersn and m. The number of intersections is n ( ), and the number of streets ism. The next m lines contain the intersections incident to each of them streets. The intersections are numbered from 1 to n, and each street is listed once. If the pair is present, will not be present. End of input is indicated byn = m = 0.Output
For each case, print the case number (starting from 1) followed by a blank line. Next, print on separate lines each street as the pair to indicate that the street has been assigned the direction going from intersectioni to intersection j. For a street that cannot be converted into a one-way street, print both and on two different lines. The list of streets can be printed in any order. Terminate each case with a line containing a single `#' character.
Note: There may be many possible direction assignments satisfying the requirements. Any such assignment is acceptable.
Sample Input
7 101 21 32 43 44 54 65 76 72 53 67 91 21 31 42 43 44 55 65 77 60 0
Sample Output
11 22 43 13 64 35 25 46 46 77 5#21 22 43 14 14 34 55 45 66 77 5#
#include<iostream>#include<algorithm>#include<cstdio>#include<cstring>#include<vector>#include<stack>#include<string.h>using namespace std;const int maxn = 1000+10;struct Edge{Edge(int uu,int vv) : u(uu) , v(vv) { }int u;int v;};bool cmp(const Edge & e1 , const Edge & e2){if (e1.u==e2.u) return e1.v < e2.v;return e1.u < e2.u;}vector<int> G[maxn];vector<Edge> ans;int dfs_clock , n , m , pre[maxn];bool vis[maxn][maxn];bool is_bridge[maxn][maxn];void init(){ans.clear();memset(pre,0,sizeof(pre));dfs_clock = 0;for (int i = 1 ; i <= n ; ++i) G[i].clear();}void input(){int u , v;while (m--){scanf("%d%d",&u,&v);is_bridge[u][v] = is_bridge[v][u] = vis[u][v] = vis[v][u] = false;G[u].push_back(v);G[v].push_back(u);}}int dfs(int u,int fa){int lowu = pre[u] = ++dfs_clock;for (int i = 0 ; i < G[u].size() ; ++i){int v = G[u][i];if (v==fa) continue;if (!pre[v]){int lowv = dfs(v,u);lowu = min(lowu,lowv);if (lowv > pre[u]) {is_bridge[u][v] = is_bridge[v][u] = true;ans.push_back(Edge(u,v));ans.push_back(Edge(v,u));}} else if (lowu > pre[v]) lowu = pre[v];}return lowu;}void dfs1(int u,int fa){for (int i = 0 ; i < G[u].size() ; ++i){int v = G[u][i];if (is_bridge[u][v] || vis[u][v] || v==fa) continue;vis[u][v] = vis[v][u] = true;ans.push_back(Edge(u,v));dfs1(v,u);}}void solve(){dfs(1,-1);for (int i = 1 ; i <= n ; ++i) dfs1(i,-1);sort(ans.begin(),ans.end(),cmp);for (int i = 0 ; i < ans.size() ; ++i) printf("%d %d\n",ans[i].u,ans[i].v);}int main(){int k = 0;while (scanf("%d%d",&n,&m),n+m){++k;init();input();printf("%d\n\n",k);solve();printf("#\n");}}
- (beginer)DFS (桥...边-双连通分量) UVA 610 - Street Directions
- POJ 1515Street Directions 边双连通分量 + dfs
- (beginer)DFS (双连通分量) UVA 10765 Doves and bombs
- poj 1515 Street Directions(双连通分量)
- uva 10972(边双连通分量)
- UVA 10972(边双连通分量)
- uva 610 - Street Directions(双联通)
- POJ -- 1515 Street Directions(双连通求割边)
- uva 10972 - RevolC FaeLoN(边双连通分量)
- UVA 10972 - RevolC FaeLoN(边-双连通分量)
- UVA - 10972 RevolC FaeLoN (边双连通分量)
- UVA 610 Street Directions
- hdu 2242(边双连通分量)
- poj3352Road Construction(边双连通分量)
- 双连通分量(点+边)
- UVA 610 - Street Directions(割边)
- poj_3352 Road Construction(求桥+边双连通分量)
- 双连通分量(DCC)
- (beginer)DFS (双连通分量) UVA 10765 Doves and bombs
- (beginer)DFS (2-SAT) UVA 11294 Wedding
- (beginer)DFS (dfs) UVA 10319 Manhattan
- (beginer) DFS (二分图判定) UVA 11080 Place the Guards
- (beginer)DFS (强连通)UVA 11504 Dominos
- (beginer)DFS (桥...边-双连通分量) UVA 610 - Street Directions
- (intermediate) DFS (高精度+找环) UVA 1361 Cactus
- 免费才是王道:盘点开发者须知的25个免费数据可视化工具
- (intermediate)DFS (边-双连通) UVA 10972 RevolC FaeLoN
- (intermediate)DFS UVA 11604 General Sultan
- (intermediate)DFS (强连通+缩点+拓扑排序) UVA 11098 - Battle II
- (beginer)最短路 UVA 10603 Fill
- (beginer) 最短路 UVA 10269 Adventure of Super Mario
- select()函数以及FD_ZERO、FD_SET、FD_CLR、FD_ISSET