Description
There are N cities in Country X whose number is from 0 to N-1. There are M bi-direction roads connecting these N cities. The citizen can reach to any city through some path.
Country Y declared a war to Country X. Its first purpose is to cut the connection of the cities among Country X. Country Y has a special attacking weapon which can make a certain road disappear. Once the cities cannot be connected, Country Y can attack Country X easily. But it’s a pity that this weapon can only be used once.
Country X has a special defense weapon which can defend such attack. The method is easy. By putting the special weapon on the certain road, the attack can be avoided.
Input
The input consists of several test cases.
The first line of the input contains a single integer T (0 < T ≤ 20), the number of test cases.
The first line of each test case contains two integers N and M, which indicate number of cities and roads in Country X respectively (0 < N ≤ 10000, 0 < M ≤ 100000).
In the following M lines, each line contains two integers, which shows the number of two cities connected by road i.
The input data guarantee the initial graph is connected and no duplicate roads.
Output
For each test case, output an integer indicating the minimum number of roads which need such defending weapon.
Sample Input
2
2 1
0 1
3 3
0 1
1 2
2 0
Sample Output
1
0
Hint
/********************************************************************* * author:kuangbin * date:2013/09/16 * PS:划划水,自己懒了一下,无耻的用斌神的万能模板过的,我就稍微改了一点点。 * PS:附带bin神blog链接:http://www.cnblogs.com/kuangbin/,欢迎访问 ************************************************************************/ #include <cstdio> #include <algorithm> #include <iostream> #include <cstring> using namespace std; /* * 求 无向图的割点和桥 * 可以找出割点和桥,求删掉每个点后增加的连通块。 * 需要注意重边的处理,可以先用矩阵存,再转邻接表,或者进行判重 */ const int MAXN=10010; const int MAXM=100010; struct Edge { int to,next; bool cut;//是否为桥的标记 }edge[MAXM]; int head[MAXN],tot; int Low[MAXN],DFN[MAXN],Stack[MAXN]; int Index,top; bool Instack[MAXN]; bool cut[MAXN]; int bridge; void addedge(int u,int v) { edge[tot].to=v; edge[tot].next=head[u]; edge[tot].cut=false; head[u]=tot++; } void Tarjan(int u,int pre) { int v; Low[u]=DFN[u]=++Index; Stack[top++]=u; Instack[u]=true; int son=0; for(int i=head[u];i!=-1;i=edge[i].next) { v =edge[i].to; if(v== pre)continue; if(!DFN[v]) { son++; Tarjan(v,u); if(Low[u]>Low[v]) Low[u]=Low[v]; //桥 //一条无向边(u,v)是桥,当且仅当(u,v)为树枝边,且满足DFS(u)<Low(v)。 if(Low[v]>DFN[u]) { bridge++; edge[i].cut=true; edge[i^1].cut=true; } //割点 //一个顶点u是割点,当且仅当满足(1)或(2) (1) u为树根,且u有多于一个子树。 //(2) u不为树根,且满足存在(u,v)为树枝边(或称父子边, //即u为v在搜索树中的父亲),使得DFS(u)<=Low(v) if(u!=pre&&Low[v]>=DFN[u])//不是树根 { cut[u]=true; } } else if(Low[u]>DFN[v]) Low[u]=DFN[v]; } //树根,分支数大于1 if(u==pre&&son>1)cut[u]=true; Instack[u]=false; top--; } void solve(int n) { memset(DFN,0,sizeof(DFN)); memset(Instack,false,sizeof(Instack)); memset(cut,false,sizeof(cut)); Index=top=0; bridge=0; for(int i=1;i<=n;i++) if(!DFN[i]) Tarjan(i,i); // int ans=0; // for(int i=1;i<=n;i++) // if(cut[i]) // ans++; //printf("%d\n",ans);此处是求割点的个数; printf("%d\n",bridge); } void init() { tot = 0; memset(head,-1,sizeof(head)); } int main() { int n,test,m,u,v; scanf("%d",&test); while(test--) { init(); scanf("%d%d",&n,&m); for(int i=1;i<=m;i++) { scanf("%d%d",&u,&v); addedge(u,v); addedge(v,u); } solve(n); } return 0; } /*1Y,ORZ bin神*/
HDU4738
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 586 Accepted Submission(s): 254
/***************************************************************** * problem:Caocao's bridge * source:2013 ACM/ICPC Asia Regional Hangzhou Online * author:sgx * date:2013/09/17 * 关键点:1.考虑重边2.守卫人数为0时,需要输出1,即需要1人放炸弹 * 3.图本来就联通,输出-1 4.图不连通,不需要炸,输出0就行了. ******************************************************************/ #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #define INF 99999999999 using namespace std; const int maxn=1000+5; struct Edge { int to; int next; bool cut; int cost; }edge[maxn*maxn]; int low[maxn],dfn[maxn],stack[maxn]; bool instack[maxn]; int head[maxn]; bool iscut[maxn]; int bridge; int cnt,index,son_num,top; int res; int n,m,step; inline int max(int a,int b) { return a>b?a:b; } inline int min(int a,int b) { return a<b?a:b; } inline void addedge(int u,int v,int cost) { edge[cnt].to=v; edge[cnt].next=head[u]; edge[cnt].cost=cost; head[u]=cnt++; edge[cnt].cut=false; } inline void makemap(int a,int b,int w) { addedge(a,b,w); addedge(b,a,w); } inline void tarjan(int u,int father) { dfn[u]=low[u]=++index; stack[top++]=u; instack[u]=true; int connect_father=0;//与父节点相连的边的数量,用来判断重边; for(int i=head[u];~i;i=edge[i].next) { int v=edge[i].to; if(v==father&&connect_father==0) { connect_father++; continue; } if(!dfn[v]) { son_num++; tarjan(v,u); low[u]=min(low[u],low[v]); if(low[v]>dfn[u]) { bridge++; edge[i].cut=true; edge[i^1].cut=true; res=min(res,edge[i].cost); //printf("当前的花费是:%d\n",res); } if(low[v]>=dfn[u]&&u!=father) { iscut[u]=true; } } else { low[u]=min(low[u],dfn[v]); } } if(u==father&&son_num>1) iscut[u]=true; instack[u]=false; top--; step++; } inline void init() { res=INF; cnt=0; memset(head,-1,sizeof(head)); } inline void solve() { top=index=son_num=bridge=step=0; memset(low,0,sizeof(low)); memset(dfn,0,sizeof(dfn)); memset(iscut,0,sizeof(iscut)); memset(instack,0,sizeof(instack)); memset(stack,0,sizeof(stack)); // for(int i=1;i<=n;i++) // { // if(!dfn(i)) // tarjan(i,i); // } tarjan(1,-1); int ans=0; for(int i=1;i<=n;i++) { if(iscut[i]) ans++; }//求出割点; //printf("%d\n",ans); //printf("%d\n",bridge);//求桥; } int main() { while(scanf("%d%d",&n,&m)!=EOF) { if(n==0&&m==0) break; init(); while(m--) { int a,b,w; scanf("%d%d%d",&a,&b,&w); makemap(a,b,w);//建图; } solve();//用tarjan求解; if(step<n) printf("0\n");//图不连通,不需要炸; else if(bridge==0) printf("-1\n");//如果原图没有桥,图是一个双连通图; else if(res==0) printf("1\n");//求得桥的最少守卫为0,需要一个士兵带炸弹去; else printf("%d\n",res); } return 0; }
WA了11次。。。。。。。。