博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 3861--The King’s Problem【scc缩点构图 && 二分匹配求最小路径覆盖】
阅读量:6338 次
发布时间:2019-06-22

本文共 3799 字,大约阅读时间需要 12 分钟。

The King’s Problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2137    Accepted Submission(s): 763
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
 
题意:国王要给n个城市进行规划。分成若干个州。有三点要求:1、有边u到v以及有边v到u,则u,v必须划分到同一个州内。

2、一个州内的两点至少要有一方能到达还有一方。3、一个点仅仅能划分到一个州内。问他至少要建多少州

思路:先把能相互两两到达的点用强连通归为一个州,然后再进行缩点。建立新图。然后用匈牙利算法求出最大匹配,答案=强连通求出的联通块-最大匹配(最小路径覆盖=结点数-最大匹配)。

#include 
#include
#include
#include
#include
#define maxn 50000+100#define maxm 200000+100using namespace std;int n, m;struct node { int u, v, next;};node edge[maxm];int head[maxn], cnt;int low[maxn], dfn[maxn];int dfs_clock;int Stack[maxn], top;bool Instack[maxn];int Belong[maxn];int scc_clock;vector
Map[maxn];void init(){ cnt = 0; memset(head, -1, sizeof(head));}void addedge(int u, int v){ edge[cnt] = {u, v, head[u]}; head[u] = cnt++;}void getmap(){ scanf("%d%d", &n, &m); while(m--){ int a, b; scanf("%d%d", &a, &b); addedge(a, b); }}void Tarjan(int u, int per){ int v; low[u] = dfn[u] = ++dfs_clock; Stack[top++] = u; Instack[u] = true; for(int i = head[u]; i != -1; i = edge[i].next){ int v = edge[i].v; if(!dfn[v]){ Tarjan(v, u); low[u] = min(low[u], low[v]); } else if(Instack[v]) low[u] = min(low[u], dfn[v]); } if(dfn[u] == low[u]){ scc_clock++; do{ v = Stack[--top]; Instack[v] = false; Belong[v] = scc_clock; } while( v != u); }}void find(){ memset(low, 0, sizeof(low)); memset(dfn, 0, sizeof(dfn)); memset(Belong, 0, sizeof(Belong)); memset(Stack, 0, sizeof(Stack)); memset(Instack, false, sizeof(false)); dfs_clock = scc_clock = top = 0; for(int i = 1; i <= n ; ++i){ if(!dfn[i]) Tarjan(i, i); }}void suodian(){ for(int i = 1; i <= scc_clock; ++i) Map[i].clear(); for(int i = 0; i < cnt; ++i){ int u = Belong[edge[i].u]; int v = Belong[edge[i].v]; if(u != v){ Map[u].push_back(v); } }}int used[maxn], link[maxn];bool dfs(int x){ for(int i = 0; i < Map[x].size(); ++i){ int y = Map[x][i]; if(!used[y]){ used[y] = 1; if(link[y] == -1 || dfs(link[y])){ link[y] = x; return true; } } } return false;}void hungary(){ int ans = 0; memset(link, -1, sizeof(link)); for(int j = 1; j <= scc_clock; ++j){ memset(used, 0, sizeof(used)); if(dfs(j)) ans++; } printf("%d\n", scc_clock - ans);}int main (){ int T; scanf("%d", &T); while(T--){ init(); getmap(); find(); suodian(); hungary(); } return 0;}

转载地址:http://tbaoa.baihongyu.com/

你可能感兴趣的文章
修改CentOS Linux的时间可以使用date指令
查看>>
专业程序员必知的技巧
查看>>
javascript 基础
查看>>
我的友情链接
查看>>
从Check Point 官网使用Google Chrome 下载补丁需要特别注意的地方
查看>>
我的友情链接
查看>>
vim 纵向编辑
查看>>
Vsftp精解之安装配置及原理(一)
查看>>
我的友情链接
查看>>
思科路由与交换系列--------NA系列
查看>>
FragmentPagerAdapter不能更新fragment的问题
查看>>
Zepto源码分析(1)——类型判断函数
查看>>
阿里AoneFlow分支管理模式-个人理解
查看>>
LAMP环境下搭建discuz论坛
查看>>
运行程序时提示:Could not resize...No space left on device
查看>>
01前段一条龙-git仓库的建立
查看>>
我的程序员之路(一)
查看>>
我的友情链接
查看>>
1号店连添头都算不上,京东与沃尔玛,谁才是接盘侠
查看>>
隐藏数值为0的情况下的div
查看>>