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

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
题目大意:给一个无向图,1)如果u v两点互相可达,则必须被划分为同一个区域,
            2)如果u 和 v 至少有一方可达另一方【并且不能经过别的区域,即所走路径必须和自己属于同一区域】则可以被划分为同一个区域
            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缩点+最小路径覆盖】的更多相关文章

  1. 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 ...

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

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

  3. Graph_Master(连通分量_C_Trajan缩点+最小路径覆盖)

    hdu_3861 题目大意:给定一张有向图,若<u,v>可达(u可以走到v,或者 v可以走到u),则<u,v>需被划分在统一城邦,问最小划分城邦数. 题解:比较裸的题,可以看出 ...

  4. Light OJ 1429 Assassin`s Creed (II) BFS+缩点+最小路径覆盖

    题目来源:Light OJ 1429 Assassin`s Creed (II) 题意:最少几个人走全然图 能够反复走 有向图 思路:假设是DAG图而且每一个点不能反复走 那么就是裸的最小路径覆盖 如 ...

  5. Light OJ 1406 Assassin`s Creed 状态压缩DP+强连通缩点+最小路径覆盖

    题目来源:Light OJ 1406 Assassin`s Creed 题意:有向图 派出最少的人经过全部的城市 而且每一个人不能走别人走过的地方 思路:最少的的人能够走全然图 明显是最小路径覆盖问题 ...

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

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

  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. 【HDU3861 强连通分量缩点+二分图最小路径覆盖】

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3861 题目大意:一个有向图,让你按规则划分区域,要求划分的区域数最少. 规则如下:1.有边u到v以及有 ...

  9. hdoj 3861 The King’s Problem【强连通缩点建图&&最小路径覆盖】

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

随机推荐

  1. windows添加PDF虚拟打印机

    添加PDF虚拟打印机(果真姜还是老的辣,我摸索了两天没结果的事情,大佬轻轻松松两分钟搞定...) 这种PDF虚拟打印机的功能是将需要被打印的内容写到当前系统的指定目录下的指定文件中.整个过程都不需要连 ...

  2. (C/C++学习笔记) 十九. 模板

    十九. 模板 ● 模板的基本概念 模板(template) 函数模板:可以用来创建一个通用功能的函数,以支持多种不同形参,进一步简化重载函数的函数体设计. 语法: template <<模 ...

  3. 1.2socket服务器使用多线程

    socket服务器代码如下 # -*- coding: utf-8 -*-from socket import * import time import threading,_thread as th ...

  4. Oracle创建database link(dblink)和同义词(synonym)

    同一个数据库不同用户之间建立dblink和synonym 1.建立dblink 实现在A用户下通过dblink访问B用户下的数据库表,需要在A用户下创建访问B库的dblink连接 --创建远程连接db ...

  5. 用FPGA对ASIC进行原型验证的过程(转)

    用FPGA对ASIC进行原型验证的过程   reference:http://xilinx.eetrend.com/d6-xilinx/article/2018-10/13736.html     鉴 ...

  6. Thread.join方法的解析(转)

    原文链接:https://www.cnblogs.com/huangzejun/p/7908898.html 1. join() 的示例和作用 1.1 示例 1 // 父线程 2 public cla ...

  7. 预期结果 参数化parametrize

    1.pytest.mark.parametrize装饰器可以实现测试用例参数化. 2.实例: import pytest @pytest.mark.parametrize("req,expe ...

  8. Android:E/Surface: getSlotFromBufferLocked: unknown buffer: 0x7f9d1b41c0

    这个问题是在测试leakCanaryTestDemo时发现的,期初看到有点蒙,这个demo中只使用了一个button和一个textView控件进行测试,按理说是不应该出现这种问题,在 网上查找这个问题 ...

  9. 【Python】多线程-3

    #练习:线程等待 Event e.set() e.wait()   from threading import Thread, Lock import threading import time   ...

  10. vuejs 在移动端调起键盘并触发‘前往’按钮

    <template> <div class="display"> <form @submit.prevent> <input @keyup ...