The King’s Problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2137 Accepted Submission(s): 763
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
2、一个州内的两点至少要有一方能到达还有一方。3、一个点仅仅能划分到一个州内。问他至少要建多少州
思路:先把能相互两两到达的点用强连通归为一个州,然后再进行缩点。建立新图。然后用匈牙利算法求出最大匹配,答案=强连通求出的联通块-最大匹配(最小路径覆盖=结点数-最大匹配)。
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <queue>
#define maxn 50000+100
#define maxm 200000+100
using namespace std;
int n, m;
struct node {
int u, v, next;
};
node edge[maxm];
int head[maxn], cnt;
int low[maxn], dfn[maxn];
int dfs_clock;
int Stack[maxn], top;
bool Instack[maxn];
int Belong[maxn];
int scc_clock;
vector<int>Map[maxn];
void init(){
cnt = 0;
memset(head, -1, sizeof(head));
}
void addedge(int u, int v){
edge[cnt] = {u, v, head[u]};
head[u] = cnt++;
}
void getmap(){
scanf("%d%d", &n, &m);
while(m--){
int a, b;
scanf("%d%d", &a, &b);
addedge(a, b);
}
}
void Tarjan(int u, int per){
int v;
low[u] = dfn[u] = ++dfs_clock;
Stack[top++] = u;
Instack[u] = true;
for(int i = head[u]; i != -1; i = edge[i].next){
int v = edge[i].v;
if(!dfn[v]){
Tarjan(v, u);
low[u] = min(low[u], low[v]);
}
else if(Instack[v])
low[u] = min(low[u], dfn[v]);
}
if(dfn[u] == low[u]){
scc_clock++;
do{
v = Stack[--top];
Instack[v] = false;
Belong[v] = scc_clock;
}
while( v != u);
}
}
void find(){
memset(low, 0, sizeof(low));
memset(dfn, 0, sizeof(dfn));
memset(Belong, 0, sizeof(Belong));
memset(Stack, 0, sizeof(Stack));
memset(Instack, false, sizeof(false));
dfs_clock = scc_clock = top = 0;
for(int i = 1; i <= n ; ++i){
if(!dfn[i])
Tarjan(i, i);
}
}
void suodian(){
for(int i = 1; i <= scc_clock; ++i)
Map[i].clear();
for(int i = 0; i < cnt; ++i){
int u = Belong[edge[i].u];
int v = Belong[edge[i].v];
if(u != v){
Map[u].push_back(v);
}
}
}
int used[maxn], link[maxn];
bool dfs(int x){
for(int i = 0; i < Map[x].size(); ++i){
int y = Map[x][i];
if(!used[y]){
used[y] = 1;
if(link[y] == -1 || dfs(link[y])){
link[y] = x;
return true;
}
}
}
return false;
}
void hungary(){
int ans = 0;
memset(link, -1, sizeof(link));
for(int j = 1; j <= scc_clock; ++j){
memset(used, 0, sizeof(used));
if(dfs(j))
ans++;
}
printf("%d\n", scc_clock - ans);
}
int main (){
int T;
scanf("%d", &T);
while(T--){
init();
getmap();
find();
suodian();
hungary();
}
return 0;
}