The King’s Problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1205 Accepted Submission(s): 453
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
1 3 2 1 2 1 3
Sample Output
2
题意:有n个城市,城市之间有若干条单向的道路。现在要把这n个城市划分为若干个州。规定:若u->v存在,v->u也存在,则u和v必须属于同一个州。另外,在同一个州中,任意两个城市u和v,必须要满足u->v存在或v->u存在。问最小可以把这n个城市划分为多少个州。
思路:把城市看作点,因为当两个城市相互可达时,他们属于同一个州,所以同属一个强连通分量的点都属于同一个州。所以要先对原图进行缩点,然后就是求最小路径覆盖了。
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <vector>
#include <cmath>
#include <stack>
#include <cstdlib>
using namespace std;
const int maxn=5005;
const int maxm=100005;
struct node
{
int v,next;
}edge[3*maxm];
int G[maxn],RG[maxn],MG[maxn],fin[maxn],scc[maxn],match[maxn];
bool vis[maxn];
int n,m,num,snum;
void init()
{
memset(G,-1,sizeof(G));
memset(RG,-1,sizeof(RG));
memset(MG,-1,sizeof(MG));
num=0;
}
void add(int *head,int u,int v)
{
edge[num].v=v;
edge[num].next=head[u];
head[u]=num++;
}
void dfs1(int x)
{
vis[x]=true;
for(int i=G[x];i!=-1;i=edge[i].next)
if(!vis[edge[i].v]) dfs1(edge[i].v);
fin[snum++]=x;
}
void dfs2(int x,int id)
{
vis[x]=true;
scc[x]=id;
for(int i=RG[x];i!=-1;i=edge[i].next)
if(!vis[edge[i].v]) dfs2(edge[i].v,id);
}
bool find(int x)
{
for(int i=MG[x];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
if(!vis[v])
{
vis[v]=true;
if(match[v]==-1||find(match[v]))
{
match[v]=x;
return true;
}
}
}
return false;
}
int MMG()
{
int ans=0;
memset(match,-1,sizeof(match));
for(int i=1;i<=snum;i++)
{
memset(vis,false,sizeof(vis));
if(find(i)) ans++;
}
return ans;
}
int main()
{
int t,a,b;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
init();
while(m--)
{
scanf("%d%d",&a,&b);
add(G,a,b);
add(RG,b,a);
}
memset(vis,false,sizeof(vis));
snum=1;
for(int i=1;i<=n;i++)
if(!vis[i]) dfs1(i);
memset(vis,false,sizeof(vis));
snum=1;
for(int i=n;i>=1;i--)
if(!vis[fin[i]])
{
dfs2(fin[i],snum);
snum++;
}
snum--;
for(int i=1;i<=n;i++)
for(int j=G[i];j!=-1;j=edge[j].next)
{
if(scc[i]!=scc[edge[j].v])
add(MG,scc[i],scc[edge[j].v]);
}
printf("%d\n",snum-MMG());
}
return 0;
}