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

来源:互联网 发布:淘宝大学需要多少学费 编辑:程序博客网 时间:2024/06/16 20:54

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~sccint num[maxn];//各个强连通分量包含点的个数,数组编号1~sccint 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