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.
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
#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; }
hdu 3861 The King’s Problem (强连通+最小路径覆盖),布布扣,bubuko.com