lightoj 1063 - Ant Hills(强连通求割点)

After many years of peace, an ant-war has broken out.

In the days leading up to the outbreak of war, the ant government devoted a great deal of resources toward gathering intelligence on ant hills. It discovered the following:

1.      The ant empire has a large network of ant-hills connected by bidirectional tracks.

2.      It is possible to send a message from any ant hill to any other ant hill.

Now you want to stop the war. Since they sometimes attack your house and disturb you quite a lot. So, you have made a plan. You have a gun which can destroy exactly one ant-hill. So, you want to hit an ant hill if it can stop at least two other ant hills passing messages between them. Now you want the total number of ant hills you may choose to fire.

Input

Input starts with an integer T (≤ 20), denoting the number of test cases.

Each test case contains a blank line and two integers n (1 ≤ n ≤ 10000), m (1 ≤ m ≤ 20000)n denotes the number of ant hills and m denotes the number of bi-directional tracks. Each of the next m lines will contain two different integers a b (1 ≤ a, b ≤ n) denoting that there is a track between a and b.

Output

For each case, print the case number and the total number of ant hills you may choose to fire.

Sample Input

Output for Sample Input

2

 

5 4

2 1

1 3

5 4

4 1

 

3 3

1 2

2 3

1 3

Case 1: 2

Case 2: 0



这题的大意就是给你n个点,m条边,然后让你破坏某个点使得至少其他两个点不连通,就是求用强连通割点。

#include<set>
#include<stack>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define endl '\n'
using namespace std;

const int maxn = 10010;

vector<int> v[maxn];
stack<int> s;
set<int> cut;
int low[maxn],DFN[maxn],vis[maxn];
int Belong[maxn];//Belong数组的值是1~scc
int num[maxn];//各个强连通分量包含点的个数,数组编号1~scc
int id;
int scc;//强连通分量的个数
void Tarjan(int x,int fa)
{
    low[x]  = DFN[x] = ++id;
    vis[x] = 1;
    s.push(x);
    int son = 0;
    for(int i=0;i<v[x].size();i++)
    {
        int xx = v[x][i];
        if(!DFN[xx])
        {
            Tarjan(xx,x);
            low[x]=min(low[x],low[xx]);
            //求割点
            if (fa == -1 && son != 0)
                cut.insert(x);
            if (fa != -1 && low[xx] >= DFN[x])
                cut.insert(x);
            son++;
        }
        else if(vis[xx])
            low[x]=min(low[x],DFN[xx]);
    }
    if(low[x]==DFN[x])
    {
        scc++;
        while(s.size())
        {
            int xx=s.top();
            s.pop();
            num[scc]++;
            Belong[xx] = scc;
            vis[xx] = 0;
            if(xx==x)
                break;
        }
    }
}
void solve(int n)
{
    for(int i = 1;i <= n;i++)
        if(!DFN[i])
            Tarjan(i,-1);
}
void init()
{
    memset(DFN,0,sizeof(DFN));
    memset(num,0,sizeof(num));
    memset(vis,0,sizeof(vis));
    while(s.size())
        s.pop();
    cut.clear();
    for(int i=0;i<=maxn;i++)
        v[i].clear();
    scc = id = 0;
}
int main(void)
{
    int T,n,m;
    scanf("%d",&T);
    int cas = 1;
    while(T--)
    {
        scanf("%d%d",&n,&m);
        init();
        for(int i=1;i<=m;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            v[x].push_back(y);
            v[y].push_back(x);
        }
        solve(n);
        printf("Case %d: %d\n",cas++,cut.size());
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值