HDOJ题目3861 The King’s Problem(强连通,最小点覆盖)
2015-03-10 15:22
399 查看
The King’s Problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1830 Accepted Submission(s): 666
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: 3859 3868 3865 3862 3866
ac代码
#include<stdio.h> #include<string.h> #include<vector> #include<string> #include<iostream> #define max(a,b) (a>b?a:b) #define min(a,b) (a>b?b:a) using namespace std; int dfn[5050],low[5050],ins[5050],head[5050],belong[5050],stack[5050],link[5050]; int cnt,top,taj,time,n,m; vector<int>vt[5050]; struct s { int u,v,next; }edge[100010*2]; void init() { cnt=top=taj=time=0; memset(belong,-1,sizeof(belong)); memset(ins,0,sizeof(ins)); memset(low,-1,sizeof(low)); memset(dfn,-1,sizeof(dfn)); memset(head,-1,sizeof(head)); memset(stack,0,sizeof(stack)); } void add(int u,int v) { edge[cnt].u=u; edge[cnt].v=v; edge[cnt].next=head[u]; head[u]=cnt++; } void tarjin(int u) { dfn[u]=low[u]=time++; stack[top++]=u; ins[u]=1; for(int i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].v; if(dfn[v]==-1) { tarjin(v); low[u]=min(low[u],low[v]); } else if(ins[v]) low[u]=min(dfn[v],low[u]); } if(dfn[u]==low[u]) { taj++; int now; do { now=stack[--top]; belong[now]=taj; ins[now]=0; }while(u!=now); } } int dfs(int u) { for(int i=0;i<vt[u].size();i++) { int v=vt[u][i]; if(!ins[v]) { ins[v]=1; if(link[v]==-1||dfs(link[v])) { link[v]=u; return 1; } } } return 0; } int main() { int t; scanf("%d",&t); while(t--) { int i; init(); scanf("%d%d",&n,&m); for(i=1;i<=n;i++) vt[i].clear(); while(m--) { int u,v; scanf("%d%d",&u,&v); add(u,v); } for(i=1;i<=n;i++) { if(dfn[i]==-1) { tarjin(i); } } for(i=0;i<cnt;i++) { int u=edge[i].u; int v=edge[i].v; if(belong[u]!=belong[v]) { vt[belong[u]].push_back(belong[v]); // printf("%d %d %d %d\n",u,v,belong[u],belong[v]); } } memset(link,-1,sizeof(link)); int ans=0; for(i=1;i<=taj;i++) { memset(ins,0,sizeof(ins)); if(dfs(i)) ans++; } printf("%d\n",taj-ans); } }
相关文章推荐
- hdoj 3861 The King’s Problem 【有向图tarjan求SCC + 缩点 + 最小路径覆盖】
- hdoj 3861 The King’s Problem【强连通缩点建图&&最小路径覆盖】
- hdu 3861 The King’s Problem【强连通Kosaraju+最小路径覆盖】
- HDOJ 3861 - The King’s Problem tarjan求强联通分量&缩点&有向图最小路径覆盖(匈牙利)
- The King’s Problem(tarjan求强连通分量缩点+匈牙利求有向无环图的最小路径覆盖)
- 【HDU】3861 The King’s Problem 强连通缩点+有向图最小路径覆盖
- HDU 3861 The King’s Problem(强连通+二分图最小路径覆盖)
- HDU - 3861 The King’s Problem(强联通分量+最小路径覆盖)
- HDU 3861--The King’s Problem【scc缩点构图 && 二分匹配求最小路径覆盖】
- hdu 3861 The King’s Problem (强连通+最小路径覆盖)
- HDU 3861 The King’s Problem 强连通缩点+最小路径覆盖
- HDU 3861--The King’s Problem【scc缩点构图 && 二分匹配求最小路径覆盖】
- 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 3861 The King’s Problem 强连通分量分解 + 二分图最小路径覆盖
- HDU 3861 The King’s Problem 强连通+最小路径覆盖
- HDU 3861 The King’s Problem(tarjan缩点+最小路径覆盖ISAP)
- HDU - 3861 The King’s Problem(强连通分量+最小路径覆盖)