XDU1271National Disaster(求桥模板题)

Problem 1271 - National Disaster
Time Limit: 1000MS      Memory Limit: 65536KB      Difficulty:    
Total Submit: 64     Accepted: 11     Special Judge: No 

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

Caocao's Bridges

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 586    Accepted Submission(s): 254


Problem Description
Caocao was defeated by Zhuge Liang and Zhou Yu in the battle of Chibi. But he wouldn't give up. Caocao's army still was not good at water battles, so he came up with another idea. He built many islands in the Changjiang river, and based on those islands, Caocao's army could easily attack Zhou Yu's troop. Caocao also built bridges connecting islands. If all islands were connected by bridges, Caocao's army could be deployed very conveniently among those islands. Zhou Yu couldn't stand with that, so he wanted to destroy some Caocao's bridges so one or more islands would be seperated from other islands. But Zhou Yu had only one bomb which was left by Zhuge Liang, so he could only destroy one bridge. Zhou Yu must send someone carrying the bomb to destroy the bridge. There might be guards on bridges. The soldier number of the bombing team couldn't be less than the guard number of a bridge, or the mission would fail. Please figure out as least how many soldiers Zhou Yu have to sent to complete the island seperating mission.


 

Input
There are no more than 12 test cases.

In each test case:

The first line contains two integers, N and M, meaning that there are N islands and M bridges. All the islands are numbered from 1 to N. ( 2 <= N <= 1000, 0 < M <= N 2 )

Next M lines describes M bridges. Each line contains three integers U,V and W, meaning that there is a bridge connecting island U and island V, and there are W guards on that bridge. ( U ≠ V and 0 <= W <= 10,000 )

The input ends with N = 0 and M = 0.


 

Output
For each test case, print the minimum soldier number Zhou Yu had to send to complete the mission. If Zhou Yu couldn't succeed any way, print -1 instead.


 

Sample Input
3 3
1 2 7
2 3 4
3 1 4
3 2
1 2 7
2 3 4
0 0


 

Sample Output
-1
4
/*****************************************************************
* 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次。。。。。。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值