【2017新疆网络赛】F Islands 强连通分量tarjan hdu2767原题

20 篇文章 0 订阅
8 篇文章 0 订阅

On the mysterious continent of Tamriel, there is a great empire founded by human.

To develope the trade, the East Empire Company is set up to transport goods from place to place.

Recently, the company wants to start their business in Solstheim, which is consists of NN islands.

Luckily, there are already MM sea routes.

All routes are one-way, and the ii-th route can transport person and goods from island uiui to vivi.

Now, the company nominates you a particular job to plan some new routes to make sure that person and goods can be transported between any two islands.

Furthermore, because the neighboring regions are under attack by an increasing number of dragons, limited resources can be used to set up new routes.

So you should plan to build new routes as few as possible.

Input Format

The first line contains an integer TT, indicating that there are TT test cases.

For each test case, the first line includes two integers N (N≤10000)N (N10000) and M (M≤100000)M (M100000), as described above.

After that there are MM lines. Each line contains two integers uiui and vivi.

Output Format

For each test case output one integer, represent the least number of routes required to new.

样例输入
2
4 3
1 2
2 3
3 4
4 4
1 2
1 4
3 2
3 4
样例输出
1
2

题意:

给n个点和m条有向边,问最少再加几条边使其变成强连通图。

思路:

先tarjan跑一遍,如果是强连通图就输出0。否则输出max(入度为0的点个数,出度为0的点个数)。


居然是hdu2767的原题,以前还做过。。。。

http://blog.csdn.net/zchahaha/article/details/51223516


#include <iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#define N 22000
#define M 110000

using namespace std;

struct node
{
    int to,next;
}e[M];

int dfn[N],low[N],head[N],in[N],out[N],cnt,scnt,top,q[N],v[N],belong[N],n,m;

void init()
{
    memset(head,-1,sizeof(head));
    memset(dfn,0,sizeof(dfn));
    memset(low,0,sizeof(low));
    memset(in,0,sizeof(in));
    memset(out,0,sizeof(out));
    memset(belong,0,sizeof(belong));
    cnt=scnt=top=0;
}

void add_edge(int u,int v)
{
    e[cnt].to=v;
    e[cnt].next=head[u];
    head[u]=cnt++;
}

void tarjan(int u)
{
    int t;
    low[u]=dfn[u]=cnt++;
    q[top++]=u;
    v[u]=1;
    for(int i=head[u];i+1;i=e[i].next)
    {
        int c=e[i].to;
        if(!dfn[c])
        {
            tarjan(c);
            low[u]=min(low[u],low[c]);
        }
        else if(v[c])
            low[u]=min(low[u],dfn[c]);
    }
    if(low[u]==dfn[u])
    {
        scnt++;
        do
        {
            t=q[--top];
            v[t]=0;
            belong[t]=scnt;
        }while(t!=u);
    }
}

void solve()
{
    for(int i=1;i<=n;i++)
        if(!dfn[i]) tarjan(i);
    if(scnt==1)
    {
        cout<<0<<endl;
        return ;
    }
    for(int i=1;i<=n;i++)
        for(int j=head[i];j+1;j=e[j].next)
        {
            int t=e[j].to;
            if(belong[i]!=belong[t])
            {
                in[belong[t]]++;
                out[belong[i]]++;
            }
        }
    int a=0,b=0;
    for(int i=1;i<=scnt;i++)
    {
        if(in[i]==0)    a++;
        if(out[i]==0)   b++;
    }
    cout<<max(a,b)<<endl;
}

int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        cin>>n>>m;
        init();
        for(int i=0;i<m;i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            add_edge(u,v);
        }
        solve();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值