题目描述
因为某国被某红色政权残酷的高压暴力统治。美国派出将军uim,对该国进行战略性措施,以解救涂炭的生灵。
该国有n个城市,这些城市以铁路相连。任意两个城市都可以通过铁路直接或者间接到达。
uim发现有些铁路被毁坏之后,某两个城市无法互相通过铁路到达。这样的铁路就被称为key road。
uim为了尽快使该国的物流系统瘫痪,希炸毁铁路,已达到存在某两个城市无法互相通过铁路到达的效果。
然而,只有一发炮弹(美国国会不给钱了)。所以,他能轰炸那一条铁路呢?
输入输出格式
输入格式:第一行n,m(1<=n<=150, 1<=m<=5000),分别表示有n个城市,总共m条铁路。
以下m行,每行两个整数a, b,表示城市a和城市b之间有铁路直接连接。
输出格式:输出有若干行。
每行包含两个数字a,b(a<b),表示<a,b>是key road。
请注意:输出时,所有的数对<a,b>必须按照a从小到大排序输出;如果a相同,则根据b从小到大排序。
输入输出样例
输入样例#1:
6 6 1 2 2 3 2 4 3 5 4 5 5 6
输出样例#1:
1 2 5 6 裸的tarjan求桥,好久没有写,看了一个小时。#include<algorithm> #include<iostream> #include<cstdio> using namespace std; const int N=155; const int M=5005; int n,m,cnt,tim,tp,hd[N],dfn[N],low[N]; struct edge { int to,nxt; }v[2*M]; struct edg { int x,y; }ans[2*M]; bool cmp(edg c,edg d) { if(c.x==d.x) return c.y<d.y; return c.x<d.x; } void addedge(int x,int y) { ++cnt; v[cnt].to=y; v[cnt].nxt=hd[x]; hd[x]=cnt; } void tarjan(int u,int fa) { dfn[u]=low[u]=++tim; for(int i=hd[u];i;i=v[i].nxt) if(!dfn[v[i].to]) { tarjan(v[i].to,u); low[u]=min(low[u],low[v[i].to]); } else if(v[i].to!=fa) low[u]=min(low[u],dfn[v[i].to]); for(int i=hd[u];i;i=v[i].nxt) if(v[i].to!=fa) if(low[v[i].to]>dfn[u]) ++tp,ans[tp].x=u,ans[tp].y=v[i].to; } int main() { scanf("%d%d",&n,&m); for(int i=1;i<=m;i++) { int x,y; scanf("%d%d",&x,&y); addedge(x,y); addedge(y,x); } tarjan(1,0); sort(ans+1,ans+tp+1,cmp); for(int i=1;i<=tp;i++) printf("%d %d\n",ans[i].x,ans[i].y); return 0; }