hdu 3861

The King’s Problem

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


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
 

强连通缩点+二分匹配

求最小路径覆盖=(顶点数 - 最大匹配数);

题目上说的很清楚,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

,所以显然要先缩点,之后用二分匹配求最小路径覆盖,思路很清晰!

 

#include<iostream>
#include<string.h>
#include<stdio.h>
#include<vector>
using namespace std;
#define max_n 5005
#define max_e 250002
#define inf 99999999

int stack[max_n],top;//栈
int isInStack[max_n];//是否在栈内
int low[max_n],dfn[max_n],tim;//点的low,dfn值;time从1开始
int node_id;//强连通分量的个数
int head[max_n],s_edge;//邻接表头  s_edge从1开始
int gro_id[max_n];//记录某个点属于哪个强连通分量
int n,m;

bool vis[max_n];
int match[max_n];
vector<int> vec[max_n];//边的后节点存储

struct Node
{
    int to;
    int next;
} edge[max_e];
int head1[max_n];
int NE;
struct Node1
{
    int from;
    int to;
    int next;
} edge1[max_e];
void init()//初始化
{
    s_edge=0;//存储
    memset(head,0,sizeof(head));
    memset(edge,0,sizeof(edge));

    top=0;//tarjian初始化
    tim=0;
    node_id=0;
    memset(isInStack,0,sizeof(isInStack));
    memset(low,0,sizeof(low));
    memset(dfn,0,sizeof(dfn));

    NE=0;
    memset(head1,-1,sizeof(head1));
    memset(match,-1,sizeof(match));
}
void addedge(int u,int v)
{
    s_edge++;
    edge[s_edge].to=v;
    edge[s_edge].next=head[u];
    head[u]=s_edge;
}
void add(int u,int v)
{
    edge1[NE].from=u;
    edge1[NE].to=v;
    edge1[NE].next=head1[u];
    head1[u]=NE++;
}
int min(int a,int b)
{
    if(a<b)return a;
    else return b;
}
void tarjan(int u)
{
    //low值为u或u的子树能够追溯到得最早的栈中节点的次序号
    stack[top++]=u;
    isInStack[u]=1;
    dfn[u]=++tim; //记录点u出现的记录,并放在栈中
    low[u]=tim;

    int e,v;
    for(e=head[u]; e; e=edge[e].next) //如果是叶子节点,head[u]=0,edge[e].next=0;
    {
        v=edge[e].to;
        if(!dfn[v])
        {
            tarjan(v);
            low[u]=min(low[u],low[v]);
        }
        else if(isInStack[v])
            low[u]=min(low[u],dfn[v]);
    }
    int j;
    if(dfn[u]==low[u])//找到一个强连通,元素出栈
    {
        node_id++;
        while(j=stack[--top])
        {
            isInStack[j]=0;
            gro_id[j]=node_id;
            if(j==u)break;
        }
    }
}

void find()//tarjian寻找
{
    for(int i = 1 ; i <=n ; ++i)
    {
        if(!dfn[i])
        {
            tarjan(i);
        }
    }
}

int dfs(int t)
{
    for(int i=head1[t]; i!=-1; i=edge1[i].next)
    {
        int idx=edge1[i].to;
        if(!vis[idx])
        {
            vis[idx]=1;
            if(match[idx]==-1||dfs(match[idx]))
            {
                match[idx]=t;
                return 1;
            }
        }
    }
    return 0;
}

int main()
{
    int t;
    int a,b;
    cin>>t;
    while(t--)
    {
        scanf("%d %d",&n,&m);
        init();
        for(int i=1; i<=n; i++)
            vec[i].clear();
        for(int i = 0 ; i <m ; ++i)
        {
            scanf("%d%d",&a,&b);
            vec[a].push_back(b);
            addedge(a,b);
        }
        find();
        for(int i=1; i<=n; i++)
        {
            for(int j=0; j<vec[i].size(); j++)//求强连通分量的出、入度
            {
                if(gro_id[i]!=gro_id[vec[i][j]])
                {
                   add(gro_id[i],gro_id[vec[i][j]]);
                }
            }
        }
        int ans=0;
        for(int i=1; i<=node_id; i++)
        {
            memset(vis,0,sizeof(vis));
            if(dfs(i))
                ans++;

        }
        cout<<node_id-ans<<endl;
    }
    return 0;
}


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值