Minimum path overlay (binary graph Max match): Air Raid

Source: Internet
Author: User
Air Raid Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64D &%i64u Submit Status

Description consider a town where all the streets is one-way and each street leads from one intersection to another. It is also known this starting from a intersection and walking through town's streets you can never reach the same inters Ection i.e. the town ' s streets Form no cycles.

With these assumptions your task was to write a program this finds the minimum number of paratroopers that can descend on t He town and visit all the intersections of this town in such a-the-more than one paratrooper visits no intersection. Each of the paratrooper lands at a intersection and can visit other intersections following the town streets. There is 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 have the format:

No_of_intersections
No_of_streets
S1 E1
S2 E2
......
Sno_of_streets eno_of_streets

The first line of all 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, and which is the number of streets in the town. The next no_of_streets lines, one for each street in the town, is randomly ordered and represent the town ' s streets. The line corresponding to Street K (k <= no_of_streets) consists of a 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's the end of the street. Intersections is represented by integers from 1 to no_of_intersections.

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

Output the result of the program was on standard output. For each input data set the program prints on a, starting from the beginning of the line, one integer:the min Imum 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

Template problem, to find the minimum path coverage

Use formula: Minimum path override required number of paths = number of vertices-maximum number of matches

The maximum number of matches is implemented by DFS algorithm in Hungary

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <

Cmath> using namespace std;
const int maxn=1000;  int un,vn;
u,v number int g[maxn][maxn];//number is 0~n-1 int LINKER[MAXN];
BOOL USED[MAXN];
    bool Dfs (int u) {int V;
            for (v=0;v<=vn;v++) if (G[u][v]&&!used[v]) {used[v]=true; if (linker[v]==-1| |
                DFS (Linker[v])) {linker[v]=u;
            return true;
}} return false;
    } int Hungary () {int res=0;
    int u;
    memset (LINKER,-1,SIZEOF (linker));
        for (u=0;u<=un;u++) {memset (used,0,sizeof (used));
    if (DFS (U)) res++;
} return res;
    } int main () {int n;
    int Inum, snum;
    int i;
    scanf ("%d", &n);
        while (n-) {memset (g, 0, sizeof (g));
        scanf ("%d%d", &inum, &snum);
        UN = 0;
        VN = 0;
            for (i = 0; i < snum; i + +) {int A; int b;
            scanf ("%d%d", &a, &b);     A--;
            Note the serial number B--;
            if (a > un) un = A;
            if (b > vn) vn = b;
        G[A][B] = 1;
        } int ans = inum-hungary ();
    printf ("%d\n", ans);
} return 0;
 }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.