HDU 1151 Air Raid (minimum path overlay)

Source: Internet
Author: User

Main topic:there are n cities, M roads, and the roads of the city are unidirectional. now our paratroopers are going to land in the city, and the paratroopers of our gate are going to search all the roads. Ask us how many cities we occupy at least to search for all the roads. we can go along the road and reach another city.  Topic Analysis:the problem is actually finding the minimum path coverageminimum path coverage problem: Overwrites all vertices of a direction-free graph with as few disjoint simple paths as possible. Divide each vertex into two, respectively, in the X and Y sets, if there is a forward edge (a, A, a, b), corresponding to the binary graph (XA,YB)minimum number of paths= number of nodes-Maximum matchSimple Explanation:There is a correspondence between the path overlay of the original image and the matching of the new graph:Each overlay edge is an edge in the match, and only the last point of the pathThe path requirements cannot intersect, exactly the two matching edges in the match cannot have a public endpoint. Therefore, after the maximum match, the point cannot be matched, that is, the last point of the path. There are many points that cannot be matched, and how many paths exist. Number of paths = Original points-match the number of edges. So we make the maximum number of matching edges, that is, to minimize the number of paths.
#include <stdio.h>#include<string.h>#include<algorithm>#include<iostream>#include<vector>#include<queue>#include<cmath>using namespacestd;#defineINF 0X3FFFFFFF#defineMAXN 1705intN, P[maxn], Head[maxn], M, K;BOOLVIS[MAXN];structedgenode{inte, Next;} EDGE[MAXN*5];voidAddedge (intSinte) {Edge[k].next=Head[s]; EDGE[K].E=e; Head[s]= k + +;}voidDFS2 (intu) {}BOOLFind (intu) {     for(intI=head[u]; i!=-1; I=Edge[i].next) {        intv =EDGE[I].E; if(!Vis[v]) {Vis[v]=true; if(P[v] = =-1||Find (P[v])) {P[v]=u; return true; }        }    }    return false;}intsolve () {intAns =0; memset (P,-1,sizeof(P));  for(intI=1; i<=n; i++) {memset (Vis,false,sizeof(VIS)); if(Find (i)) ans++; }    returnN-ans;}intMain () {intT; scanf ("%d", &T);  while(t--) {scanf ("%d%d", &n, &m); intA, B; K=0; memset (Head,-1,sizeof(Head));  for(intI=0; i<m; i++) {scanf ("%d%d", &a, &b);        Addedge (A, b); } printf ("%d\n", Solve ()); }    return 0;}

HDU 1151 Air Raid (minimum path overlay)

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.