hdu 3849 求无向图的桥

来源:互联网 发布:工作备忘录 软件 编辑:程序博客网 时间:2024/05/16 14:45
#include<cstdio>#include<cstring>#include<map>#define MIN(x,y) ((x)>(y)?(y):(x))using namespace std;int n,m; struct node{char str[20];bool operator<(const node &b)const{return strcmp(str,b.str)<0;}};map<node,int>has;struct edge{node u,v;bool flag;}b[201000];struct node1{int to,next;}e[201000];int head[201000],cnt;int pre[10200],d[10200];void add_edge(int from,int to){e[cnt].to=to;e[cnt].next=head[from];head[from]=cnt++;//printf("%d %d %d\n",e[cnt-1].to,e[cnt-1].next,head[from]); }void dfs(int u,int v,int depth){pre[u]=d[u]=depth;//printf("in %d\n",u); for(int i=head[u];i!=-1;i=e[i].next){int to=e[i].to;//printf("u=%d to=%d\n",u,to);if(to==v)continue;if(!d[to]){dfs(to,u,depth+1);pre[u]=MIN(pre[u],pre[to]);}elsepre[u]=MIN(pre[u],d[to]);}} int main(){int T;scanf("%d",&T);while(T--){scanf("%d%d",&n,&m);has.clear();memset(d,0,sizeof(d));memset(head,-1,sizeof(head));cnt=0;int tot=0;for(int i=0;i<m;i++){b[i].flag=0;scanf("%s%s",b[i].u.str,b[i].v.str);if(has.find(b[i].u)==has.end())has[b[i].u]=++tot;if(has.find(b[i].v)==has.end())has[b[i].v]=++tot;int x=has[b[i].u],y=has[b[i].v];//printf("x=%d y=%d\n",x,y);add_edge(x,y);add_edge(y,x);}//printf("cnt=%d\n",cnt); dfs(1,-1,1);//for(int i=1;i<=n;i++)//printf("%d ",d[i]);//printf("\n");//for(int i=1;i<=n;i++)//printf("%d ",pre[i]);//printf("\n");int sym=0;for(int i=1;i<=n;i++)if(!d[i]){sym=1;//printf("here\n");break;}if(sym)printf("0\n");else{int res=0;for(int i=0;i<m;i++){int u=has[b[i].u],v=has[b[i].v];if(pre[u]>d[v]||pre[v]>d[u]){b[i].flag=1;res++;}}printf("%d\n",res);for(int i=0;i<m;i++)if(b[i].flag){printf("%s %s\n",b[i].u.str,b[i].v.str);}}}}

原创粉丝点击