关闭

hdu3861The King’s Problem

745人阅读 评论(0) 收藏 举报
分类:

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
 

 题目大意: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;
}
0
0

猜你在找
【直播】机器学习&数据挖掘7周实训--韦玮
【套餐】系统集成项目管理工程师顺利通关--徐朋
【直播】3小时掌握Docker最佳实战-徐西宁
【套餐】机器学习系列套餐(算法+实战)--唐宇迪
【直播】计算机视觉原理及实战--屈教授
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之矩阵--黄博士
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之凸优化--马博士
【套餐】Javascript 设计模式实战--曾亮
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:384054次
    • 积分:8363
    • 等级:
    • 排名:第2328名
    • 原创:434篇
    • 转载:80篇
    • 译文:13篇
    • 评论:71条
    记录成长的足迹
    编程是件有趣的事情,因为热爱所以专一!
    最新评论