The King’s Problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2523 Accepted Submission(s): 905
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
Source
Recommend
题目大意:一个有向图,让你按规则划分区域,要求划分的区域数最少。
规则如下:1、有边u到v以及有边v到u,则u,v必须划分到同一个区域内。2、一个区域内的两点至少要有一方能到达另一方。3、一个点只能划分到一个区域内。
题解:用tarjan 算法缩完点,然后跑二分图的最大匹配,最小路径覆盖数=点数-最大匹配数
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 500030
using namespace std;
int n,m;
int point[N],next1[N],v[N],belong[N],st[N];
int cnt,tot,top,sz,dfs[N],low[N],x[N],y[N];
int a[N],b[N],ins[N],vis[N],belong1[N];
void add(int x,int y)
{
tot++; next1[tot]=point[x]; point[x]=tot; v[tot]=y;
}
void tarjan(int x)
{
st[++top]=x; ins[x]=1;
dfs[x]=low[x]=++sz;
for (int i=point[x];i;i=next1[i])
{
int j=v[i];
if (!dfs[j])
{
tarjan(j);
low[x]=min(low[x],low[j]);
}else if(ins[j]) low[x]=min(low[x],dfs[j]);
}
if (dfs[x]==low[x])
{
cnt++; int j=0;
do
{
j=st[top--];
ins[j]=0;
belong[j]=cnt;
}while (j!=x);
}
}
int find(int x,int j)
{
for (int i=point[x];i;i=next1[i])
{
int t=v[i];
if (vis[t]==j) continue;
vis[t]=j;
if (!belong1[t]||find(belong1[t],j))
{
belong1[t]=x;
return true;
}
}
return false;
}
int main()
{
int t; scanf("%d",&t);
for (int T=1;T<=t;T++)
{
scanf("%d%d",&n,&m);
cnt=top=tot=sz=0;
memset(next1,0,sizeof(next1));
memset(point,0,sizeof(point));
memset(dfs,0,sizeof(dfs));
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
for (int i=1;i<=m;i++)
{
scanf("%d%d",&x[i],&y[i]);
add(x[i],y[i]);
}
for (int i=1;i<=n;i++)
if (!dfs[i]) tarjan(i);
tot=0; memset(point,0,sizeof(point));
memset(next1,0,sizeof(next1));
for (int i=1;i<=m;i++)
{
int x1=belong[x[i]]; int y1=belong[y[i]];
if (x1!=y1)
add(x1,y1);
}
int num=0;
memset(belong1,0,sizeof(belong1));
memset(vis,0,sizeof(vis));
for (int i=1;i<=cnt;i++)
if (find(i,i)) num++;
printf("%d\n",cnt-num);
}
}