HDU - 3861_The King’s Problem(强联通分量+最小路径覆盖)

HDU - 3861
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
题意
有n个城市,在任意两个城市之间存在m条单向路,现在king想要将这n个城市划分成若干个州,如果有两个城市(u,v)可以互相到达(即同时存在从u到v和从v到u)的两条单向路,那么这两各城市必须在同一个州中,同一个州中的城市必须满足可以从一个城市出发不经过其他州到达另本州的一个城市,问最少可以分多少个州。
思路
一开始没看懂题意,以为只有能够互相到达才可以组成一个州,那不就是简单的强连通分量呗,结果套上模板跑了一遍样例,结果发现样例跑的结果都不对,搞了半天才明白,是相互到达必在同一州,而根据上文的“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. ”可知,只要满足有路径连接就可以划为一块,因为上文用的是"or";搞明白了这一点,思路就很明确了,这个任意到达很明显是二分图的最小路径覆盖,因为存在环,所以要先用强连通分量跑一边缩点,然后套模板就可以了。
相关内容
贴篇我个人觉得讲的不错的博客:
最小路径覆盖
强连通分量

最后,惯例贴代码

#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<vector>
using namespace std;
const int pot=5004;
const int sid=100004;
const int inf=0x3f3f3f3f;
struct node
{
    int v,next;
}edg[sid];
int head[pot],low[pot],dfn[pot],fstack[pot],belong[pot];
int index,top,cnt,scc;
bool instack[pot];
int sum[pot],vis[pot],use[pot],heading[pot];
void add(int u,int v)
{
    edg[cnt].v=v;
    edg[cnt].next=head[u];
    head[u]=cnt++;
}
void tarjan(int u)
{
    int v;
    low[u]=dfn[u]=++index;
    fstack[top++]=u;
    instack[u]=1;
    for(int s=head[u];~s;s=edg[s].next)
    {
        v=edg[s].v;
        if(!dfn[v])
        {
            tarjan(v);
            if(low[u]>low[v])low[u]=low[v];
        }
        else if(instack[v]&&low[u]>dfn[v])
        {
            low[u]=dfn[v];
        }
    }
    if(low[u]==dfn[u])
    {
        scc++;
        do
        {
            v=fstack[--top];
            instack[v]=0;
            belong[v]=scc;
            sum[scc]++;
        }while(v!=u);
    }
}
void solve(int n)
{
    memset(dfn,0,sizeof dfn);
    memset(instack,0,sizeof instack);
    memset(sum,0,sizeof sum);
    index=scc=top=0;
    for(int s=1;s<=n;s++)
    {
        if(!dfn[s])
        {
            tarjan(s);
        }
    }
}
int pcnt;
void init()
{
    cnt=0;pcnt=0;
    memset(head,-1,sizeof head);
    memset(heading,-1,sizeof heading);
}
struct fucking
{
    int vv,net;
}fuck[sid];
void padd(int u,int v)
{
    fuck[pcnt].vv=v;
    fuck[pcnt].net=heading[u];
    heading[u]=pcnt++;
}
bool findz(int x)
{
    for(int i=heading[x];~i;i=fuck[i].net)
    {
        int v=fuck[i].vv;
        if(!vis[v])
        {
            vis[v]=1;
            if(use[v]==0||findz(use[v]))
            {
                use[v]=x;
                return 1;
            }
        }
    }
    return 0;
}
int puf()
{
    memset(use,0,sizeof use);
    int ans=0;
    for(int s=1;s<=scc;s++)
    {
        memset(vis,0,sizeof vis);
        if(findz(s))ans++;
    }
    return ans;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,m;
        scanf("%d %d",&n,&m);
        init();
        for(int i=1;i<=m;i++)
        {
            int a,b;
            scanf("%d %d",&a,&b);
            add(a,b);
        }
        solve(n);
        for(int k=1;k<=n;k++)
        {
            for(int j=head[k];~j;j=edg[j].next)
            {
                int v=edg[j].v;
                if(belong[v]!=belong[k])
                {
                    padd(belong[k],belong[v]);
                }
            }
        }
        int ans=puf();
        ans=scc-ans;
        printf("%d\n",ans);
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值