hdu 3861 The King’s Problem【强连通Kosaraju+最小路径覆盖】

The King’s Problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2552    Accepted Submission(s): 917

Problem Description

In the Kingdom of Silence, the king has a new problem. There are N cities in the kingdom and there are M directional roads between the cities. That means that if there is a road from u to v, you can only go from city u to city v, but can’t go from city v to city u. In order to rule his kingdom more effectively, the king want to divide his kingdom into several states, and each city must belong to exactly one state. What’s more, for each pair of city (u, v), if there is one way to go from u to v and go from v to u, (u, v) have to belong to a same state. And the king must insure that in each state we can ether go from u to v or go from v to u between every pair of cities (u, v) without passing any city which belongs to other state.

Now the king asks for your help, he wants to know the least number of states he have to divide the kingdom into.

Input

The first line contains a single integer T, the number of test cases. And then followed T cases. 

The first line for each case contains two integers n, m(0 < n <= 5000,0 <= m <= 100000), the number of cities and roads in the kingdom. The next m lines each contains two integers u and v (1 <= u, v <= n), indicating that there is a road going from city u to city v.

Output

The output should contain T lines. For each test case you should just output an integer which is the least number of states the king have to divide into.

Sample Input

1

3 2

1 2

1 3

Sample Output

2

Source

2011 Multi-University Training Contest 3 - Host by BIT

 

 题目大意:

有t组数据,每组数据有n个节点,有m条有向边,我们的任务是将所有节点分成若干个区域,使得区域数最小。

对于区域的划分,要满足这样的条件:互通的两个节点必须属于一个区域,在一个区域里边的任意两点,要么u有边到达v,要么v有边到达u,任何一个节点不能同时属于两个区域。


思路:

1、首先我们先分析这样一个没有互通点的情况下的一个图:


因为点比较少,而且边也不多,我们可以很容易分析出来,最小划分的区域数为2。

我们可以让 4 5 6 2 3属于一个集合,让1自己属于一个集合,那么最小划分区域数就为2.没有更优解。


其实我们仔细分析一下不难理解出:如果让一堆点属于一个区域,这些点完全可以取自一条路径,这样我们就保证了集合内任意两点要么u能够到达v,要么v能够到达u的条件。问题转化一下,其实就是在求一个图的最小路径覆盖数。


最小路径覆盖数=n-最大二分匹配数,我们这里可以进行简单的证明:

有向图的最小路径覆盖数:

对于二分匹配得到的匹配数m,一共能够覆盖掉2m个点,我们设除了这2m个点之外,整个图中还剩下a个点没有被覆盖,这个时候我们需要a条边来覆盖这些点。

不难看出:n=2m+a,而且最小路径覆盖数==m+a,那么:m+a=n-m。


2、对于互通点一定要属于一个集合,我们就可以将互通点集看成一个点,也就是我们常说的缩点,缩点染色可以靠强连通相关算法来搞定,我们这里采用Kosaraju算法来实现缩点染色。


注意的点:ans=n-最大二分匹配数 ,这个n=sig,不等于最开始输入进来的n,这块容易思维定式缩点求完二分匹配数之后直接用n了。


AC代码:


#include<stdio.h>
#include<string.h>
#include<vector>
using namespace std;
int head[10005];
int head2[10005];
struct EdgeNode
{
    int from;
    int to;
    int next;
}e[300000],ee[300000];
int n,m,cont,cont2,cont3,sig;
vector<int >mp[10005];
int match[100005];
int vis[10005];
int num[10005];
int color[10005];
void add(int from,int to)
{
    e[cont].from=from;
    e[cont].to=to;
    e[cont].next=head[from];
    head[from]=cont++;
}
void add2(int from,int to)
{
    ee[cont2].from=from;
    ee[cont2].to=to;
    ee[cont2].next=head2[from];
    head2[from]=cont2++;
}
void init()
{
    cont=0;
    cont2=0;
    for(int i=0;i<=n;i++)mp[i].clear();
    memset(vis,0,sizeof(vis));
    memset(match,-1,sizeof(match));
    memset(head,-1,sizeof(head));
    memset(head2,-1,sizeof(head2));
    memset(color,0,sizeof(color));
    memset(num,0,sizeof(num));
}
void Dfs(int u)
{
    vis[u]=1;
    for(int k=head[u];k!=-1;k=e[k].next)
    {
        int v=e[k].to;
        if(vis[v]==0)
        {
            Dfs(v);
        }
    }
    num[sig++]=u;
}
void Dfs2(int u)
{
    vis[u]=1;
    color[u]=sig;
    for(int k=head2[u];k!=-1;k=ee[k].next)
    {
        int v=ee[k].to;
        if(vis[v]==0)
        {
            Dfs2(v);
        }
    }
}
int find(int u)
{
    vis[u]=1;
    for(int i=0;i<mp[u].size();i++)
    {
        int v=mp[u][i];
        if(vis[v]==0)
        {
            vis[v]=1;
            if(match[v]==-1||find(match[v]))
            {
                match[v]=u;
                return 1;
            }
        }
    }
    return 0;
}
void Kosaraju()
{
    sig=1;
    for(int i=1;i<=n;i++)
    {
        if(vis[i]==0)
        {
            Dfs(i);
        }
    }
    sig=0;
    memset(vis,0,sizeof(vis));
    for(int i=n;i>=1;i--)
    {
        if(vis[num[i]]==0)
        {
            sig++;
            Dfs2(num[i]);
        }
    }
    for(int i=1;i<=n;i++)
    {
        for(int k=head[i];k!=-1;k=e[k].next)
        {
            int v=e[k].to;
            if(color[i]!=color[v])
            {
                mp[color[i]].push_back(color[v]);
            }
        }
    }
    int output=0;
    for(int i=1;i<=sig;i++)
    {
        memset(vis,0,sizeof(vis));
        if(find(i))
        output++;
    }
    printf("%d\n",sig-output);
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        init();
        scanf("%d%d",&n,&m);
        for(int i=0;i<m;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            add(x,y);
            add2(y,x);
        }
        Kosaraju();
    }
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值