题意:给你n个城市,每个城市之间有一条有向边,将城市划分为几个区域,问你最小的划分方法,

划分规则为:能相互到达的放在一个区域;然后区域内的a,b两点肯定存在某种方式,使得a能到b或者b能到a(注意,这里没说一定是相互能到);

解题思路:这道题其实就是DAG上的对应二分图的最小路径覆盖;

因为DAG,所以先缩点,然后对应二分图的最小路径覆盖=顶点数-对应二分图的最大匹配;

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<vector>
#define N 5005
#define M 100005
using namespace std;
struct node
{
int x;
int y;
}a[M];
struct Edge
{
int next;
int to;
}edge[M];
int ans=0;
int match[M];
int head[M];
int instack[M];
int visit[M];
int low[M];
int dfn[M];
int sccno[M];
int book[M];
int cnt;
int step;
int index;
int scc_cnt;
bool e[N][N];
int n,m;
vector<int>scc[N];
void add(int u,int v)
{
edge[cnt].next=head[u];
edge[cnt].to=v;
head[u]=cnt++;
}
void tarjan(int u)
{
low[u]=dfn[u]=++step;
visit[u]=1;
instack[++index]=u;
for(int i=head[u];i!=-1;i=edge[i].next)
{
if(!dfn[edge[i].to])
{
tarjan(edge[i].to);
low[u]=min(low[u],low[edge[i].to]);
}
else if(visit[edge[i].to])
{
low[u]=min(low[u],dfn[edge[i].to]);
}
}
if(low[u]==dfn[u])
{
scc_cnt++;
scc[scc_cnt].clear();
do
{
scc[scc_cnt].push_back(instack[index]);
sccno[instack[index]]=scc_cnt;
visit[instack[index]]=0;
index--;
}
while(u!=instack[index+1]);
}
return;
} bool dfs(int u)
{
for(int i=1;i<=scc_cnt;i++)
{
if(book[i]==0&&e[u][i])
{
book[i]=1;
if(match[i]==-1||dfs(match[i]))
{
match[i]=u;
return true;
}
}
}
return false;
}
void init()
{
memset(instack,0,sizeof(instack));
memset(match,-1,sizeof(match));
memset(e,0,sizeof(e));
memset(visit,0,sizeof(visit));
memset(low,0,sizeof(low));
memset(dfn,0,sizeof(dfn));
memset(head,-1,sizeof(head));
memset(book,0,sizeof(book));
cnt=step=scc_cnt=index=ans=0;
}
int main()
{
int t;
init();
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
{
scanf("%d%d",&a[i].x,&a[i].y);
add(a[i].x,a[i].y);
}
for(int i=1;i<=n;i++)
{
if(!dfn[i])
tarjan(i);
}
for(int i=1;i<=m;i++)
{
if(sccno[a[i].x]!=sccno[a[i].y])
{
e[sccno[a[i].x]][sccno[a[i].y]]=1;
// e[sccno[a[i].y]][sccno[a[i].x]]=1;
}
}
for(int i=1;i<=scc_cnt;i++)
{
memset(book,0,sizeof(book));
if(dfs(i))
ans++;
}
printf("%d\n",scc_cnt-ans);
init();
}
return 0;
}

  

hdu—3861(tarjan+二分图)的更多相关文章

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

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

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

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

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

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

  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(tarjan缩点+最小路径覆盖:sig-最大二分匹配数,经典题)

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

  6. 缩点+最小路径覆盖 hdu 3861

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3861 题意:输入t,表示t个样例.接下来每个样例第一行有两个数n,m表示点数和有向边的数量,接下来输入 ...

  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 题目大意: 在csdn王国里面, 国王有一个新的问题. 这里有N个城市M条单行路,为了让他的王国 ...

  9. hdu 2119(简单二分图) Matrix

    http://acm.hdu.edu.cn/showproblem.php?pid=2119 一个由0和1构成的矩阵,每次选取一行或者一列将其中的1变成0,求最小删除次数 简单的二分图应用,矩阵的横坐 ...

随机推荐

  1. selenium:解决页面元素display:none的方法

    在UI自动化测试中,有时候会遇到页面元素无法定位的问题,包括xpath等方法都无法定位,是因为前端元素被设置为不可见导致. 这篇博客,介绍下如何通过JavaScript修改页面元素属性来定位的方法.. ...

  2. object detection[YOLO]

    这部分,我们来聊聊YOLO. YOLO:You Only Look Once,顾名思义,就是希望网络在训练过程中,一张图片只要看一次就行,不需要去多次观察,比如滑框啥的,从而从底层原理上就减少了很多的 ...

  3. Codechef SUMCUBE Sum of Cubes 组合、三元环计数

    传送门 好久没有做过图论题了-- 考虑\(k\)次方的组合意义,实际上,要求的所有方案中导出子图边数的\(k\)次方,等价于有顺序地选出其中\(k\)条边,计算它们在哪一些图中出现过,将所有方案计算出 ...

  4. C#总结(五)调用C++动态库(类型对照)

    函数调用导致堆栈不对称.原因可能是托管的 PInvoke 签名与非托管的目标签名不匹配. 在dllimport中加入CallingConvention参数就行了, [DllImport(PCAP_DL ...

  5. 通过 JS 脚本去除csdn广告

    1. chorme 浏览器 1.1 通过书签方式添加 新建书签: 在网址一栏中输入: javascript: $(function () { $('aside .csdn-tracking-stati ...

  6. 朱晔的互联网架构实践心得S1E5:不断耕耘的基础中间件

    朱晔的互联网架构实践心得S1E5:不断耕耘的基础中间件 [下载本文PDF进行阅读] 一般而言中间件和框架的区别是,中间件是独立运行的用于处理某项专门业务的CS程序,会有配套的客户端和服务端,框架虽然也 ...

  7. sql面试学到新内容

    1.事物的保存点 MYSQL可以让我们对事务进行部分回滚,就是在事务里调用SAVEPOINT语句来设置一些命名标记.如果想要回滚到那个标记点位置,需要使用ROLLBACK语句来指定哪个保存点. mys ...

  8. [LeetCode] Department Highest Salary -- 数据库知识(mysql)

    184. Department Highest Salary The Employee table holds all employees. Every employee has an Id, a s ...

  9. Python_列表推导式_生成器的表达式_各种推导式_40

    列表推导式: #列表推导式: egg_list = [] for i in range(10): egg_list.append('鸡蛋%s'%i) print(egg_list) egon egg_ ...

  10. Success Rate CodeForces - 807C (数学+二分)

    You are an experienced Codeforces user. Today you found out that during your activity on Codeforces ...