• hdu 1151 Air Raid


    Consider a town where all the streets are one-way and each street leads from one intersection to another. It is also known that starting from an intersection and walking through town's streets you can never reach the same intersection i.e. the town's streets form no cycles. 

    With these assumptions your task is to write a program that finds the minimum number of paratroopers that can descend on the town and visit all the intersections of this town in such a way that more than one paratrooper visits no intersection. Each paratrooper lands at an intersection and can visit other intersections following the town streets. There are no restrictions about the starting intersection for each paratrooper. 

    Input

    Your program should read sets of data. The first line of the input file contains the number of the data sets. Each data set specifies the structure of a town and has the format: 

    no_of_intersections 
    no_of_streets 
    S1 E1 
    S2 E2 
    ...... 
    Sno_of_streets Eno_of_streets 

    The first line of each data set contains a positive integer no_of_intersections (greater than 0 and less or equal to 120), which is the number of intersections in the town. The second line contains a positive integer no_of_streets, which is the number of streets in the town. The next no_of_streets lines, one for each street in the town, are randomly ordered and represent the town's streets. The line corresponding to street k (k <= no_of_streets) consists of two positive integers, separated by one blank: Sk (1 <= Sk <= no_of_intersections) - the number of the intersection that is the start of the street, and Ek (1 <= Ek <= no_of_intersections) - the number of the intersection that is the end of the street. Intersections are represented by integers from 1 to no_of_intersections. 

    There are no blank lines between consecutive sets of data. Input data are correct. 
    Output

    The result of the program is on standard output. For each input data set the program prints on a single line, starting from the beginning of the line, one integer: the minimum number of paratroopers required to visit all the intersections in the town. 
    Sample Input

    2
    4
    3
    3 4
    1 3
    2 3
    3
    3
    1 3
    1 2
    2 3

    Sample Output

    2
    1

      最小路径覆盖裸模型。答案 = 点数 - 最大匹配数。

    Code

      1 /**
      2  * hdu
      3  * Problem#1151
      4  * Accepted
      5  * Time:0ms
      6  * Memory:1680k
      7  */
      8 #include<iostream>
      9 #include<cstdio>
     10 #include<ctime>
     11 #include<cctype>
     12 #include<cstring>
     13 #include<cstdlib>
     14 #include<fstream>
     15 #include<sstream>
     16 #include<algorithm>
     17 #include<map>
     18 #include<set>
     19 #include<stack>
     20 #include<queue>
     21 #include<vector>
     22 #include<stack>
     23 #ifndef WIN32
     24 #define Auto "%lld"
     25 #else
     26 #define Auto "%I64d"
     27 #endif
     28 using namespace std;
     29 typedef bool boolean;
     30 const signed int inf = (signed)((1u << 31) - 1);
     31 const double eps = 1e-10;
     32 #define smin(a, b) a = min(a, b)
     33 #define smax(a, b) a = max(a, b)
     34 #define max3(a, b, c) max(a, max(b, c))
     35 #define min3(a, b, c) min(a, min(b, c))
     36 template<typename T>
     37 inline boolean readInteger(T& u){
     38     char x;
     39     int aFlag = 1;
     40     while(!isdigit((x = getchar())) && x != '-' && x != -1);
     41     if(x == -1) {
     42         ungetc(x, stdin);
     43         return false;
     44     }
     45     if(x == '-'){
     46         x = getchar();
     47         aFlag = -1;
     48     }
     49     for(u = x - '0'; isdigit((x = getchar())); u = (u << 1) + (u << 3) + x - '0');
     50     ungetc(x, stdin);
     51     u *= aFlag;
     52     return true;
     53 }
     54 
     55 ///map template starts
     56 typedef class Edge{
     57     public:
     58         int end;
     59         int next;
     60         Edge(const int end = 0, const int next = 0):end(end), next(next){}
     61 }Edge;
     62 
     63 typedef class MapManager{
     64     public:
     65         int ce;
     66         int *h;
     67         Edge *edge;
     68         MapManager(){}
     69         MapManager(int points, int limit):ce(0) {
     70             h = new int[(const int)(points + 1)];
     71             edge = new Edge[(const int)(limit + 1)];
     72             memset(h, 0, sizeof(int) * (points + 1));
     73         }
     74         inline void addEdge(int from, int end){
     75             edge[++ce] = Edge(end, h[from]);
     76             h[from] = ce;
     77         }
     78         inline void addDoubleEdge(int from, int end){
     79             addEdge(from, end);
     80             addEdge(end, from);
     81         }
     82         Edge& operator [] (int pos) {
     83             return edge[pos];
     84         }
     85         inline void clear() {
     86             delete[] h;
     87             delete[] edge;
     88         }
     89 }MapManager;
     90 #define m_begin(g, i) (g).h[(i)]
     91 ///map template ends
     92 
     93 int T;
     94 int n, m;
     95 MapManager g;
     96 
     97 inline void init() {
     98     readInteger(n);
     99     readInteger(m);
    100     g = MapManager(n, m);
    101     for(int i = 0, a, b; i < m; i++) {
    102         readInteger(a);
    103         readInteger(b);
    104         g.addEdge(a, b);
    105     }
    106 }
    107 
    108 int* match;
    109 boolean* vis;
    110 boolean dfs(int node) {
    111     for(int i = m_begin(g, node); i; i = g[i].next) {
    112         int &e = g[i].end;
    113         if(vis[e])    continue;
    114         vis[e] = true;
    115         if(match[e] == -1 || dfs(match[e])) {
    116             match[e] = node;
    117             return true;
    118         }
    119     }
    120     return false;
    121 }
    122 
    123 int res;
    124 inline void solve() {
    125     res = n;
    126     vis = new boolean[2 * n + 1];
    127     match = new int[2 * n + 1];
    128     memset(match, -1, sizeof(int) * (2 * n + 1));
    129     for(int i = 1; i <= n; i++) {
    130         memset(vis, false, sizeof(boolean) * (2 * n + 1));
    131         if(dfs(i))    res--;
    132     }
    133     printf("%d
    ", res);
    134     delete[] vis;
    135     delete[] match;
    136 }
    137 
    138 int main() {
    139     readInteger(T);
    140     while(T--) {
    141         init();
    142         solve();
    143     }
    144     return 0;
    145 }
  • 相关阅读:
    POJ 3630
    Codeforces Round #219 (Div. 2) D题
    Codeforces Round #232 (Div. 2) On Sum of Fractions
    Codeforces Round #232 (Div. 2) C
    撸呀撸的左手(KMP+DP)
    hdu poj KMP简单题目总结
    LCT总结
    bzoj1019 [SHOI2008]汉诺塔
    NOIP2016总结
    p1199八数码问题
  • 原文地址:https://www.cnblogs.com/yyf0309/p/7127630.html
Copyright © 2020-2023  润新知