hdu——3861 The King’s Problem

          The King’s Problem

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


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
 

问题描述
在寂静的Kingdom,国王遇到了一个新问题。王国里有N个城市,城市之间有许多方向性的道路。这意味着,如果有一条从U到V的道路,你只能从城市到第五城,但不能从第五城到美国城市,为了更有效地统治他的王国,国王想要把他的王国分割成几个州,每个城市必须属于一个州。更重要的是,对于每一对城市(U,V),如果有一种从u到V,从V到u,(u,v)必须属于同一状态的方法。国王必须保证,在每一个州,我们都可以从U到v,或者在每一对城市(u,V)之间从v到u,而不经过任何属于另一个州的城市。
现在国王请求你的帮助,他想知道他必须把王国分成多少个州。
输入
第一行包含一个整数T,测试用例的个数。然后随访t病例。
每个案例的第一行包含两个整数n,m(0<n<= 5000,0 < = M = 100000),在英国的城市和道路的数量。下一行m每一行包含两个整数u和v(1 < u = v,v=n),表示有一条从城市u到城市v的道路。
输出
输出应该包含T线。对于每个测试用例,您只需输出一个整数,这是国王必须划分的最小数目。

 
思路:
tarjan缩点+最小路径覆盖
邻接矩阵MLE ,只好改用邻接表

题意是说一个有向图将n个点划分成最少的集合个数,要求任意两个相互到达的点在同一个几何中,且属于同一个集合的任意俩个点对(u,v),至少存在一条路径,使得v对于u 可达 或者 u 对于v 可达。首先要缩点求集合的个数,然后重新构图,任意俩点,只要在同一条有向路径上,则可以属于一个集合,所以要在缩点后的图上用二分匹配找最小路径覆盖即可。

最小路径覆盖=点数-最大匹配。这里点数是缩点之后的点数。

代码:
 
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 5100
using namespace std;
bool vis[N];
int n,m,t,x,y,tot,tim,ans,sum,top;
int dfn[N],low[N],head[100001],head1[100001],girl[N],stack[N],belong[N];
struct Edge
{
    int to,from,next;
}edge[100001];
int add(int x,int y)
{
    tot++;
    edge[tot].to=y;
    edge[tot].next=head[x];
    head[x]=tot;
}
struct Edde
{
    int to,from,next;
}edde[100001];
int add1(int x,int y)
{
    tot++;
    edde[tot].to=y;
    edde[tot].next=head1[x];
    head1[x]=tot;
}
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 x*f;
}
int tarjan(int now)
{
    dfn[now]=low[now]=++tim;
    stack[++top]=now,vis[now]=true;
    for(int i=head[now];i;i=edge[i].next)
    {
        int t=edge[i].to;
        if(vis[t]) low[now]=min(low[now],dfn[t]);
        else if(!dfn[t]) tarjan(t),low[now]=min(low[now],low[t]);
    }
    if(low[now]==dfn[now])
    {
        sum++,belong[now]=sum;
        for(;stack[top]!=now;top--)
         belong[stack[top]]=sum,vis[stack[top]]=false;
        vis[now]=false,top--;
    }
}
int shink_point()
{
    tot=0;
    for(int i=1;i<=n;i++)
     for(int j=head[i];j;j=edge[j].next)
      if(belong[i]!=belong[edge[j].to]) 
       add1(belong[i],belong[edge[j].to]);
}
int find(int x)
{
    for(int i=head1[x];i;i=edde[i].next)
      if(!vis[edde[i].to])
      {
          int t=edde[i].to;
          vis[t]=true;
        if(girl[t]==-1||find(girl[t])) {girl[t]=x; return 1;}
      }
    return 0;
}
int begin()
{
    top=0,tim=0,tot=0,ans=0,sum=0;
    memset(low,0,sizeof(low));
    memset(dfn,0,sizeof(dfn));
    memset(vis,0,sizeof(vis));
    memset(head,0,sizeof(head));
    memset(girl,-1,sizeof(girl));
    memset(head1,0,sizeof(head1));
    memset(stack,0,sizeof(stack));
    memset(belong,0,sizeof(belong));
}
int main()
{
    t=read();
    while(t--)
    {
        n=read(),m=read();
        begin();
        for(int i=1;i<=m;i++)
         x=read(),y=read(),add(x,y);
        for(int i=1;i<=n;i++)
         if(!dfn[i]) tarjan(i);
        shink_point();
        for(int i=1;i<=sum;i++) 
        {
            memset(vis,0,sizeof(vis));
            ans+=find(i);
        }
        printf("%d\n",sum-ans); 
    }
}

 

转载于:https://www.cnblogs.com/z360/p/7469659.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值