zoukankan      html  css  js  c++  java
  • hdu3861他的子问题是poj2762二分匹配+Tarjan+有向图拆点 其实就是求DAG的最小覆盖点

    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 = 20010;
    const int MAXM = 100010;
    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 = 0;
        memset(head, -1, 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 != -1; 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, 0, sizeof(Low));
        memset(DFN, 0, sizeof(DFN));
        memset(num, 0, sizeof(num));
        memset(Stack, 0, sizeof(Stack));
        memset(Instack, false, sizeof(Instack));
        Index = scc = top = 0;
        for (int i = 1; i <= n; i++) 
            if (!DFN[i])
                Tarjan(i);
    }
    vector<int> g[MAXN];
    int linker[MAXN], used[MAXN];
    bool dfs(int u) {
        for (int i = 0; i < g[u].size(); i++) {
            int v = g[u][i]; 
            if (!used[v]) {
                used[v] = 1; 
                if (linker[v] == -1 || dfs(linker[v])) {
                    linker[v] = u; 
                    return true;
                }
            } 
        }
        return false;
    }
    int hungary() {
        int res = 0;
        memset(linker, -1, sizeof(linker));
        for (int i = 1; i <= scc; i++) {
            memset(used, 0, 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 = 0; i < m; i++) {
                scanf("%d%d", &u, &v); 
                addedge(u, v); 
            }
            solve(); 
            for (int i = 0; i <= scc; i++) g[i].clear();
            for (int u = 1; u <= n; u++) {
                for (int i = head[u]; i != -1; i = edge[i].next) {
                    int v = edge[i].to;
                    if (Belong[u] != Belong[v]) 
                        g[Belong[u]].push_back(Belong[v]);
                }  
            }
            printf("%d
    ", hungary()); 
        }
        return 0;
    }
     
  • 相关阅读:
    中国黑客传说:游走在黑暗中的精灵
    智能硬件安全入门
    迈克菲:2016年的八大网络安全威胁
    走进科学之WAF(Web Appllication Firewall)篇
    从对SAE的一次授权安全评估浅谈云安全
    沟通的艺术,心理学与生活,学会提问
    知道创宇研发技能表v3.0
    SYN Cookie的原理和实现
    1043. 输出PATest(20)
    1042. 字符统计(20)
  • 原文地址:https://www.cnblogs.com/mfys/p/7260886.html
Copyright © 2011-2022 走看看