首页 > 代码库 > hdu3861The King’s Problem (强连通 缩点+最小路径覆盖)

hdu3861The King’s Problem (强连通 缩点+最小路径覆盖)

The King’s Problem

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


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
2011 Multi-University Training Contest 3 - Host by BIT 
题意:有n个城市和m条有向通道。如果从某个城市一条道路一直走到没有路可走,那么在这条首路上的所有城市划分成一个国家,每个城市只能属于一个国家,一个环形上的城市当然也是属于同一国家。问这些城市最少分为多少个国家。
解题:很明显是最小路径覆盖,但在这有向图中存在环,而二分图是在有向无环图时才正确,所以先用强连通缩点重新构成有向无环图。再二分匹配。
#include<stdio.h>
#include<iostream>
#include<vector>
using namespace std;
int vist[5005],match[5005];
vector<int>map[5005];
int find(int i)
{
    for(int j=0;j<map[i].size();j++)
    if(vist[map[i][j]]==0)
    {
        vist[map[i][j]]=1;
        if(match[map[i][j]]==0||find(match[map[i][j]]))
        {
            match[map[i][j]]=i; return 1;
        }
    }
    return 0;
}
//-----下方是用强连通把环缩成一个点,使整个图变成有向无环图------
int stack[5005],sn,low[5005],dfn[5005],deep,node[5005],k;
int Min(int a,int b)
{
    return a>b?b:a;
}
void dfs(int i)
{
    stack[++sn]=i; vist[i]=1;
    deep++; low[i]=dfn[i]=deep;
    for(int j=0;j<map[i].size();j++)
    if(vist[map[i][j]]==0)
    {
        dfs(map[i][j]);
        low[i]=Min(low[i],low[map[i][j]]);
    }
    else if(vist[map[i][j]]==1)//这个点在栈中
    low[i]=Min(low[i],dfn[map[i][j]]);
    
    if(low[i]==dfn[i])//在栈中点i及到栈顶的点是一个强连通分量
    {
        k++;
        while(stack[sn]!=i)
        {
            node[stack[sn]]=k; //表示原先的点stack[sn]在缩点后的图中是第k类点。
            vist[stack[sn]]=2; //走过了,但己经出栈
            sn--;
        }
        node[stack[sn]]=k; vist[stack[sn]]=2; sn--;
    }
}
int reSetMap(int n)
{
    vector<int>mp[5005];
    sn=0; deep=0; k=0;
    for(int j=1;j<=n;j++)
    vist[j]=0;

    for(int i=1;i<=n;i++)
    if(vist[i]==0)
    dfs(i);

    for(int i=1;i<=n;i++)//缩点构成新图
    for(int j=0;j<map[i].size();j++)
    if(node[i]!=node[map[i][j]])//在新图中不是同一个点时
    mp[node[i]].push_back(node[map[i][j]]);

    for(int i=1;i<=k;i++)
    map[i]=mp[i];
    return k;//反回新图的点的个数
}
int main()
{
    int t,n,m,a,b,ans;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
        map[i].clear(),match[i]=0;
        while(m--)
        {
            scanf("%d%d",&a,&b); map[a].push_back(b);
        }
        n=reSetMap(n);//缩点构成新的有向无环图
        ans=0;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)vist[j]=0;
            ans+=find(i);
        }
        printf("%d\n",n-ans);
    }
}