Binary graph matching (Hungarian algorithm) URAL 1721 two Sides of the same Coin

Source: Internet
Author: User

Topic Portal

1 /*2 Test instructions: Three kinds of people, statements,testdata,anthing. Requires two people to complete s and t two jobs, and rank differs by 23 binary map Matching: This problem learning to build skills, two sets and internal must not be connected, rank modulus 4 is less than 2 and greater than or equal to 2 people can match, and the difference is exactly 2,4 Two people will not be the same. The above conditions to delete two sets, can not be divided according to Anthing. 5 */6#include <cstdio>7#include <iostream>8#include <algorithm>9#include <cstring>Ten#include <vector> One#include <string> A#include <cmath> -#include <map> - using namespacestd; the  - Const intMAXN = 1e3 +Ten; - Const intINF =0x3f3f3f3f; - structSetter + { -     Charname[ A], can[ One]; +     intRK; A     BOOL operator< (ConstSetter &r)Const at     { -         returnRK <R.rk; -     } - }P[MAXN]; - intSVIS[MAXN], VIS[MAXN]; - intMP[MAXN]; in intX[MAXN], Y[MAXN]; -vector<int>G[MAXN]; to intN, un, vn; +  - BOOLDFS (intu) the { *      for(intI=0; I<g[u].size (); ++i) $     {Panax Notoginseng         intv =G[u][i]; -         if(!Vis[v]) the         { +VIS[V] =true; A             if(Y[v] = =-1||DFS (Y[v])) the             { +Y[V] = u; X[u] = v;return true; -             } $         } $     } -  -     return false; the } - Wuyi voidHungaryvoid) the { -Sort (p+1, p+1+N); memset (Svis,0,sizeof(Svis)); WuUN =0; -      for(intI=1; i<=n; ++i) About     { $         if(Svis[i]! =0|| P[i].rk%4>1)Continue; -Svis[i] =-1; Mp[++un] =i; -          for(intj=1; j<=n; ++j) -         { A             if(Svis[j] = =-1|| P[j].rk%4<=1)Continue; +             if(ABS (P[I].RK-P[J].RK)! =2)Continue; the             if(p[i].can[0] = = p[j].can[0] && p[i].can[0] !='a')Continue; -SVIS[J] =1; G[un].push_back (j); $         } the     } the  the     intres =0; memset (x,-1,sizeof(x)); memset (Y,-1,sizeof(y)); the      for(intI=1; i<=un; ++i) -     { inmemset (Vis,false,sizeof(Vis)); the         if(DFS (i)) res++; the     } About      theprintf ("%d\n", res); the      for(intI=1; i<=un; ++i) the     { +         if(X[i] = =-1)Continue; -         intID1 = Mp[i], Id2 =X[i]; the         if(p[id1].can[0] =='T'|| p[id2].can[0] =='s') Swap (ID1, ID2);Bayiprintf ("%s%s\n", P[id1].name, p[id2].name); the     } the } -  - intMainvoid)//URAL 1721, Sides of the same Coin the { the     //freopen ("k.in", "R", stdin); the  thescanf ("%d", &n); -      for(intI=1; i<=n; ++i) the     { thescanf ("%s%s%d", P[i].name, P[i].can, &p[i].rk); the     }94  the Hungary (); the      the     return 0;98}

Binary graph matching (Hungarian algorithm) URAL 1721 two Sides of the same Coin

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.