题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3861

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

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

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

定义:

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

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

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

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

 #include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std; const int maxn=;
int dfn[maxn], low[maxn], stack[maxn], belong[maxn], visit[maxn], match[maxn];
bool instack[maxn];
int top, scnt, Index, n, m, T;
vector<int>vt[maxn]; struct Node
{
int u, v;
}f[*maxn]; void Init_tarjan()
{
top=scnt=Index=;
for(int i=; i<=n; i++) dfn[i]=low[i]=instack[i]=;
} void tarjan(int u)
{
stack[++top]=u;
dfn[u]=low[u]=++Index;
instack[u]=;
for(int i=; i<vt[u].size(); i++)
{
int v=vt[u][i];
if(!dfn[v])
{
tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(instack[v])
{
low[u]=min(low[u],dfn[v]);
}
}
if(low[u]==dfn[u])
{
int v;
scnt++;
do
{
v=stack[top--];
instack[v]=;
belong[v]=scnt;
}
while(u!=v);
}
} bool find(int u)
{
for(int i=; i<vt[u].size(); i++)
{
int v=vt[u][i];
if(!visit[v])
{
visit[v]=;
if(match[v]==-||find(match[v]))
{
match[v]=u;
return true;
}
}
}
return false;
} int hungary()
{
int cnt=;
memset(match,-,sizeof(match));
for(int i=; i<=scnt; i++)
{
for(int j=; j<=scnt; j++) visit[j]=;
if(find(i)) cnt++;
}
return cnt;
} int main()
{
cin >> T;
while(T--)
{
cin >> n >> m;
for(int i=; i<=n; i++) vt[i].clear();
for(int i=; i<m; i++)
{
scanf("%d%d",&f[i].u,&f[i].v);
vt[f[i].u].push_back(f[i].v);
}
Init_tarjan();
for(int i=; i<=n; i++)
if(!dfn[i]) tarjan(i);
for(int i=; i<=n; i++) vt[i].clear();
for(int i=; i<m; i++)
{
int u=belong[f[i].u], v=belong[f[i].v];
if(u==v) continue;
vt[u].push_back(v);
}
int ans=hungary();
cout << scnt-ans <<endl;
}
return ;
} /*
10
10 11
1 2
2 3
3 1
3 4
5 6
6 7
7 5
4 5
10 9
9 8
8 4
2
*/

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

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

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

  2. [bzoj2150]部落战争_二分图最小路径覆盖

    部落战争 bzoj-2150 题目大意:题目链接. 注释:略. 想法: 显然是最小路径覆盖,我们知道:二分图最小路径覆盖等于节点总数-最大匹配. 所以我们用匈牙利或者dinic跑出最大匹配,然后用总结 ...

  3. Taxi Cab Scheme POJ - 2060 二分图最小路径覆盖

    Running a taxi station is not all that simple. Apart from the obvious demand for a centralised coord ...

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

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

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

    对多校赛的题目,我深感无力.题目看不懂,英语是能懂的,题目具体的要求以及需要怎么做没有头绪.样例怎么来的都不明白.好吧,看题解吧. http://www.cnblogs.com/kane0526/ar ...

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

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

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

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

  8. HDU 3861 The King’s Problem(tarjan连通图与二分图最小路径覆盖)

    题意:给我们一个图,问我们最少能把这个图分成几部分,使得每部分内的任意两点都能至少保证单向连通. 思路:使用tarjan算法求强连通分量然后进行缩点,形成一个新图,易知新图中的每个点内部的内部点都能保 ...

  9. hdu 1151 Air Raid(二分图最小路径覆盖)

    http://acm.hdu.edu.cn/showproblem.php?pid=1151 Air Raid Time Limit: 1000MS   Memory Limit: 10000K To ...

随机推荐

  1. cve-2015-1635漏洞分析

    上周(2015.4.15)爆出的cve-2015-1635漏洞,遂分析一番,留作记录.使用poc如下. wget 192.168.16.168/welcome.png --debug --header ...

  2. 多个$(document).ready()函数的执行顺序问题,(未解决)

    今天遇到了一个问题: jQuery获取不了动态添加的元素,我使用的是append添加的.寻求了帮助,得到解决方案: 在文件开头写上这样一段代码来获取,写在$(document).ready()里面. ...

  3. hdu1087 最大递增子段和

    http://acm.split.hdu.edu.cn/showproblem.php?pid=1087 状态方程:sum[j]=max{sum[i]}+a[j]; 其中,0<=i<=j, ...

  4. css随记01编辑技巧,背景与边框

    代码优化 一个按钮的例子,使其值同比例变化; button{ color: white; background: #58a linear-gradient(#77a0bb, #58a); paddin ...

  5. DFS Codeforces Round #290 (Div. 2) B. Fox And Two Dots

    题目传送门 /* DFS:每个点四处寻找,判断是否与前面的颜色相同,当走到已走过的表示成一个环 */ #include <cstdio> #include <iostream> ...

  6. Android定位方式和测试方法

    Android常用的三种定位方式有:基于GPS定位.基于基站地位.基于wifi定位. 1.基于GPS定位: GPS定位需要GPS模块(硬件)的支持,没有GPS模块是无法进行GPS定位的. GPS定位最 ...

  7. iOS学习37数据处理之CoreData

    1. CoreData数据库框架的优势 1> CoreData历史 CoreData数据持久化框架是Cocoa API 的一部分,首次在iOS5版本的系统中出现,它允许按照实体-属性-值模型组织 ...

  8. 【Cocos2d-x游戏开发】解决Cocos2d-x中文乱码的三种方法

    众所周知,Cocos2d-x是一款不错的开源引擎,但是在Cocos2d-x中直接使用中文是无法正确显示的.比如下面的情况: 解决这个问题常用的有三种方法:1.通过转换为UTF-8编码来显示.2.使用i ...

  9. python 代码片段24

    #coding=utf-8 #内部类 class MyClass(object): class InnerClass: pass # 正则表达式 # 通过re模块来访问 import re m=re. ...

  10. leetcode Linked List Cycle

    Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ext ...