hdu 3861 The King’s Problem (强连通+最小路径覆盖)
来源:互联网 发布:淘宝机器人客服 编辑:程序博客网 时间:2024/05/18 08:46
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 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.
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.
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
13 21 21 3
Sample Output
2
#include"stdio.h"#include"string.h"#include"queue"#include"vector"#include"algorithm"using namespace std;#define N 5005#define M 100005#define min(a,b) (a<b?a:b)vector<int>g[N];struct node{ int u,v,next;}e[M];int t,stop,index,bcnt,head[N],link[N];int mark[N],dfn[N],low[N],stap[N],be[N];bool vis[N];int find(int k){ int i,v; for(i=0;i<g[k].size();i++) { v=g[k][i]; if(!vis[v]) { vis[v]=true; if(link[v]==-1||find(link[v])) { link[v]=k; return 1; } } } return 0;}void add(int u,int v){ e[t].u=u; e[t].v=v; e[t].next=head[u]; head[u]=t++;}void tarjan(int u){ int i,v; dfn[u]=low[u]=++index; stap[++stop]=u; mark[u]=1; for(i=head[u];i!=-1;i=e[i].next) { v=e[i].v; if(!dfn[v]) { tarjan(v); low[u]=min(low[u],low[v]); } else if(mark[v]) low[u]=min(low[u],dfn[v]); } if(dfn[u]==low[u]) { bcnt++; do { v=stap[stop--]; mark[v]=0; be[v]=bcnt; } while(u!=v); }}void solve(int n){ int i; index=bcnt=stop=0; memset(dfn,0,sizeof(dfn)); for(i=1;i<=n;i++) { if(!dfn[i]) tarjan(i); }}void work(int n){ int i,j,u,v; for(i=0;i<N;i++) g[i].clear(); for(i=1;i<=n;i++) { u=be[i]; for(j=head[i];j!=-1;j=e[j].next) { v=be[e[j].v]; if(u!=v) { g[u].push_back(v); // g[v].push_back(u); } } } int ans=0; memset(link,-1,sizeof(link)); for(i=1;i<=bcnt;i++) { memset(vis,false,sizeof(vis)); ans+=find(i); } printf("%d\n",bcnt-ans);}int main(){ int T,n,m,u,v,i; scanf("%d",&T); while(T--) { scanf("%d%d",&n,&m); t=0; memset(head,-1,sizeof(head)); for(i=0;i<m;i++) { scanf("%d%d",&u,&v); add(u,v); } solve(n); //printf("bcnt%d\n",bcnt); work(n); } return 0;}
0 0
- hdu 3861 The King’s Problem (强连通+最小路径覆盖)
- HDU 3861 The King’s Problem(强连通+二分图最小路径覆盖)
- HDU 3861 The King’s Problem 强连通+最小路径覆盖
- HDU - 3861 The King’s Problem(强连通分量+最小路径覆盖)
- HDU 3861The King’s Problem 强连通分量分解 + 二分图最小路径覆盖
- hdu 3861 The King’s Problem【强连通Kosaraju+最小路径覆盖】
- hdu 1269 The King’s Problem(强连通分量+缩点+最小路径覆盖)
- hdu3861 The King’s Problem【强连通+最小路径覆盖】
- hdu 3681The King’s Problem(强连通分量+最小路径覆盖)
- HDU 3861 The King’s Problem (强连通分量缩点+二分图匹配最小路径覆盖)
- HDU 3861 The King’s Problem(强连通缩点 + 最小路径覆盖)
- hdu3861 The King’s Problem (强连通+最小路径覆盖)
- [HDU 3861] The King's Problem (最小路径覆盖)
- HDOJ题目3861 The King’s Problem(强连通,最小点覆盖)
- 【HDU】3861 The King’s Problem 强连通缩点+有向图最小路径覆盖
- The King’s Problem (hdu 3861 强连通缩点+最小路径覆盖)
- HDU 3861 The King’s Problem 强联通缩点 + 最小路径覆盖
- HDU --- 3861 The King’s Problem 【强联通缩点 + 最小路径覆盖】
- 软件版本号规则和命名规则
- 移植OpenWrt到RT5350
- Android PagerSlidingTitleIconTabStrip 可以在title旁边加小图标的PagerSlidingTabStrip
- javascript原生的事件扩展
- Win32串口API
- hdu 3861 The King’s Problem (强连通+最小路径覆盖)
- Java连接数据库主要有两种方式
- 2013年度总结——那年毕业
- HDU 1503 LCS 最长公共子串
- 查询每秒平均事务数
- COCOS2D-X事件类发布
- Android BLE开发记录
- JAX-RS
- Vimrc常用设置