The King’s Problem 【scc+缩点+最小路径覆盖】

本文介绍了一种解决特定图论问题的方法:如何将王国划分为最少数量的州,使得每座城市属于唯一的一个州,并且对于每一对城市(u,v),如果存在从u到v或从v到u的道路,则这两座城市必须属于同一个州。文章详细阐述了使用深度优先搜索(DFS)和二分匹配来求解DAG图的最小顶点覆盖。

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

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

乍一看很像 求scc的个数,但是连样例都过不去,仔细看题,题目中有个关键性的条件,就是 国王要求 在有路的两个点之间,要么有从(u–>v)或者 有(v–>u) ,并且这些点在同一个地区内。
所以就不是简单的scc问题了,其实是求DAG图的最小顶点覆盖。

代码

 #include<bits/stdc++.h>
#define LL long long 
using namespace std;

const int MAXN = 5000+100;
const int MAXM = 1e6;
inline int read(){
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9') {if(ch=='-') f=-1; ch=getchar();} 
    while(ch>='0'&&ch<='9') { x=x*10+ch-'0'; ch=getchar();}
    return f*x;
}
/*----------------------------*/
struct Edge{
    int from,to,nexts;
}edge[MAXM];
int head[MAXN],top;
int n,m;
void init(){
    memset(head,-1,sizeof(head)) ;
    top=0;
}
void addedge(int a,int b){
    Edge e={a,b,head[a]} ;
    edge[top]=e;head[a]=top++;
}
void getmap(){
    int a,b;
    while(m--){
        a=read();b=read();
        addedge(a,b);
    }
}
int dfn[MAXN],low[MAXN];
int sccno[MAXN],scc_cnt;
stack<int>S;bool Instack[MAXN];
vector<int>scc[MAXN];
bool mp[MAXN][MAXN];
int dfs_clock;
void tarjan(int now){
    low[now]=dfn[now]=++dfs_clock;
    S.push(now);Instack[now]=1;
    for(int i=head[now];i!=-1;i=edge[i].nexts){
        Edge e=edge[i];
        if(!dfn[e.to]){
            tarjan(e.to);
            low[now]=min(low[now],low[e.to]);
        }else if(Instack[e.to])
            low[now]=min(low[now],dfn[e.to]);
    }
    if(dfn[now]==low[now]){
        scc_cnt++;scc[scc_cnt].clear();
        for(;;){
            int nexts=S.top();S.pop();Instack[nexts]=0;
            sccno[nexts]=scc_cnt;
            scc[scc_cnt].push_back(nexts);
            if(nexts==now) break;
        }
    }
}
void find_cut(int le,int ri){

    memset(low,0,sizeof(low));
    memset(dfn,0,sizeof(dfn));
    memset(Instack,0,sizeof(Instack));
    memset(sccno,0,sizeof(sccno));
    dfs_clock=scc_cnt=0;
    for(int i=le;i<=ri;i++)
        if(!dfn[i]) tarjan(i);
}
void suodian(){  // 建立DAG图
    for(int i=1;i<=scc_cnt;i++) 
    memset(mp[i],0,sizeof(mp[i]));//都要赋值为0才行。

    for(int i=0;i<top;i++){
        Edge e=edge[i];
        int now=sccno[e.from];
        int nexts=sccno[e.to];
        if(now!=nexts){
            mp[now][nexts]=1;
        }
    }
}
int pipei[MAXN];
bool vis[MAXN];
bool Find(int x){//二分匹配求最小路径覆盖
    for(int i=1;i<=scc_cnt;i++){
        if(!vis[i]&&mp[x][i]){
            vis[i]=1;
            if(!pipei[i]||Find(pipei[i])){
                pipei[i]=x;
                return 1;
            }
        }
    }
    return 0;
}
void solve(){

    memset(pipei,0,sizeof(pipei));
    int cnt=0;
    for(int i=1;i<=scc_cnt;i++){ 
        memset(vis,0,sizeof(vis));
        if(Find(i))  cnt++;
    }

    printf("%d\n",scc_cnt-cnt);
}
int main(){
    int t;cin>>t;while(t--){
        scanf("%d%d",&n,&m);
        init();
        getmap();
        find_cut(1,n);
        suodian();
        solve();
    }
    return 0.;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值