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);}}}}
阅读全文
0 0
- hdu 3849 求无向图的桥
- HDU 3849 By Recognizing…(求无向图的桥数目)
- HDU 3849 By Recognizing…(求无向图的桥数目)
- HDU 3849 By Recognizing…(求无向图的桥数目)
- hdu 4738 Caocao's Bridges(Tarjan求无向图的桥)
- HDU 4738 浅谈无向图边双连通求TarJAn求桥及如何“炸桥”
- hdu4738(求一个无向图的桥的最小值)
- ZOJ 2588 Burning Bridges 求无向图的桥
- acdreamOJ 1236 求无向图的桥
- 求无向图的割点和桥
- hdu4738Caocao's Bridges tarjan求无向图的桥
- hdoj 4738 tarjan求无向图的桥
- 求无向图的割点和桥
- 求无向图的 割点和桥 【模版】
- 求无向图的关节点算法
- 求无向图的连通分量
- hdu 1599 (floyd求无向图最小环)
- 求桥(无向图)
- JSP 过滤器
- centos6.8 安装redis
- 一些优秀的Android学习地址
- 1231 最优布线问题 kruskal
- 「iOS」行车服务app 「客户端、后端思路+代码」
- hdu 3849 求无向图的桥
- Arrays and generics 数组和通用 不能建立对象的几种实现
- 关于maven出现java.lang.NoSuchMethodError: org.springframework.core.io.ResourceEditor
- android中加载webview以及与JS的交互
- failed to create JVM code-6/-4
- 在仿真文件中读取或者写入txt文件
- 如何在Rancher上运行无服务器应用程序
- Spring_spring自动装配置注解(@Autowired)指定要装配置的参数(@Qualifier)
- Java接口总结