HDU3861 The King’s Problem

The King’s Problem


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


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

题意:一个国王要将他的城市分成若干个州,如果两个点相互可达一定属于一个州,只有一条单向的边可达也属于一个州,问最少分多少个州。

如果两个点相互可达,这就成了一个环,所以我们先用强连通的tarjan算法把环全部找出来,然后缩点,这些点之间肯定没有环了,重新建图,所以这就成了一个DAG,然后求出这个DAG图的最小路径覆盖就是答案了,可以这么理解,如果这个点和其他的点有连线,所以要覆盖的边就要减少一条,最小路径覆盖=结点数-最大二分匹配。。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stack>
#include<vector>
using namespace std;
const int MAXN=5010;
const int MAXE=100010;
struct EDGE
{
    int v,next;
}G[MAXE];
int pre[MAXN],low[MAXN],sccno[MAXN],dfs_clock,sc_cnt;
vector<int> g[MAXN];
stack<int> S;
int head[MAXN],size;
void init()
{
    memset(pre,0,sizeof(pre));
    memset(sccno,0,sizeof(sccno));
    dfs_clock=sc_cnt=size=0;
    memset(head,-1,sizeof(head));
}
void add(int u,int v)
{
    G[size].v=v;
    G[size].next=head[u];
    head[u]=size++;
}
void tarjan(int u)
{
    low[u]=pre[u]=++dfs_clock;
    S.push(u);
    for(int i=head[u];i!=-1;i=G[i].next)
    {
        int v=G[i].v;
        if(!pre[v])
        {
            tarjan(v);
            low[u]=min(low[u],low[v]);
        }
        else if(!sccno[v])
        {
            low[u]=min(low[u],pre[v]);
        }
    }
    if(low[u]==pre[u])
    {
        sc_cnt++;
        while(1)
        {
            int x=S.top();
            S.pop();
            sccno[x]=sc_cnt;
            if(u==x)
                break;
        }
    }
}
bool vis[MAXN];
int link[MAXN];
bool dfs(int u)
{
    for(int i=0;i<g[u].size();i++)
    {
        int v=g[u][i];
        if(!vis[v])
        {
            vis[v]=1;
            if(link[v]==-1||dfs(link[v]))
            {
                link[v]=u;
                return 1;
            }
        }
    }
    return 0;
}
int main()
{
    int t,n,m,i;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        init();
        int u,v;
        while(m--)
        {
            scanf("%d%d",&u,&v);
            u--,v--;
            add(u,v);
        }
        for(i=0;i<n;i++)
            if(!pre[i])
            tarjan(i);
        for(i=1;i<=sc_cnt;i++)
            g[i].clear();
        memset(link,-1,sizeof(link));
        for(u=0;u<n;u++)
        {
            for(i=head[u];i!=-1;i=G[i].next)
            {
                v=G[i].v;
                if(sccno[u]!=sccno[v])
                {
                    g[sccno[u]].push_back(sccno[v]);
                }
            }
        }
        int sum=0;
        for(i=1;i<=sc_cnt;i++)
        {
            memset(vis,0,sizeof(vis));
            if(dfs(i))
                sum++;
        }
        printf("%d\n",sc_cnt-sum);
    }
    return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值