HDU——T The King’s Problem

http://acm.hdu.edu.cn/showproblem.php?pid=3861

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

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

Source

2011 Multi-University Training Contest 3 - Host by BIT

Recommend

lcy   |   We have carefully selected several similar problems for you:  3863 3859 3868 3865 3862

Tarjan缩点+最大独立集(强连通个数-最大匹配数)

  1 #include <cstring>
  2 #include <cstdio>
  3
  4 #define min(a,b) (a<b?a:b)
  5 #define max(a,b) (a>b?a:b)
  6 const int N(5000+115);
  7 const int M(100000+5);
  8 int hed[N],sumedge,had[N];
  9 struct Edge
 10 {
 11     int v,next;
 12     Edge(int v=0,int next=0):v(v),next(next){}
 13 }edge[M],e[M];
 14 inline void ins(int u,int v,int *head,Edge *edge)
 15 {
 16     edge[++sumedge]=Edge(v,head[u]);
 17     head[u]=sumedge;
 18 }
 19
 20 int tim,dfn[N],low[N];
 21 int top,instack[N],Stack[N];
 22 int sumcol,col[N],rd[N],cd[N];
 23 void DFS(int u)
 24 {
 25     low[u]=dfn[u]=++tim;
 26     Stack[++top]=u; instack[u]=1;
 27     for(int v,i=hed[u];i;i=edge[i].next)
 28     {
 29         v=edge[i].v;
 30         if(!dfn[v]) DFS(v), low[u]=min(low[u],low[v]);
 31         else if(instack[v]) low[u]=min(low[u],dfn[v]);
 32     }
 33     if(low[u]==dfn[u])
 34     {
 35         col[u]=++sumcol;
 36         for(;u!=Stack[top];top--)
 37         {
 38             col[Stack[top]]=sumcol;
 39             instack[Stack[top]]=0;
 40         }
 41         instack[u]=0; top--;
 42     }
 43 }
 44
 45 int sumvis,vis[N],match[N];
 46 bool find(int u)
 47 {
 48     for(int v,i=had[u];i;i=e[i].next)
 49     {
 50         v=e[i].v;
 51         if(vis[v]==sumvis) continue;
 52         vis[v]=sumvis;
 53         if(!match[v]||find(match[v]))
 54         {
 55             match[v]=u;
 56             return true;
 57         }
 58     }
 59     return false;
 60 }
 61
 62 inline void init()
 63 {
 64     tim=top=sumedge=sumcol=sumvis=0;
 65     memset(e,0,sizeof(e));
 66     memset(vis,0,sizeof(vis));
 67     memset(col,0,sizeof(col));
 68     memset(dfn,0,sizeof(dfn));
 69     memset(low,0,sizeof(low));
 70     memset(hed,0,sizeof(hed));
 71     memset(had,0,sizeof(had));
 72     memset(edge,0,sizeof(edge));
 73     memset(Stack,0,sizeof(Stack));
 74     memset(match,0,sizeof(match));
 75     memset(instack,0,sizeof(instack));
 76 }
 77 inline void read(int &x)
 78 {
 79     x=0; register char ch=getchar();
 80     for(;ch>‘9‘||ch<‘0‘;) ch=getchar();
 81     for(;ch>=‘0‘&&ch<=‘9‘;ch=getchar()) x=x*10+ch-‘0‘;
 82 }
 83
 84 int main()
 85 {
 86     int t; read(t);
 87     for(int n,m;t--;init())
 88     {
 89         read(n),read(m);
 90         for(int u,v;m--;)
 91             read(u),read(v),ins(u,v,hed,edge);
 92         for(int i=1;i<=n;i++)
 93             if(!dfn[i]) DFS(i);
 94         for(int u=1;u<=n;u++)
 95             for(int v,i=hed[u];i;i=edge[i].next)
 96             {
 97                 v=edge[i].v;
 98                 if(col[u]!=col[v]) ins(col[u],col[v],had,e);
 99             }
100         int ans=0;
101         for(int i=1;i<=sumcol;i++)
102         {
103             sumvis++;
104             if(find(i)) ans++;
105         }
106         printf("%d\n",sumcol-ans);
107     }
108     return 0;
109 }
时间: 2024-11-02 22:39:07

HDU——T The King’s Problem的相关文章

hdu 3861 The King’s Problem (强连通+最小路径覆盖)

The King's Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1637    Accepted Submission(s): 600 Problem Description In the Kingdom of Silence, the king has a new problem. There are N cit

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

HDU 3861 The King's Problem 题目链接 题意:给定一个有向图,求最少划分成几个部分满足下面条件 互相可达的点必须分到一个集合 一个对点(u, v)必须至少有u可达v或者v可达u 一个点只能分到一个集合 思路:先强连通缩点,然后二分图匹配求最小路径覆盖 代码: #include <cstdio> #include <cstring> #include <vector> #include <algorithm> #include <

HDU 3861 The King&#39;s Problem(强连通分量缩点+最小路径覆盖)

http://acm.hdu.edu.cn/showproblem.php?pid=3861 题意: 国王要对n个城市进行规划,将这些城市分成若干个城市,强连通的城市必须处于一个州,另外一个州内的任意两个城市u,v,有从u到v的路径或从v到u的路径.求最少可以分成几个州. 思路: 这道题目挺好. 首先,强连通分量进行缩点,重新建图. 新建的图就是一个DAG图,接下来就转换成了最小路径覆盖问题. 最小路径覆盖就是用尽量少的不相交的简单路径覆盖DAG的所有顶点.每个顶点只属于一条路径,单个顶点也可以

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

The King’s Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2947    Accepted Submission(s): 1049 Problem Description In the Kingdom of Silence, the king has a new problem. There are N cit

hdu 3861 The King’s Problem trajan缩点+二分图匹配

The King’s Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) 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 betw

HDU 3861 The King’s Problem (强连通+二分匹配)

题目地址:HDU 3861 这题虽然是两个算法结合起来的.但是感觉挺没意思的..结合的一点也不自然,,硬生生的揉在了一块...(出题者不要喷我QAQ.) 不过这题让我发现了我的二分匹配已经好长时间没用过了..都快忘了..正好在省赛之前又复习了一下. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm>

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

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3861 最小路径覆盖的一篇博客:https://blog.csdn.net/qq_39627843/article/details/82012572 题意: 把城市至少分成几个块,规则有三 1. A能B,B能到A,那么A,B一定要在一起. 2. 一个城市只能属于一个块. (说明了是最小不相交覆盖)3. 在一个块里的城市,任意2点之间必须有路径. 对于规则1,就是说强连通的必须在一起,所以用Tarjan

HDU 3861 The King’s Problem

题意:给我们一个图,问我们最少能把这个图分成几部分,使得每部分内的任意两点都能至少保证单向连通. 思路:使用tarjan算法求强连通分量然后进行缩点,形成一个新图,易知新图中的每个点内部的内部点都能保证双向连通,而新图中的点都是单向无环的,这个时候题目中要求的划分部分的条件,其实就是求最短路径覆盖(需要好好想一想),最短路径覆盖 = 结点个数 - 最大匹配值. 这个题我当时把j写成了i,就这么一个小地方,找了快20分钟,还死活发现不了..真是晕死了- #include<iostream> #i

HDU - 3861 The King’s Problem (强连通分量+最小路径覆盖)

思路:tarjarn缩点,然后剩下的就是纯粹的最小路径覆盖,最小路径覆盖=顶点数-匹配数.匹配数跑一遍匈牙利即可. 1 #include <iostream> 2 #include <queue> 3 #include <stack> 4 #include <cstdio> 5 #include <vector> 6 #include <map> 7 #include <set> 8 #include <bitset