"Getting Started" Hungarian algorithm +hnoi2006 hero Super Heroes

Source: Internet
Author: User

First, about the Hungarian algorithm

Hungarian algorithm is proposed by the Hungarian mathematician Edmonds, using the augmented path to find the maximum matching algorithm of the binary graph.

Sounds high-end, in fact, plainly is:

Suppose there is no unrequited love (single dog Secretly Tears), in a country where homosexuality is not legal (there is no discrimination # Orthophoto), there are some men and women, they have some relationships with each other. The Hungarian algorithm is meant to facilitate as many couples as possible.


Such as:

The green callout is a maximum binary match for this graph.

First mention a noun as follows:
Augmented path: If P is a path in Figure G that connects two unmatched vertices, and an edge that belongs to M and an edge that does not belong to M (that is, matched and to be matched) is alternately present on p, then p is an augmented route relative to M.

The following is a talk about the Hungarian algorithm idea:
1. Starting from each vertex of a part, looking for an augmented road;

2, traverse all the edges at the beginning of the current point, if a augmented path is found, update the match number and return 1; otherwise repeat two until all edges are traversed;

3, if the current point as the starting point for all the side is traversed still did not find an extensible path, return 1.

Obviously, the Hungarian algorithm is a DFS-based algorithm because the process of finding the augmented path requires deep searching. The idea is very simple, so I do not paste the pseudo-code is not because lazy and also can not write pseudo-code (crossed off), anyway, hero is a naked problem, the solution also has a template.

Second, HNOI2006 Hero Super Heroes

Title Description:

Now the TV station has a kind of program called superhero, the approximate process is each player to the stage to answer the host several questions, and then according to answer the question how much to obtain a different number of prizes or bonuses. Moderator questions prepared a number of topics, only when the player correctly answer a question, can enter the next question, or be eliminated. In order to increase the interest of the program and to reduce the difficulty, the host always provide players with several "tricks", such as to help the audience, or remove a number of wrong answers (choice) and so on. Here, let's change the rules a little bit. Assuming that the host has a total of M-questions, the contestants have n different "tricks". The moderator stipulates that each problem can be selected from two "tricks", and each "Ace" can only be used once. We also assume that a problem with the use of it allows the ACE, it will be able to correctly answer, smoothly into the next question. Now I came to the show, but I was too stupid, so that a problem will not do, each problem had to use the "Ace" to pass. If I knew in advance which two "tricks" can be used for each question, then can you tell me how to choose the maximum number of questions?

Input:

The input file is a line of two positive integers n and m (0 < n <1001,0 < M < 1001) indicating a total of n "Ace", numbered 0~n-1, with a total of M questions.
The following M-line, two numbers per line, indicates the number of "Ace" that can be used for question M.
Note that each number of "Ace" can only be used once, the same problem of two "tricks" may be the same.

Output:

The number of questions that the first behavior can pass is p.

Sample input:

5 6
3 2
2 0
0 3
0 4
3 2
3 2

Sample output:

4

Data range:

0 < N, m < 1001

Absolutely naked question, naked let me suspect this is I saw on the Bzoj. Attached link →_→ http://www.lydsy.com/JudgeOnline/problem.php?id=1191

The code is as follows:

1#include <stdio.h>2#include <string.h>3 structnode4 {5     intv;6     intNext;7 };8Node e[3010];9 intst[3010],cnt;Ten intn,m; One BOOLvis[3010];//determine if a path has been traversed in a search A intlink[3010];//determine whether the left and right endpoints are connected Link[i]=j represents the brocade sac I is connected with the problem J, in particular, when link[i]=0 indicates that I is an open point - voidBuildintAintb) - { thee[++cnt].v=b; -e[cnt].next=St[a]; -st[a]=CNT; -}//map, problem connected to the brocade sac + BOOLDfsintx) - { +     inti; A      for(i=st[x];i!=0; i=e[i].next)//enumerate the edges that are currently starting at the left end at     { -         if(Vis[i])Continue;//If the enumerated edge has been accessed, skip this edge -vis[i]=true; -         inttemp=e[i].v; -         if(!link[temp]| | DFS (Link[temp]))//If temp is an open point or a problem that corresponds to temp starts with a scalable path, change the problem of temp to X and think there is an augmented path starting with x -         { inlink[temp]=x; -             return true; to         } +     } -     return false; the } * intMain () $ {Panax Notoginsengscanf"%d%d",&n,&m); -     inta,b,i,ans=0; the      for(i=1; i<=m;i++) +     { Ascanf"%d%d",&a,&b); the build (i,a); + build (i,b); -}//map, problem connected to the brocade sac $      for(i=1; i<=m;i++) $     { -memset (Vis,false,sizeof(VIS));  -         if(Dfs (i)) ans++; the         Else       Break;//This sentence must be added! Be sure to add! Be sure to add! Say three times ←__← at first did not add unlimited WA, and later realized that this is a level game, a close without the end of the game, will not be done in the future -     }Wuyiprintf"%d", ans); the     return 0; -}
HNOI2006 Hero Super hero

"Getting Started" Hungarian algorithm +hnoi2006 hero Super Heroes

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.