# HDU - 3861 The King’s Problem

Problem
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

ans = 缩点后点的数量 - 最大匹配数。

``````#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<iostream>
#include<vector>
#include<stack>
using namespace std;
const int maxn = 5050;
vector<int> G[maxn];
vector<int> G2[maxn];
int dfn[maxn],low[maxn],belong[maxn];
int match[maxn];
bool vis[maxn];
int cnt,index;
stack<int> S;

void Clear()
{
for(int i=0;i<maxn;i++)
G[i].clear(),G2[i].clear();
cnt = index = 0;
memset(dfn,0,sizeof(dfn));
memset(low,0,sizeof(low));
memset(belong,0,sizeof(belong));
memset(match,0,sizeof(match));
memset(vis,0,sizeof(vis));
}

void Tarjan(int u)
{
dfn[u] = low[u] = ++index;
S.push(u);
vis[u]=true;
for(int i=0;i<G[u].size();i++)
{
int v = G[u][i];
if(!dfn[v])
{
Tarjan(v);
low[u] = min(low[u],low[v]);
}
else if(!belong[v])
{
low[u] = min(low[u],dfn[v]);
}
}
if(dfn[u] == low[u])
{
int x;
cnt++;
do
{
x = S.top(); S.pop();
vis[x] = false;
belong[x] = cnt;
}
while(u!=x);
}
}
bool dfs(int u)
{
for(int i = 0;i<G2[u].size();i++)
{
int v = G2[u][i];
if(!vis[v])
{
vis[v] = true;
if(match[v] == 0 || dfs(match[v]))
{
match[v] = u;
return true;
}
}
}
return false;
}
int hungary()
{
int res = 0;
memset(match,0,sizeof(match));
for(int u=1;u<=cnt;u++)
{
memset(vis,0,sizeof(vis));
if(dfs(u))  res++;
}
return res;
}
int n,m;
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
Clear();
scanf("%d%d",&n,&m);
for(int i=0;i<m;i++)
{
int a,b;
scanf("%d%d",&a,&b);
G[a].push_back(b);
}
for(int i=1;i<=n;i++)
{
if(!dfn[i]) Tarjan(i);
}
//printf("***********\n");
for(int i=1;i<=n;i++)
{
for(int j=0;j<G[i].size();j++)
{
int x = belong[i];
int y = belong[ G[i][j] ];
if(x != y)
{
G2[x].push_back(y);
}
}
}
int ans = cnt - hungary();
printf("%d\n",ans);
}
}
``````