The King’s Problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3120    Accepted Submission(s): 1096

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
 
 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
const int MAXN = ;
const int MAXM = ;
struct Edge{
    int to, next;
}edge[MAXM];
int head[MAXN], tot;
int Low[MAXN], DFN[MAXN], Stack[MAXN], Belong[MAXN];
int Index, top;
int scc;
bool Instack[MAXN];
int num[MAXN];
int n, m;
void init() {
    tot = ;
    memset(head, -, sizeof(head));
}
void addedge(int u, int v) {
    edge[tot].to = v;
    edge[tot].next = head[u];
    head[u] = tot++;
}
void Tarjan(int u) {
    int v;
    Low[u] = DFN[u] = ++Index;
    Stack[top++] = u;
    Instack[u] = true;
    for (int i = head[u]; i != -; i = edge[i].next) {
        v = edge[i].to;        
        if (!DFN[v]) {
            Tarjan(v);
            if (Low[u] > Low[v]) Low[u] = Low[v];
        }
        else if (Instack[v] && Low[u] > DFN[v])
            Low[u] = DFN[v];
    }
    if (Low[u] == DFN[u]) {
        scc++;
        do {
            v = Stack[--top];
            Instack[v] = false;
            Belong[v] = scc;
            num[scc]++;
        } while (v != u);
    }
}
void solve() {
    memset(Low, , sizeof(Low));
    memset(DFN, , sizeof(DFN));
    memset(num, , sizeof(num));
    memset(Stack, , sizeof(Stack));
    memset(Instack, false, sizeof(Instack));
    Index = scc = top = ;
    for (int i = ; i <= n; i++)
        if (!DFN[i])
            Tarjan(i);
}
vector<int> g[MAXN];
int linker[MAXN], used[MAXN];
bool dfs(int u) {
    for (int i = ; i < g[u].size(); i++) {
        int v = g[u][i];
        if (!used[v]) {
            used[v] = ;
            if (linker[v] == - || dfs(linker[v])) {
                linker[v] = u;
                return true;
            }
        }
    }
    return false;
}
int hungary() {
    int res = ;
    memset(linker, -, sizeof(linker));
    for (int i = ; i <= scc; i++) {
        memset(used, , sizeof(used));
        if (dfs(i)) res++;
    }
    return scc - res;
}
int main() {
    int cas;
    scanf("%d", &cas);
    while (cas--) {
        scanf("%d%d", &n, &m);        
        init();
        int u, v;
        for (int i = ; i < m; i++) {
            scanf("%d%d", &u, &v);
            addedge(u, v);
        }
        solve();
        for (int i = ; i <= scc; i++) g[i].clear();
        for (int u = ; u <= n; u++) {
            for (int i = head[u]; i != -; i = edge[i].next) {
                int v = edge[i].to;
                if (Belong[u] != Belong[v])
                    g[Belong[u]].push_back(Belong[v]);
            } 
        }
        printf("%d\n", hungary());
    }
    return ;
}
 

hdu3861他的子问题是poj2762二分匹配+Tarjan+有向图拆点 其实就是求DAG的最小覆盖点的更多相关文章

  1. HDU 1068 Girls and Boys 二分图最大独立集(最大二分匹配)

    Girls and Boys Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  2. hdu 1281棋盘游戏(二分匹配)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1281   Problem Description 小希和Gardon在玩一个游戏:对一个N*M的棋盘, ...

  3. hdu_5727_Necklace(二分匹配)

    题目连接:hdu_5727_Necklace 题意: 有2*n个珠子,n个阳珠子,n个阴珠子,现在要将这2n个珠子做成一个项链,珠子只能阴阳交替排,有些阳珠子周围如果放了指定的阴珠子就会变坏,给你一个 ...

  4. HDU 6178 Monkeys(树上的二分匹配)

    http://acm.hdu.edu.cn/showproblem.php?pid=6178 题意:现在有一n个顶点的树形图,还有k只猴子,每个顶点只能容纳一只猴子,而且每只猴子至少和另外一只猴子通过 ...

  5. hdu 2444 The Accomodation of Students 判断二分图+二分匹配

    The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  6. hdu 1281 棋盘游戏(二分匹配)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1281 棋盘游戏 Time Limit: 2000/1000 MS (Java/Others)    M ...

  7. LA 2038 Strategic game(最小点覆盖,树形dp,二分匹配)

    题意即求一个最小顶点覆盖. 对于没有孤立点的图G=(V,E),最大独立集+最小顶点覆盖= V.(往最大独立集加点) 问题可以变成求树上的最大独立集合. 每个结点的选择和其父节点选不选有关, dp(u, ...

  8. POJ 1274 The Perfect Stall、HDU 2063 过山车(最大流做二分匹配)

    The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24081   Accepted: 106 ...

  9. [kuangbin带你飞]专题十 匹配问题 二分匹配部分

    刚回到家 开了二分匹配专题 手握xyl模板 奋力写写写 终于写完了一群模板题 A hdu1045 对这个图进行 行列的重写 给每个位置赋予新的行列 使不能相互打到的位置 拥有不同的行与列 然后左行右列 ...

随机推荐

  1. Spring Cloud sleuth with zipkin over RabbitMQ教程

    文章目录 Spring Cloud sleuth with zipkin over RabbitMQ demo zipkin server的搭建(基于mysql和rabbitMQ) 客户端环境的依赖 ...

  2. mysql之浅谈主外键

    主键(PRIMARY KEY) 主键在一个数据表中只能有唯一的一个,约束当前字段的值不能重复,且非空保证数据的完整性,也可以当做当前数据表的标识符用来查询(当做索引,唯一性索引的一种) 创建带主键的表 ...

  3. 原生JS中获取位置的方案总结

    获取鼠标当前位置 clientY.clientX: 鼠标当前位置 相对于 浏览器可视区域顶部.浏览器可视区域左部 的位置: pageY.pageX: 鼠标当前位置 相对于 文档顶部.文档左部的位置: ...

  4. IBM:向所有云平台开放Watson人工智能系统

    据美国科技媒体TechCrunch报道,IBM今天宣布不再把沃森(Watson)品牌的人工智能服务局限于自家云计算服务,而是会允许其他企业在自己的数据中心里使用和运行这个平台."客户很难把人 ...

  5. CF思维联系–CodeForces - 222 C Reducing Fractions(数学+有技巧的枚举)

    ACM思维题训练集合 To confuse the opponents, the Galactic Empire represents fractions in an unusual format. ...

  6. 图论--最小生成树--Prim算法(带边输出)模板

    #include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const int maxn = 100 ...

  7. HTML(表单标签)

    <form> 标签 用于为用户输入创建 HTML 表单 表单能够包含 input 元素,比如:文本字段.复选框.单选框.提交按钮等等 表单用于向服务器传输数据 action 属性:规定当提 ...

  8. 蓝色展开收缩悬浮QQ客服代码

    放在我的博客首页上的的预览图: 在文章区的预览图如下: 代码如下: <div class="scrollsidebar" id="scrollsidebar&quo ...

  9. IP 基础知识全家桶,45 张图一套带走

    前言 前段时间,有读者希望我写一篇关于 IP 分类地址.子网划分等的文章,他反馈常常混淆,摸不着头脑. 那么,说来就来!而且要盘就盘全一点,顺便挑战下小林的图解功力,所以就来个 IP 基础知识全家桶. ...

  10. JavaScript 循环判断练习题

    JavaScript 循环判断练习题 小明有一组水果("苹果","梨子","香蕉","葡萄","西瓜" ...