The King’s Problem

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

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
 
题目意思:
现在有n个点,m条边的有向图,要求划分的区域最少
规则如下:
1.可以互相到达的点必须属于一个区域
2.u可以到v或者v可以到v,即一个区域内任意两点u,v,必须存在路径从u->v或者从v->u
3.一个点只能属于一个区域
4.所有点都应该被划分
分析:
可以互相到达的点肯定是属于一个强连通分量的,所以利用tarjan将属于同一个强连通分量的点缩成一个点
得到新图,现在新图是一个DAG图,有向无环图
最小路径的定义:在一个有向图中,找出最少的路径,使得这些路径经过了所有的点
对照一下题目:一个区域其实就是一条路径
最少的区域数目就是最少的路径数目
所以题目转换成最小不相交的路径覆盖,注意:不相交的路径,疑问一个点只能属于一个区域
最小不相交路径覆盖=点数-最大二分匹配
所以对得到的新图,求一遍最大二分匹配就好
最大二分匹配用匈牙利算法写
 
#include<stdio.h>
#include<iostream>
#include<math.h>
#include<string.h>
#include<set>
#include<map>
#include<list>
#include<math.h>
#include<queue>
#include<algorithm>
using namespace std;
typedef long long LL;
#define INF 0x7fffffff
#define mem(a,x) memset(a,x,sizeof(a))
int mon1[]= {,,,,,,,,,,,,};
int mon2[]= {,,,,,,,,,,,,};
int dir[][]= {{,},{,-},{,},{-,}}; int getval()
{
int ret();
char c;
while((c=getchar())==' '||c=='\n'||c=='\r');
ret=c-'';
while((c=getchar())!=' '&&c!='\n'&&c!='\r')
ret=ret*+c-'';
return ret;
} #define max_v 5005
int dfn[max_v];
int low[max_v];
int vis[max_v];
int stk[max_v];
int color[max_v];
vector<int> G[max_v];
vector<int> G2[max_v];
int n,m;
int sig,cnt,sp; int link[max_v];
int match[max_v];
void init()
{
mem(dfn,);
mem(low,);
mem(vis,);
mem(stk,);
mem(color,);
for(int i=;i<=n;i++)
{
G[i].clear();
G2[i].clear();
}
sig=;
cnt=;
sp=-;
} int tarjan(int u)
{
vis[u]=;
low[u]=dfn[u]=cnt++;
stk[++sp]=u;
for(int j=;j<G[u].size();j++)
{
int v=G[u][j];
if(vis[v]==)
tarjan(v);
if(vis[v]==)
low[u]=min(low[u],low[v]);
}
if(low[u]==dfn[u])
{
sig++;
do
{
color[stk[sp]]=sig;
vis[stk[sp]]=-;
}while(stk[sp--]!=u);
}
} int dfs(int u)
{
for(int j=;j<G2[u].size();j++)
{
int v=G2[u][j];
if(vis[v]==)
{
vis[v]=;
if(match[v]==-||dfs(match[v]))
{
match[v]=u;
return ;
}
}
}
return ;
} int max_match()//匈牙利算法
{
mem(match,-);
int ans=;
for(int i=;i<=sig;i++)
{
mem(vis,);
if(dfs(i))
ans++;
}
return ans;
} int main()
{
int t;
cin>>t;
int x,y;
while(t--)
{
scanf("%d %d",&n,&m);
init();
for(int i=;i<=m;i++)
{
scanf("%d %d",&x,&y);
if(count(G[x].begin(),G[x].end(),y)==)//重边
G[x].push_back(y);
}
for(int i=;i<=n;i++)
{
if(vis[i]==)
tarjan(i);
}
for(int i=;i<=n;i++)
{
for(int j=;j<G[i].size();j++)
{
if(color[i]!=color[G[i][j]])
{
if(count(G2[color[i]].begin(),G2[color[i]].end(),color[G[i][j]])==)//重边
G2[color[i]].push_back(color[G[i][j]]);
}
}
}
printf("%d\n",sig-max_match());//最小不相交路径覆盖=新图点数-最大二分匹配数
}
return ;
}
/*
题目意思:
现在有n个点,m条边的有向图,要求划分的区域最少
规则如下:
1.可以互相到达的点必须属于一个区域
2.u可以到v或者v可以到v,即一个区域内任意两点u,v,必须存在路径从u->v或者从v->u
3.一个点只能属于一个区域
4.所有点都应该被划分 分析:
可以互相到达的点肯定是属于一个强连通分量的,所以利用tarjan将属于同一个强连通分量的点缩成一个点
得到新图,现在新图是一个DAG图,有向无环图 最小路径的定义:在一个有向图中,找出最少的路径,使得这些路径经过了所有的点 对照一下题目:一个区域其实就是一条路径
最少的区域数目就是最少的路径数目
所以题目转换成最小不相交的路径覆盖,注意:不相交的路径,疑问一个点只能属于一个区域 最小不相交路径覆盖=点数-最大二分匹配
所以对得到的新图,求一遍最大二分匹配就好
最大二分匹配用匈牙利算法写 gameover! */
 

HDU 3861 The King’s Problem(tarjan缩点+最小路径覆盖:sig-最大二分匹配数,经典题)的更多相关文章

  1. HDU 3861.The King’s Problem 强联通分量+最小路径覆盖

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

  2. HDU 3861 The King’s Problem(强连通+二分图最小路径覆盖)

    HDU 3861 The King's Problem 题目链接 题意:给定一个有向图,求最少划分成几个部分满足以下条件 互相可达的点必须分到一个集合 一个对点(u, v)必须至少有u可达v或者v可达 ...

  3. HDU 3861 The King’s Problem(强连通分量+最小路径覆盖)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3861 题目大意: 在csdn王国里面, 国王有一个新的问题. 这里有N个城市M条单行路,为了让他的王国 ...

  4. hdu 3861 The King’s Problem trajan缩点+二分图匹配

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

  5. HDU 3861 The King’s Problem (强连通缩点+DAG最小路径覆盖)

    <题目链接> 题目大意: 一个有向图,让你按规则划分区域,要求划分的区域数最少. 规则如下:1.所有点只能属于一块区域:2,如果两点相互可达,则这两点必然要属于同一区域:3,区域内任意两点 ...

  6. 【HDOJ3861】【Tarjan缩点+最小路径覆盖】

    http://acm.hdu.edu.cn/showproblem.php?pid=3861 The King’s Problem Time Limit: 2000/1000 MS (Java/Oth ...

  7. hdu——3861 The King’s Problem

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

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

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

  9. HDU 3861 The King's Problem(强连通分量缩点+最小路径覆盖)

    http://acm.hdu.edu.cn/showproblem.php?pid=3861 题意: 国王要对n个城市进行规划,将这些城市分成若干个城市,强连通的城市必须处于一个州,另外一个州内的任意 ...

随机推荐

  1. Javascript 回调函数理解---二娃子买肾机6

    在Javascript中什么是回调函数,我认为简单来说就是把一个函数B作为参数传递给另一个函数A,在A函数中的一定时机调用函数B. 这里可以看出回调函数形成了一个闭包,它可以访问函数A中的活动对象. ...

  2. Ajax常见面试题 -- 前端面试题(二)

    1:什么是ajax?ajax作用是什么? 异步的javascript和xml AJAX 是一种用于创建快速动态网页的技术. ajax用来与后台交互   2:原生js ajax请求有几个步骤?分别是什么 ...

  3. python学习笔记之——操作mysql数据库

    Python 标准数据库接口为 Python DB-API,Python DB-API为开发人员提供了数据库应用编程接口. Python 数据库接口支持非常多的数据库,你可以选择适合你项目的数据库: ...

  4. Linux 修改linux的SSH的默认端口

    修改linux的SSH的默认端口 by:授客 QQ:1033553122 安装完linux后,默认的情况下ssh是开放的,容易造到黑客攻击,简单有效的操作之一就是修改默认端口号   步骤一:修改/et ...

  5. JSP基本语法总结【1】(jsp工作原理,脚本元素,指令元素,动作元素)

    时隔半年,回头对jsp复习整理一下,温故而知新. jsp工作原理: jsp服务器管理jsp页面分两个阶段:转换阶段(translation phase)和执行阶段(execution phase). ...

  6. MAYA逼真手枪制作视频教程 中文字幕

    下载地址 更多中文字幕教程请关注微镜映画网,有各类CG教程提供

  7. 关于innodb mtr模块

    mtr (mini-transaction)微事务 mtr作用 mtr模块主要保证物理操作的一致性和原子性 1 一致性:通过读写锁来保证 2 原子性:涉及到的物理更新,都记入redo日志 mtr何时使 ...

  8. c#经典算法之冒泡排序(Bubble Sort)

    转载于:https://www.cnblogs.com/shen-hua/p/5422676.html 原理:比较两个相邻的元素,将值大的元素交换至右端. 思路:依次比较相邻的两个数,将小数放在前面, ...

  9. 如何加密 Windows VM 上的虚拟磁盘

    为了增强虚拟机 (VM) 的安全性以及符合性,可以加密 Azure 中的虚拟磁盘. 磁盘是使用 Azure 密钥保管库中受保护的加密密钥加密的. 可以控制这些加密密钥,以及审核对它们的使用. 本文详细 ...

  10. [SQLSERVER] [GPO] Add the Log on as a service Right to an Account

    Add the Log on as a service Right to an Account Updated: August 8, 2008 Applies To: Windows Server 2 ...