hdu 3816 The King’s Problem (tarjan缩点+最小路径覆盖)

The King’s Problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3791 Accepted Submission(s): 1338

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

题意

一个有向图,要把他分成尽量少的块。
分块规则:1、如果u到v,且v到u,则u和v必须在一个块里 。2、每个块里的两个点,至少要有一个能够到达另一个点。3、每个点只能在一个块里。
逐个条件分析。
1、说明每个强连通分量必须当做一个整体来看,于是先用tarjan缩点。
2、说明每个块都是一条路径。
3、每个点只能在一个路径里,于是问题转化成了缩点之后的树求最小不相交路径覆盖。
最小路径覆盖的讲解可以看这位大佬的博客https://www.cnblogs.com/justPassBy/p/5369930.html

思路

tarjan缩点之后求最大匹配数,最后的答案即为缩点之后的节点数-最大匹配数。

代码

#define push_back pb
#define make_pair mk
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<cmath>
#include<map>
#include<algorithm>
#include<string>
#include<string.h>
#include<set>
#include<queue>
#include<bitset>
#include<stack>
#include<functional>
using std::pair;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<double,int>PDI;
const double PI=acos(-1);
const int maxn=5e3+10;
const int maxm=1e5+10;
const int INF = 0x3f3f3f3f;
using namespace std;
struct edge{int to,next;}e[maxm];
int index,dfn[maxn],low[maxn],scccnt,S[maxn],top,instack[maxn];
int head[maxn],cnt,id[maxn];
int n,m;
bool f[maxn][maxn];
bool  vis[maxn*2];
int flag[maxn*2];
void tarjan(int u)
{
    instack[u]=1;
    dfn[u]=low[u]=++index;
    S[++top]=u;
    for(int i=head[u];~i;i=e[i].next)
    {
        int v=e[i].to;
        if(!dfn[v])
        {
            tarjan(v);
            low[u]=min(low[u],low[v]);
        }
        else if(instack[v]) low[u]=min(low[u],dfn[v]);
    }
    if(dfn[u]==low[u])
    {
        scccnt++;
        while(1)
        {
            int x=S[top--];
            instack[x]=0;
            id[x]=scccnt;
            if(x==u) break;
        }
    }
}
void find_scc()
{
    memset(dfn,0,sizeof(dfn));
    top=-1;
    scccnt=index=0;
    for(int i=1;i<=n;i++) if(!dfn[i]) tarjan(i);
}

void add(int u,int v)
{
    e[cnt].to=v;
    e[cnt].next=head[u];
    head[u]=cnt++;
}
bool dfs(int a)
{
    for(int i=1;i<=n;i++)
    {
        if(f[a][i]==1&&!vis[i])
        {
            vis[i]=1;
            if(flag[i]==0||dfs(flag[i]))
            {
                flag[i]=a;
                return true;
            }
        }
    }
    return false;
}
int match()
{
    int ans=0;
    memset(flag,0,sizeof(flag));
    for(int i=1;i<=n;i++)
    {
        memset(vis,0,sizeof(vis));
        if(dfs(i)) ans++;
    }
    return ans;
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        scanf("%d%d",&n,&m);
        int u,v;
        cnt=0;
        memset(head,-1,sizeof(head));
        for(int i=0;i<m;i++)
        {
            scanf("%d%d",&u,&v);
            add(u,v);
        }
        find_scc();
        memset(f,0,sizeof(f));
        for(int i=1;i<=n;i++)
        {
            for(int j=head[i];~j;j=e[j].next)
            {
                int v=e[j].to;
                if(id[i]!=id[v])
                {
                    f[id[i]][id[v]]=1;
                }
            }
        }
        int res=match();
        //cout<<res<<endl;
        printf("%d\n",scccnt-res);
    }
}

猜你喜欢

转载自blog.csdn.net/a670531899/article/details/81515055