The King’s Problem

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

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
题意转载自http://www.cnblogs.com/kane0526/archive/2013/07/21/3203992.html

题意:一个有向图,让你按规则划分区域,要求划分的区域数最少。

规则如下:1、有边u到v以及有边v到u,则u,v必须划分到同一个区域内。2、一个区域内的两点至少要有一方能到达另一方。3、一个点只能划分到一个区域内。

解题思路:根据规则1可知必然要对强连通分量进行缩点,缩点后变成了一个弱连通图。根据规则2、3可知即是要求图的最小路径覆盖。

定义:

最小路径覆盖:在图中找一些路径(路径数最少),使之覆盖了图中所有的顶点,且每个顶点有且仅和一条路径有关联。

最小顶点覆盖:在图中找一些点(顶点数最少),使之覆盖了图中所有的边,每条边至少和一个顶点有关联。

二分图:最小顶点覆盖=最大匹配数。

最小路径覆盖=顶点数-最大匹配数。

二分图最最小路径覆盖:https://www.cnblogs.com/justPassBy/p/5369930.html

匈牙利算法:https://blog.csdn.net/dark_scope/article/details/8880547

代码:

#include<stdio.h>
#include<vector>
#include<stack>
#include<string.h>
using namespace std;
vector<int> s[5050];//
stack<int> st;
int vt[5050];
int cnt,ct;
int low[5050],dfn[5050];
int bl[5050],nd[5050];//例:如果是a-->b,则bl[b]=a;如果a点再经过tarjan算法后属于第i个集合,nd[a]=i;
struct
{
  int x,y;
}mp[100050];
int min(int a,int b)
{
  if(a<=b)
  return a;
  return b;
}
int tarjan(int a)//tarjan算法
{
  int i,j;
  low[a]=dfn[a]=cnt++;
  vt[a]=1;
  st.push(a);
  for(i=0;i<s[a].size();i++)
  {
    int u=s[a][i];
    if(!dfn[u])
    {
      tarjan(u);
      low[a]=min(low[a],low[u]);
    }
    else if(vt[u])
    low[a]=min(low[a],dfn[u]);
  }
  if(low[a]==dfn[a])
  {
    int x;
    ct++;
    do//为缩点作准备
    {
      x=st.top();
      vt[x]=0;
      nd[x]=ct;
      st.pop();
    }while(x!=a);
  }
  return 0;
}
int find(int a)//匈牙利算法
{
  int i,j;
  for(i=0;i<s[a].size();i++)
  {
    int u=s[a][i];
    if(!vt[u])
    {
      vt[u]=1;
      if(bl[u]==0||find(bl[u]))
      {
        bl[u]=a;
        //printf("www%d %d\n",bl[u],u);
        return 1;
      }
    }
  }
  return 0;
}
int main()
{
  int n,m,t;
  int i,j;
  int a,b,sum;
  scanf("%d",&t);
while(t--)
{
  memset(dfn,0,sizeof(dfn));
  memset(vt,0,sizeof(vt));
  memset(bl,0,sizeof(bl));
  ct=0;
  cnt=1;
  scanf("%d%d",&n,&m);
  for(i=1;i<=n;i++)
  s[i].clear();
  for(i=1;i<=m;i++)
  {
    scanf("%d%d",&mp[i].x,&mp[i].y);
    s[mp[i].x].push_back(mp[i].y);
  }
  for(i=1;i<=n;i++)
  if(!dfn[i])tarjan(i);
  sum=0;
  for(i=1;i<=n;i++)
  s[i].clear();
  for(i=1;i<=m;i++)//缩点并重新制图
  {
    int u,v;
    u=nd[mp[i].x];
    v=nd[mp[i].y];
    if(u!=v)
    s[u].push_back(v);
  }
  for(i=1;i<=ct;i++)
  {
    memset(vt,0,sizeof(vt));
    if(find(i))
    sum++;
  }
  printf("%d\n",ct-sum);
  }
  return 0;
}

例:

6 6

1 2

2 3

3 1

4 1

5 2

6 3

3

10 11

1 2

2 3

3 1

3 4

4 5

5 6

6 7

7 5

10 9

9 8

8 4

2

hdu3861 强连通分量缩点+二分图最最小路径覆盖的更多相关文章

  1. 【HDU3861 强连通分量缩点+二分图最小路径覆盖】

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3861 题目大意:一个有向图,让你按规则划分区域,要求划分的区域数最少. 规则如下:1.有边u到v以及有 ...

  2. POJ 1422 Air Raid(二分图匹配最小路径覆盖)

    POJ 1422 Air Raid 题目链接 题意:给定一个有向图,在这个图上的某些点上放伞兵,能够使伞兵能够走到图上全部的点.且每一个点仅仅被一个伞兵走一次.问至少放多少伞兵 思路:二分图的最小路径 ...

  3. POJ:3020-Antenna Placement(二分图的最小路径覆盖)

    原题传送:http://poj.org/problem?id=3020 Antenna Placement Time Limit: 1000MS Memory Limit: 65536K Descri ...

  4. POJ 3020:Antenna Placement(无向二分图的最小路径覆盖)

    Antenna Placement Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6334   Accepted: 3125 ...

  5. HDU 3861 The King’s Problem 最小路径覆盖(强连通分量缩点+二分图最大匹配)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3861 最小路径覆盖的一篇博客:https://blog.csdn.net/qq_39627843/ar ...

  6. hdoj 3861 The King’s Problem【强连通缩点建图&&最小路径覆盖】

    The King’s Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  7. POJ 3020 Antenna Placement(无向二分图的最小路径覆盖)

    ( ̄▽ ̄)" //无向二分图的最小路径覆盖数=顶点总数-最大匹配数/2(最大匹配数=最小点覆盖数) //这里最大匹配数需要除以2,因为每两个相邻的*连一条边,即<u,v>和< ...

  8. UVA 1201 - Taxi Cab Scheme(二分图匹配+最小路径覆盖)

    UVA 1201 - Taxi Cab Scheme 题目链接 题意:给定一些乘客.每一个乘客须要一个出租车,有一个起始时刻,起点,终点,行走路程为曼哈顿距离,每辆出租车必须在乘客一分钟之前到达.问最 ...

  9. POJ 1422 二分图(最小路径覆盖)

    Air Raid Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7278   Accepted: 4318 Descript ...

随机推荐

  1. hammer.js方法总结(只做了一个简单的demo)

    html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <titl ...

  2. 微信小程序 swiper 显示图片计数 当前/总数

    <view class="swiperContainer"> <swiper bindchange="swiperChange" autopl ...

  3. SQL SERVER 一组数据按规律横着放置,少则补空,如人员按一进一出的规律,进出为一组,缺少的补null

    假设一组数据:人员进出刷卡数据表[SwingCard] ID MenID Door 1 1 In 2 1 In 3 1 Out 4 1 In 5 1 Out 6 1 Out 想要变成如下:一进一出为一 ...

  4. hashCode会出现负数吗,答案是肯定的

    先来普及一下基本数据类型的长度: unsigned   int   0-4294967295   int   -2147483648-2147483647 unsigned long 0-429496 ...

  5. Centos7 JDK安装过程中 解决java -version 报错: bash: /home/jdk1.8.0_161/bin/java: Permission denied

    1.执行Linux命令 -----vim /etc/profile  编辑profile  文件,在里面添加: #set java enviroment JAVA_HOME=/opt/JavaHome ...

  6. ssh -vT git@github.com get “ No such file or directory” 错误

    在"Are you sure you want to continue connnecting"的时候回复y,不要打空格跳过 参考:http://stackoverflow.com ...

  7. 根据科目计算父科目ID,并递归累计求父科目的金额

    通常情况下,我们会从外部系统或者其他数据源得到以下树形结构的数据,并需要对其进行处理 其中,需要做的处理包括 1.计算每个科目的父科目ID,即PARENT_ID; 2.计算每个科目的ITEM_LEVE ...

  8. Jmeter4.0----录制脚本

    1.前言 Jmeter录制脚本有两种方式.1.通过第三方工具录制比如:Badboy,然后转化为jmeter可用的脚本:2.使用jmeter本身自带的录制脚本功能. 对于测试小白来说可用先使用jmete ...

  9. maven-assembly-plugin

    <build> <finalName>detail</finalName> <plugins> <plugin> <artifactI ...

  10. 牛客网 PAT 算法历年真题 1001 : A+B和C (15)

    1001 : A+B和C (15) 时间限制 1000 ms 内存限制 32768 KB 代码长度限制 100 KB 判断程序 Standard 题目描述 给定区间[-2的31次方, 2的31次方]内 ...