Hdu5090--game with pearls (binary graph matching)

Source: Internet
Author: User
Tags cmath

Test instructions: give n a pipe, each tube has a certain number of pearls, now Jerry began to put some pearls on the pipe, put on the number of pearls must be a multiple of k, can not put. Finally, the pipe is sorted, if I can do the first pipe above the I have a pearl, then Jerry wins, and Tom wins.

Idea: The data is relatively small, so I am water, simulation process + greedy can pass. But the positive solution is a binary graph match, which has not been touched before.

Binary graph matching: Explain the second set of examples, k = 2;

From left to right, each can match I (a), I remove the other side around I, and then also remove a can point to the edge, calculate a total of several can match, if it is N, then Jerry wins, or Tom wins;

1 ///water over the code2#include <iostream>3#include <cmath>4#include <cstdio>5#include <cstring>6#include <cstdlib>7#include <string>8#include <sstream>9#include <algorithm>Ten #defineMax 2147483647 One #defineINF 0x7fffffff A #defineN 90010 - #definell Long Long - #defineMem (A, B) memset (A,b,sizeof (a)) the #defineRepu (I, A, b) for (int i = (a); I < (b); i++) - Const DoublePi=-acos (-1.0); - using namespacestd; - intA[n]; + intMain () - { +     intn,t,k; ACin>>T; at      while(t--) -     { -Cin>>n>>K; -Repu (I,1,1+N) -Cin>>A[i]; -Sort (A +1, A +1+n); in         intOK =-1, i=1; -          while(i<=N) to         { +             if(a[i]>i) -             { theOK =1; *                  Break; $             }Panax Notoginseng             Else if(a[i]<i) -             { thea[i]+=K; +Sort (A +1, A +1+n); A                 Continue; the             } +             Else -i++; $         } $         if(ok==1) -cout<<"tom\n"; -         Else thecout<<"jerry\n"; -     }Wuyi     return 0; the}
View Code

Binary Graph matching Code:

1#include <iostream>2#include <cmath>3#include <cstdio>4#include <cstring>5#include <cstdlib>6#include <string>7#include <sstream>8#include <algorithm>9 #defineMax 2147483647Ten #defineINF 0x7fffffff One #defineN 901 A #definell Long Long - #defineMem (A, B) memset (A,b,sizeof (a)) - #defineRepu (I, A, b) for (int i = (a); I < (b); i++) the using namespacestd; - intG[n][n],vis[n],march[n]; - intN; - intDfsintu) + { -      for(intv =1; V <= N; v++)///consider all Yi vertex v +     { A         if(G[u][v]&&!vis[v])///v is contiguous with u and has not been accessed at         { -VIS[V] =1;///if v does not match, or V is already matched, but from march[v] you can find an augmented path -             if(March[v] = =-1|| DFS (March[v]))///Note If the previous condition is true, it will not be called recursively -             { -MARCH[V] = u;///match v to u -                 return 1;///find an augmented path in             } -         } to     } +     return 0;///If there is no augmented path starting from U - } the  * voidMaxmatch ()///Hungarian algorithm for finding the maximum matching of two-part graphs $ {Panax Notoginseng     intres =0;///The maximum match is obtained -      for(inti =1; I <= N; i++) the     { +memset (Vis,0,sizeof(Vis)); ARes + = DFS (i);///each finding of an augmented path can add 1 to the match the     } +     if(res==N) -printf"jerry\n"); $     Else $printf"tom\n"); - } -  the intMain () - {Wuyi     intT,k,a; theCin>>T; -      while(t--) Wu     { -Memset (G,0,sizeof(g)); Aboutmemset (march,-1,sizeof(March)); $Cin>>n>>K; -Memset (G,0,sizeof(g)); -Repu (I,1,1+N) -         { ACin>>A; +              while(A <=N) the             { -G[a][i] =1;///bidirectional graphs $A + =K; the             } the         } the Maxmatch (); the     } -     return 0; in}
View Code

Hdu5090--game with pearls (binary graph matching)

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.