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

这个题的意思如果几个点强连通,那么他们必须属于同一个state。
一个state里的任意两个点之间必须至少有一条单向边连通;
用tarjan缩个点,新建一个DAG图,然后再用匈牙利算法,求一下最大匹配数。
ans = 缩点后点的数量 - 最大匹配数。

贴代码 =.=

#include
#include
#include
#include
#include
#include
#include
using namespace std;
const int maxn = 5050;
vector G[maxn];
vector G2[maxn];
int dfn[maxn],low[maxn],belong[maxn];
int match[maxn];
bool vis[maxn];
int cnt,index;
stack S;

void Clear()
{
    for(int i=0;i

你可能感兴趣的:(HDU - 3861 The King’s Problem)