The King’s Problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2259    Accepted Submission(s):
795

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个城市,m条路)分成若干个州,要求:如果两个城市u,v之间有路(u,v)和(v,u)则这两个城市必须在同一个州中,且任意 两个州之间要有路;现在问你   国王最少要将王国分成多个州
 题解:首先求scc缩点建新图,之后,因为有的scc之间有边相连,即可以匹配,所以我们找出新图的最大匹配数,求出最小路径覆盖:用最少的边来覆盖所有的点
#include<stdio.h>
#include<string.h>
#include<stack>
#include<queue>
#include<vector>
#include<algorithm>
#define MAX 5200
#define MAXM 200100
using namespace std;
vector<int>newmap[MAX];
vector<int>scc[MAX];
int sccno[MAX];
int in[MAX],out[MAX];
int scccnt,dfsclock;
int n,m;
int low[MAX],dfn[MAX];
int instack[MAX];
int ans,head[MAX];
int vis[MAX],city[MAX];
stack<int>s;
struct node
{
int beg,end,next;
}edge[MAXM];
void init()
{
ans=0;
memset(head,-1,sizeof(head));
}
void add(int u,int v)
{
edge[ans].beg=u;
edge[ans].end=v;
edge[ans].next=head[u];
head[u]=ans++;
}
void getmap()
{
int a,b;
while(m--)
{
scanf("%d%d",&a,&b);
add(a,b);
}
}
void tarjan(int u)
{
int v,i,j;
low[u]=dfn[u]=++dfsclock;
s.push(u);
instack[u]=1;
for(i=head[u];i!=-1;i=edge[i].next)
{
v=edge[i].end;
if(!dfn[v])
{
tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(instack[v])
low[u]=min(low[u],dfn[v]);
}
if(dfn[u]==low[u])
{
scccnt++;
while(1)
{
v=s.top();
s.pop();
instack[v]=0;
sccno[v]=scccnt;
if(v==u)
break;
}
}
}
void find(int l,int r)
{
memset(low,0,sizeof(low));
memset(dfn,0,sizeof(dfn));
memset(instack,0,sizeof(instack));
memset(sccno,0,sizeof(sccno));
dfsclock=scccnt=0;
for(int i=l;i<=r;i++)
{
if(!dfn[i])
tarjan(i);
}
}
void suodian()
{
find(1,n);
for(int i=1;i<=scccnt;i++)
newmap[i].clear();
memset(in,0,sizeof(in));
memset(out,0,sizeof(out));
int u,v,i,j;
for(i=0;i<ans;i++)
{
u=sccno[edge[i].beg];
v=sccno[edge[i].end];
if(u!=v)
{
newmap[u].push_back(v);
in[v]++;
out[u]++;
}
}
}
int query(int x)
{
int i,j;
for(i=0;i<newmap[x].size();i++)
{
int y=newmap[x][i];
if(!vis[y])
{
vis[y]=1;
if(city[y]==0||query(city[y]))
{
city[y]=x;
return 1;
}
}
}
return 0;
}
void solve()
{
int i,j;
int sum=0;
memset(city,0,sizeof(city));
for(i=1;i<=scccnt;i++)
{
memset(vis,0,sizeof(vis));
if(query(i))
sum++;
}
printf("%d\n",scccnt-sum);//最小路径覆盖=顶点数-最大匹配数
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
init();
getmap();
suodian();
solve();
}
return 0;
}

  

 

hdoj 3861 The King’s Problem【强连通缩点建图&&最小路径覆盖】的更多相关文章

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

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

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

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

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

  4. HDU 3861 The King’s Problem 强连通分量 最小路径覆盖

    先找出强连通分量缩点,然后就是最小路径覆盖. 构造一个二分图,把每个点\(i\)拆成两个点\(X_i,Y_i\). 对于原图中的边\(u \to v\),在二分图添加一条边\(X_u \to Y_v\ ...

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

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

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

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

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

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

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

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

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

随机推荐

  1. codevs 1540 银河英雄传说

    题目描述 Description 公元五八○一年,地球居民迁移至金牛座α第二行星,在那里发表银河联邦创立宣言,同年改元为宇宙历元年,并开始向银河系深处拓展. 宇宙历七九九年,银河系的两大军事集团在巴米 ...

  2. Monitor All SQL Queries in MySQL (alias mysql profiler)

    video from youtube: http://www.youtube.com/watch?v=79NWqv3aPRI one blog post: Monitor All SQL Querie ...

  3. I2C总线模拟(郭天祥视屏)

    电路图 思路 1.向EEPROM中通过I2C总线写入一个字节 2.通过I2C总线读出写入的字节 3.如果写入和读出成功点亮发光二极管 程序 #include <REGX51.H> #def ...

  4. 告诉你KVC的一切-b

    KVC(Key-value coding)键值编码,单看这个名字可能不太好理解.其实翻译一下就很简单了,就是指iOS的开发中,可以允许开发者通过Key名直接访问对象的属性,或者给对象的属性赋值.而不需 ...

  5. c++ 输入一行字符串

    ]; //cin>>str1; 方式1 不能统计(录入)空格后的字符 cin.); //方式2 能统计空格后输入的字符 按回车键输入结束 get()会将换行符保存在序列里 //gets(s ...

  6. float([x]): 将一个字符串或数转换为浮点数。如果无参数将返回0.0

    float([x]): 将一个字符串或数转换为浮点数.如果无参数将返回0.0 >>> float(12) 12.0 >>> float(-122) -122.0 & ...

  7. Android-java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

    章出自:luchg技术交流 http://www.luchg.com 版权所有.本站文章除注明出处外,皆为作者原创文章,可自由引用,但请注明来源,谢谢. Android-java.lang.Runti ...

  8. 年度十佳 DevOps 博客文章(前篇)

    如果说 15 年你还没有将 DevOps 真正应用起来,16 年再不实践也未免太落伍了.国内 ITOM 领军企业 OneAPM 工程师为您翻译整理了,2015 年十佳 DevOps 文章,究竟是不是深 ...

  9. [线段树]HDOJ5091 Beam Cannon

    题意:给n, w, h  (1 <= N <= 10000,1 <= W <= 40000,1 <= H <= 40000) $w\times h$是可以射到的范围 ...

  10. servlet向ajax传递list数据类型,ajax向servlet传递array数据类型

    因工作需要, 1,后台向前台传递一个list 2,前台向后台传递类似于list的结构,但是因为javascript不支持list类型,所以只能使用二维数组代替 后台运行后的截图:           ...