HDU 3861 The King’s Problem 强连通分量+二分匹配(难)

本文探讨了一种算法,用于解决有向图中将节点划分为最少数量集合的问题,确保同一集合内的节点间可达,并介绍了解决方案的具体实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

点击打开链接

 

The King’s Problem

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


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
 


 

Recommend
lcy

 

题意是说一个有向图将n个点划分成最少的集合个数,要求任意两个相互到达的点在同一个几何中,且属于同一个集合的任意俩个点对(u,v),至少存在一条路径,使得v对于u 可达 或者 u 对于v 可达。首先要缩点求集合的个数,然后重新构图,任意俩点,只要在同一条有向路径上,则可以属于一个集合,所以要在缩点后的图上用二分匹配找最小路径覆盖即可。

最小路径覆盖=点数-最大匹配。这里点数是缩点之后的点数。

 

#include<stdio.h>
#include<string.h>
#include<stack>
#include<vector>
#define M 5007
using namespace std;
vector<int> g1[M],g2[M];
stack<int> s;
int low[M],dfn[M],ans[M],match[M];
bool vis[M],id[M];
int n,m,cnt,cnst;

int min(int a,int b)
{
    return a<b?a:b;
}

void init()
{
    memset(vis,false,sizeof(vis));
    memset(id,false,sizeof(id));
    memset(match,-1,sizeof(match));
    for(int i=0;i<=n;i++)
    {
        g1[i].clear();
        g2[i].clear();
    }
    cnt=cnst=0;
}

void tarjan(int u)
{
    low[u]=dfn[u]=cnt++;
    vis[u]=true;
    id[u]=true;
    s.push(u);
    int v;
    for(int i=0;i<g1[u].size();i++)
    {
        v=g1[u][i];
        if(!vis[v])
        {
            tarjan(v);
            low[u]=min(low[u],low[v]);
        }
        else if(id[v])
        {
            low[u]=min(dfn[v],low[u]);
        }
    }
    if(low[u]==dfn[u])
    {
        do
        {
            v=s.top();
            id[v]=false;
            s.pop();
            ans[v]=cnst;
        }
        while(v!=u);
        cnst++;
    }

}

int dfs(int x)
{
    vector<int>::iterator it=g2[x].begin();
    for(;it!=g2[x].end();it++)
    {
        int v=*it;
        if(!vis[v])
        {
            vis[v]=true;
            if(match[v]==-1||dfs(match[v]))
            {
                match[v]=x;
                return 1;
            }
        }
    }
    return 0;
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        init();
        int a,b;
        while(m--)
        {
            scanf("%d%d",&a,&b);
            g1[a].push_back(b);
        }
        //求强连通分支
        for(int i=1;i<=n;i++)
        {
            if(!vis[i])
                tarjan(i);
        }
        //重新建图
        for(int i=1;i<=n;i++)
            for(int j=0;j<g1[i].size();j++)
                if(ans[i]!=ans[g1[i][j]])
                    g2[ans[i]].push_back(ans[g1[i][j]]);
        int count=0;
        //求最大匹配数
        for(int i=0;i<cnst;i++)
        {
            memset(vis,false,sizeof(vis));
            count+=dfs(i);
        }
        printf("%d\n",cnst-count);
    }
    return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值