Ural 1109,nyoj 239, Hungarian algorithm adjacency table

Source: Internet
Author: User

Nyoj 239:http://acm.nyist.net/judgeonline/problem.php?pid=239

Ural 1109:http://acm.timus.ru/problem.aspx?space=1&num=1109

Nyoj Matchmaker The problem, is the bare maximum match, very annoying is the adjacency array timeout. Use the adjacency table instead.

#include <bits/stdc++.h>using namespacestd;#defineMAXN 1005Vector<int>G[MAXN];BOOLUSE[MAXN];intMATCH[MAXN];intm,n,k;BOOLDfsintu) {     for(intI=0; I<g[u].size (); i++)    {        if(use[g[u][i]]==false) {Use[g[u][i]]=true; if(match[g[u][i]]==-1||DFS (Match[g[u][i])) {Match[g[u][i]]=u; return true; }        }    }    return false;}intMain () {scanf ("%d%d%d",&m,&n,&k);  for(intI=0; i<k;i++)    {        intu,v; scanf ("%d%d",&u,&v);    G[u].push_back (v); } memset (Match,-1,sizeof(match)); intAns =0;  for(intI=1; i<=m;i++) {memset (use,0,sizeof(use)); if(Dfs (i)) ans++; } printf ("%d\n", ans); //printf ("%d\n", M+n-ans);    return 0;}
View Code

Then the Ural, the smallest path overlay.

Test instructions

A country has m representatives, b state-owned n representatives, of which K can negotiate with delegates (one is a, one is State B), and each represents at least one pair (that is, everyone can at least find another person to negotiate), Each pair of negotiations requires a pair of phone calls (a pair of phone numbers of 1), which now makes it possible for everyone to make a minimum number of contacts for a phone call.

is to ask for the minimum logarithm. Each point must have an edge attached to it, so that the minimum number of edges-the minimum path coverage.

First ask for the maximum match (both are a pair), there may be no matching people, plus these people, such as case: Maximum match 2, there is no match on the left number 2nd. Add this man.

Formula:

Minimum path override = n+ M-2 * ans + ans;

#include <bits/stdc++.h>using namespacestd;#defineMAXN 1005Vector<int>G[MAXN];BOOLUSE[MAXN];intMATCH[MAXN];intm,n,k;BOOLDfsintu) {     for(intI=0; I<g[u].size (); i++)    {        if(use[g[u][i]]==false) {Use[g[u][i]]=true; if(match[g[u][i]]==-1||DFS (Match[g[u][i])) {Match[g[u][i]]=u; return true; }        }    }    return false;}intMain () {scanf ("%d%d%d",&m,&n,&k);  for(intI=0; i<k;i++)    {        intu,v; scanf ("%d%d",&u,&v);    G[u].push_back (v); } memset (Match,-1,sizeof(match)); intAns =0;  for(intI=1; i<=m;i++) {memset (use,0,sizeof(use)); if(Dfs (i)) ans++; } printf ("%d\n", m+n-ans); return 0;}
View Code

Ural 1109,nyoj 239, Hungarian algorithm adjacency table

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.