hdu 3861 The King’s Problem

本文介绍了一种结合Tarjan缩点算法与匈牙利算法解决王国划分问题的方法。具体地,该问题涉及在一个有向图中找到最少的连通分量数,使得从任一城市出发都能到达其他所有城市,并且每个城市只属于一个州。通过Tarjan算法进行缩点,再使用匈牙利算法求解最大匹配,最终确定最少的州数。

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


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
 
 
Tarjan缩点+匈牙利算法
Tarjan写错了。。
#include <cstring>
#include <cstdio>
#define N 100005

struct Edge
{
    Edge *next;
    int to;
}*head[N],edge[N];
struct EDge
{
    EDge *next;
    int to;
}*newhead[N],newedge[N];
bool instack[N],vis[N];
int cnt,T,n,m,ans,stack[N],top,low[N],dfn[N],tim,col[N],sumcol,f[N];
inline void init()
{
    ans=top=tim=sumcol=cnt=0;
    memset(f,-1,sizeof(f));
    memset(col,0,sizeof(col));
    memset(low,0,sizeof(low));
    memset(dfn,0,sizeof(dfn));
    memset(head,0,sizeof(head));
    memset(edge,0,sizeof(edge));
    memset(newhead,0,sizeof(newhead));
    memset(newedge,0,sizeof(newedge));
}
struct node
{
    int x,y;
}e[N<<10];
inline int min(int a,int b) {return a>b?b:a;}
void tarjan(int x)
{
    low[x]=dfn[x]=++tim;
    instack[x]=1;
    stack[++top]=x;
    for(Edge * u=head[x];u;u=u->next)
    {
        int v=u->to;
        if(instack[v]) low[x]=min(low[x],dfn[v]);
        else if(!dfn[v])
        {
            tarjan(v);
            low[x]=min(low[x],low[v]);
        }
    }
    if(low[x]==dfn[x])
    {
        int k;
        sumcol++;
        do
        {
            k=stack[top--];
            instack[k]=false;
            col[k]=sumcol;
        }while(k!=x);
    }
}
bool dfs(int x)
{
    for(EDge * u=newhead[x];u;u=u->next)
    {
        int v=u->to;
        if(!vis[v])
        {
            vis[v]=1;
            if(f[v]==-1||dfs(f[v]))
            {
                f[v]=x;
                return 1;
            }
        }
    }
    return 0;
}
inline void ins(int u,int v)
{
    edge[++cnt].next=head[u];
    edge[cnt].to=v;
    head[u]=edge+cnt;
}
inline void insnew(int u,int v)
{
    newedge[++cnt].next=newhead[u];
    newedge[cnt].to=v;
    newhead[u]=newedge+cnt;
}
int Main()
{
    scanf("%d",&T);
    for(;T--;)
    {
        scanf("%d%d",&n,&m);
        init();
        for(int i=1;i<=m;++i)
        {
            scanf("%d%d",&e[i].x,&e[i].y);
            ins(e[i].x,e[i].y);
        }
        for(int i=1;i<=n;++i)
         if(!dfn[i]) tarjan(i);
        cnt=0;
        for(int i=1;i<=m;++i)
        {
            int cx=col[e[i].x],cy=col[e[i].y];
            if(cx!=cy) insnew(cx,cy);
        }
        for(int i=1;i<=sumcol;++i)
        {
            memset(vis,0,sizeof(vis));
            if(dfs(i)) ans++;
        }
        printf("%d\n",sumcol-ans);
    }
    return 0;
}
int sb=Main();
int main(int argc,char *argv[]) {;}

 

转载于:https://www.cnblogs.com/ruojisun/p/7528209.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值