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): 3432    Accepted Submission(s): 1215


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
题意:有n个城市和m条有向通道。如果从某个城市一条道路一直走到没有路可走,那么在这条首路上的所有城市划分成一个国家,每个城市只能属于一个国家,一个环形上的城市当然也是属于同一国家。问这些城市最少分为多少个国家。
思路:最小路径覆盖,但是有向图中有环的存在,二分图只有在无环才正确,先进行tarjan缩点,然后建图,再二分匹配。
代码:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <stack>
#include <cmath>
#include <vector>
using namespace std;
const int N=5555;
int dfn[N],low[N],belong[N];
bool instack[N];
int flag[N];
int used[N];
vector<int>g[N];
vector<int>gg[N];
stack<int>s;
int n,m;
int cnt,index;
void init()
{
    memset(dfn,0,sizeof(dfn));
    memset(belong,0,sizeof(belong));
    memset(instack,false,sizeof(instack));
    memset(low,0,sizeof(low));
    while(!s.empty())
    {
        s.pop();
    }
    for(int i=0;i<=n;i++)
    {
        g[i].clear();
    }
    cnt=index=0;
}
void tarjan(int u)
{
    dfn[u]=low[u]=++index;
    s.push(u);
    instack[u]=true;
    int v;
    for(int i=0;i<g[u].size();i++)
    {
        v=g[u][i];
        if(!dfn[v])
        {
            tarjan(v);
            low[u]=min(low[v],low[u]);
        }
        else if(instack[v])
        {
            low[u]=min(dfn[v],low[u]);
        }
    }
    if(low[u]==dfn[u])
    {
        cnt++;
        do{
            v=s.top();
            s.pop();
            instack[v]=false;
            belong[v]=cnt;
        }while(u!=v);
    }
}
bool dfs(int u)
{
    for(int i=0;i<gg[u].size();i++)
    {
        int v=gg[u][i];
        if(used[v]==0)
        {
            used[v]=1;
            if(flag[v]==-1||dfs(flag[v]))
            {
                flag[v]=u;
                return true;
            }
        }
    }
    return false;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        init();
        int i,j;
        int a,b;
        for(i=0;i<m;i++)
        {
            scanf("%d%d",&a,&b);
            g[a].push_back(b);
        }
        for(i=1;i<=n;i++)
        {
            if(!dfn[i])
                tarjan(i);
        }
        for(i=0;i<=n;i++)
        {
            gg[i].clear();
        }
        for(i=1;i<=n;i++)
        {
            for(j=0;j<g[i].size();j++)
            {
                int v=g[i][j];
                if(belong[i]!=belong[v])
                {
                    gg[belong[i]].push_back(belong[v]);
                }
            }
        }
        int ans=0;
        memset(flag,-1,sizeof(flag));
        for(i=1;i<=cnt;i++)
        {
            memset(used,0,sizeof(used));
            if(dfs(i))
                ans++;
        }
        printf("%d\n",cnt-ans);
    }
    return 0;
}

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值