【HDOJ3861】【Tarjan缩点+最小路径覆盖】
http://acm.hdu.edu.cn/showproblem.php?pid=3861
The King’s Problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3544 Accepted Submission(s): 1256
Now the king asks for your help, he wants to know the least number of states he have to divide the kingdom into.
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.
3 2
1 2
1 3
定义:
最小路径覆盖:在图中找一些路径(路径数最少),使之覆盖了图中所有的顶点,且每个顶点有且仅和一条路径有关联。
最小顶点覆盖:在图中找一些点(顶点数最少),使之覆盖了图中所有的边,每条边至少和一个顶点有关联。
二分图:最小顶点覆盖=最大匹配数。
最小路径覆盖=顶点数-最大匹配数。
//Wannafly挑战赛14 C https://www.nowcoder.com/acm/contest/81/C
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<vector>
using namespace std;
const int maxn=;
struct edge{
int from;
int to;
int next;
}EDGE[maxn];
vector<int>vc[maxn];
int head[maxn],dfn[maxn],vis[maxn],vvis[maxn],used[maxn],low[maxn],col[maxn],in[maxn],out[maxn],en[maxn],stk[maxn];//各个变量的意义可参照上篇博客
int edge_cnt=,tot1=,tot2=,scc_cnt=,tot0=;
void add(int x,int y)
{
EDGE[edge_cnt].from=x;
EDGE[edge_cnt].to=y;
EDGE[edge_cnt].next=head[x];
head[x]=edge_cnt++;
}
bool find(int x)
{
for(int i = head[x] ; i != - ; i=EDGE[i].next)
{
if(!used[EDGE[i].to])
{
used[EDGE[i].to]=;
if(!vvis[EDGE[i].to]||find(vvis[EDGE[i].to]))
{
vvis[EDGE[i].to]=x;
return ;
}
}
}
return ;
}
void Tarjan(int u)
{
low[u]=dfn[u]=++tot1;//注意tot1的初值必须是1【因为dfn必须为正数】,所以这里使用++tot1而不用tot1++;
vis[u]=;
stk[++tot2]=u;
for(int i = head[u]; i != - ; i = EDGE[i].next)
{
if(!dfn[EDGE[i].to]){
Tarjan(EDGE[i].to);
low[u]=min(low[u],low[EDGE[i].to]);
}
else if(vis[EDGE[i].to]){
low[u]=min(low[u],low[EDGE[i].to]);
}
}
if(low[u]==dfn[u]){
int xx;
scc_cnt++;
do{
xx=stk[tot2--];
vc[scc_cnt].push_back(xx);
col[xx]=scc_cnt;
vis[xx]=;
}while(xx!=u);
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
edge_cnt=,tot1=,tot2=,scc_cnt=,tot0=;
scc_cnt=;
int n,m;
scanf("%d%d",&n,&m);
memset(head,-,sizeof(head));
memset(stk,,sizeof(stk));
memset(in,,sizeof(in));
memset(out,,sizeof(out));
memset(dfn,,sizeof(dfn));
memset(low,,sizeof(low));
memset(col,,sizeof(col));
memset(vis,,sizeof(vis));
memset(vvis,,sizeof(vvis));
while(m--)
{
int a,b;
scanf("%d%d",&a,&b);
add(a,b);
}
for(int i = ; i <= n; i++)
{
if(!dfn[i])Tarjan(i);
}
memset(head,-,sizeof(head));
int orz0=edge_cnt;
// cout << "$$$\n";
edge_cnt=;
for(int i = ; i < orz0 ; i++)
{
if(col[EDGE[i].from]!=col[EDGE[i].to])
{
add(col[EDGE[i].from],col[EDGE[i].to]);
// cout << col[EDGE[i].from] << endl;
}
}
// cout << "###\n";
int sum=;
//int sum1=0,sum2=0;
for(int i = ; i <= scc_cnt ; i++)
{
memset(used,,sizeof(used));
if(find(i))sum++;
}
cout << scc_cnt-sum <<endl;
for(int i = ; i <= scc_cnt ; i++)
vc[i].clear();
}
return ;
}
/*4 5
1 3
2 4
4 2
1 4
2 1*/
【HDOJ3861】【Tarjan缩点+最小路径覆盖】的更多相关文章
- HDU 3861 The King’s Problem(tarjan缩点+最小路径覆盖:sig-最大二分匹配数,经典题)
The King’s Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- 缩点+最小路径覆盖 hdu 3861
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3861 题意:输入t,表示t个样例.接下来每个样例第一行有两个数n,m表示点数和有向边的数量,接下来输入 ...
- Graph_Master(连通分量_C_Trajan缩点+最小路径覆盖)
hdu_3861 题目大意:给定一张有向图,若<u,v>可达(u可以走到v,或者 v可以走到u),则<u,v>需被划分在统一城邦,问最小划分城邦数. 题解:比较裸的题,可以看出 ...
- Light OJ 1429 Assassin`s Creed (II) BFS+缩点+最小路径覆盖
题目来源:Light OJ 1429 Assassin`s Creed (II) 题意:最少几个人走全然图 能够反复走 有向图 思路:假设是DAG图而且每一个点不能反复走 那么就是裸的最小路径覆盖 如 ...
- Light OJ 1406 Assassin`s Creed 状态压缩DP+强连通缩点+最小路径覆盖
题目来源:Light OJ 1406 Assassin`s Creed 题意:有向图 派出最少的人经过全部的城市 而且每一个人不能走别人走过的地方 思路:最少的的人能够走全然图 明显是最小路径覆盖问题 ...
- HDU 3861 The King's Problem(强连通分量缩点+最小路径覆盖)
http://acm.hdu.edu.cn/showproblem.php?pid=3861 题意: 国王要对n个城市进行规划,将这些城市分成若干个城市,强连通的城市必须处于一个州,另外一个州内的任意 ...
- HDU 3861.The King’s Problem 强联通分量+最小路径覆盖
The King’s Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- 【HDU3861 强连通分量缩点+二分图最小路径覆盖】
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3861 题目大意:一个有向图,让你按规则划分区域,要求划分的区域数最少. 规则如下:1.有边u到v以及有 ...
- hdoj 3861 The King’s Problem【强连通缩点建图&&最小路径覆盖】
The King’s Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
随机推荐
- xml常用操作(js、sql、vb)
我们经常会用到xml操作,如下介绍了js.sql.vb等对xml的操作. JS创建xml对象 //创建对象 function getDataXML() { var objTds = $(&qu ...
- 普通程序员,三年成为年薪70w架构师,只因做到了这些
每个程序员.或者说每个工作者都应该有自己的职业规划,如果你不是富二代,不是官二代,也没有职业规划,希望你可以思考一下自己的将来.今天给大家分享的是一篇来自阿里Java架构师对普通程序员的职业建议,希望 ...
- prppppne2
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http:/ ...
- day21 MRO和C3算法
核能来袭 --MRO和C3算法 1. python的多继承 2.python经典类的MRO 3.python新式类的MRO, C3算法 4.super 是什么鬼? 一.python的多继承 在前面的学 ...
- Java连接SqlServer 2008数据库
将sqljdbc4.jar包添加到工程 连接SqlServer 2008数据库 import java.sql.Connection; import java.sql.DriverManager; i ...
- Java多线程习题 ===重点 ,错题积累
多线程重点,错题分析 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: . 12: 13: 14: 15:
- aspnet core 2.0 发布之后没有 views文件夹
在项目文件里面 增加这个节点: MvcRazorCompileOnPublish 设置为false 是会发布views <PropertyGroup> <PackageTargetF ...
- dialog销毁不干净与弹出多个dialog问题
1.关闭dialog的时候不销毁.重新打开然后影响页面的效果与样式. 原因: dialog的close()只是将html片段隐藏,并没有销毁移除. 解决方式: 打开dialog的时候在写onClose ...
- 『转』VC++ webbrowser函数使用范例
/*============================说明部分================================= 实现一下函数需包含头文件 #include <Winine ...
- 构造方法调用另一个构造方法,用this
using System; class Person { public int age; public string name; public Person(int age, string name) ...