您的位置:首页 > 其它

hdu3861The King’s Problem

2013-12-03 12:36 155 查看


The King’s Problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 1366    Accepted Submission(s): 507


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.

 

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

 

Source

2011 Multi-University
Training Contest 3 - Host by BIT

 

 题目大意:1每个城市当且仅当属于一个state。2相互可达的两个城市要求必须属于一个state。3同一个state的两个城市u,v至少存在一条路径从u 到 v或在从v到u且不经过其他的state。求最小state的个数。
解体过程:刚开始练习图的连通性,我就读此题目,怎么也看不懂,然后就做其他强连通类的题目,回首再度才发现一句关键的话without passing any city which belongs to other
state。因为图的二分匹配还没涉及到所有昨天看看匈牙利算法求二分匹配,估计到零点了就没在写,但思路已经很清晰了。
对原图缩点建DAG图,就是程序中的G2,然后用二分匹配求最大匹配(要明白DAG的最小路径覆盖和二分匹配的关系)。
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <stack>
#define DEBUG 10
using namespace std;
const int maxn = 5000+10;
//Accepted	3861	140MS	1536K	2751 B	G++	Achiberx
int gn, gm;
vector<int> G[maxn]; // for original graph.
vector<int> G2[maxn];// for DAG.
// for dfs().
int dfs_clock, scc_cnt, sccno[maxn];
int pre[maxn], lowlink[maxn];
stack<int> S;
// for hungary();
int from[maxn], tot;
bool use[maxn];

void init() {
for(int i = 0; i < maxn; i++) {
G[i].clear(); G2[i].clear();
}
}

void dfs(int u) {
pre[u] = lowlink[u] = ++dfs_clock;
S.push(u);
for(int i = 0; i < (int)G[u].size(); i++) {
int v = G[u][i];
if(!pre[v]) {
dfs(v);
lowlink[u] = min(lowlink[u], lowlink[v]);
} else if(!sccno[v]) {
lowlink[u] = min(lowlink[u], pre[v]);
}
}
if(pre[u] == lowlink[u]) {
scc_cnt++;
for(;;) {
int x = S.top(); S.pop();
sccno[x] = scc_cnt;
if(x == u) break;
}
}
}

#ifndef DEBUG
void mid_res() {
for(int i = 1; i <= gn; i++) {
printf("sccno[%d] = %d\n", i, sccno[i]);
}
}
#endif

void find_scc(int n) {
dfs_clock = scc_cnt = 0;
memset(pre, 0, sizeof(pre));
memset(sccno, 0, sizeof(sccno));
memset(lowlink, 0, sizeof(lowlink));
for(int i = 1; i <= gn; i++) {
if(!pre[i]) dfs(i);
}
}

void build_map() {
int u, v;
for(int i = 1; i <= gn; i++) {
for(int j = 0; j < (int)G[i].size(); j++) {
u = sccno[i]; v = sccno[G[i][j]];
if(u != v) {
G2[u].push_back(v);
// printf("%d -> %d\n", u, v);   // check if G2[] is correct.
}
}
}
}

bool match(int x) {
int v;
for(int i = 0; i < (int)G2[x].size(); i++) {
v = G2[x][i];
if(!use[v]) {
use[v] = true;
if(from[v] == -1 || match(from[v])) {
from[v] = x;
return true;
}
}
}
return false;
}

int hungary() {
tot = 0;
memset(from, -1, sizeof(from));
for(int i = 1; i <= scc_cnt; i++) {
memset(use, 0, sizeof(use));
if(match(i)) ++tot;
}
return tot;
}

int main()
{
int T, u, v;
scanf("%d", &T);
while(T--) {
scanf("%d%d", &gn, &gm);
init();
for(int i = 1; i <= gm; i++) {
scanf("%d%d", &u, &v);
G[u].push_back(v);
}
find_scc(gn);
// mid_res();
build_map();
int res = hungary();
printf("%d\n", scc_cnt-res);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: