hdu 3861 The King’s Problem trajan缩点+二分图匹配

本文介绍了一道编程题目“国王的问题”,任务是将王国划分成尽可能少的州,使得每个城市恰好属于一个州,并且如果两个城市间存在相互可达的路径,则它们必须属于同一个州。文章提供了完整的代码实现,包括使用Tarjan算法寻找强连通分量以及求解无环DAG图的最小路径覆盖。

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

The King’s Problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)



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

 题意:给你n个点,m条边,可以将一个单联通分量缩成一个点,最少能分成几个点;

思路:先将强连通分量缩点,强连通肯定是可以合并成一个点,然后求无环DAG图的最小路径覆盖即可;

#include<iostream>
#include<cstdio>
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
#include<bitset>
using namespace std;
#define LL unsigned long long
#define pi (4*atan(1.0))
#define eps 1e-4
#define bug(x)  cout<<"bug"<<x<<endl;
const int N=5e3+10,M=1e5+10,inf=1e9+10;
const LL INF=1e18+10,mod=2147493647;

struct is
{
    int u,v;
    int next;
}edge[M];
int head[N];
int belong[N];
int dfn[N];
int low[N];
int stackk[N<<1];
int instack[N];
int number[N];
int n,m,jiedge,lu,bel,top;
void update(int u,int v)
{
    jiedge++;
    edge[jiedge].u=u;
    edge[jiedge].v=v;
    edge[jiedge].next=head[u];
    head[u]=jiedge;
}
void dfs(int x)
{
    dfn[x]=low[x]=++lu;
    stackk[++top]=x;
    instack[x]=1;
    for(int i=head[x];i;i=edge[i].next)
    {
        if(!dfn[edge[i].v])
        {
            dfs(edge[i].v);
            low[x]=min(low[x],low[edge[i].v]);
        }
        else if(instack[edge[i].v])
        low[x]=min(low[x],dfn[edge[i].v]);
    }
    if(low[x]==dfn[x])
    {
        int sum=0;
        bel++;
        int ne;
        do
        {
            sum++;
            ne=stackk[top--];
            belong[ne]=bel;
            instack[ne]=0;
        }while(x!=ne);
        number[bel]=sum;
    }
}
void tarjan()
{
    memset(dfn,0,sizeof(dfn));
    bel=lu=top=0;
    for(int i=1;i<=n;i++)
    if(!dfn[i])
    dfs(i);
}
void init()
{
    memset(head,0,sizeof(head));
    jiedge=0;
}
vector<int> g[N];
int cy[N];
bool vis[N];
bool dfs1(int u){
    for(int i=0; i<g[u].size(); ++i){
        int v = g[u][i];
        if(vis[v]) continue;
        vis[v] = true;
        if(cy[v]==-1 || dfs1(cy[v])){
            cy[v] = u;
            return true;
        }
    }
    return false;
}
int solve(int n){
    int ret = 0;
    memset(cy, -1, sizeof(cy));
    for(int i=1;i<=n;++i){
        memset(vis, 0, sizeof(vis));
        ret += dfs1(i);
    }
    return n - ret;
}


int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        init();
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
            g[i].clear();
        for(int i=1;i<=m;i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            update(u,v);
        }
        tarjan();
        for(int i=1;i<=jiedge;i++)
        {
            if(belong[edge[i].v]!=belong[edge[i].u])
            {
                g[belong[edge[i].u]].push_back(belong[edge[i].v]);
            }
        }
        int ans=solve(bel);
        printf("%d\n",ans);
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/jhz033/p/7106907.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值