HDU 3472 HS BDC (mixed Eulerian graph (with maximum flow)) template

Source: Internet
Author: User

HS BDCTime limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 980 Accepted Submission (s): 398


Problem Descriptionielts is around the corner! LOVE8909 has registered for the exam, but he still hasn ' t got prepared. Now he decides the actions. But if he takes out the New Oriental IELTS vocabulary, he finds there is so many words. But love8909 doesn ' t get scared, because he had got a special skill. If He can make a list with some meaningful words, he'll quickly remember these words and would not forget them. If the last letter of some word Wa was the same as the first letter of the some word Wb and then you can connect these the words a nd make a list of the words. If You can connect a word to a list, you'll make a longer list.

While love8909 was making the list, he finds that some words be still meaningful words if you reverse them. For example, if you reverse the word "Pat", you'll get another meaningful word "tap".

After scanning the vocabulary, love8909 have found there are N words, some of them is meaningful if reversed, while others is not. Now he wonders whether he can remember all these words using his special skill.

The N-word list must contain every word once and only once.

Inputan integer t (t <=) comes on the first line, indicating the number of test cases.

On the first line of all test cases is a integer n (n <=), telling you there be N words that love8909 want S to remember. Then comes N lines. Each of the following N lines have this format:word type. Word would be a string with only ' a ' ~ ' Z ', and type would be 0 (not meaningful when reversed) or 1 (meaningful when reversed). The length of each word was guaranteed to being less than 20.


outputthe format of the output is like "Case t:s", the number of the test cases, starting from 1, and S is a string.
If love8909 can remember all the words, S would be is "well done!", otherwise it ' s "Poor boy!"


Sample Input
36aloha 0arachnid 0dog 0gopher 0tar 1tiger 03thee 1earn 0nothing 02pat 1ACM 0

Sample Output
Case 1:well done! Case 2:well done! Case 3:poor boy! Hint In the first case, the word "tar" was still meaningful when reversed, and love8909 can make a list as "aloha-arachnid-dog-g Opher-rat-tiger ". In the second case, the word "thee" was still meaningful when reversed, and love8909 can make a list as "thee-earn-nothing" . In the third case, no lists can be created.

Authorallenlowesy
Source2010 acm-icpc multi-university Training Contest (4)--host by UESTC test instructions: give n strings, 0: A character that represents the first and last words cannot be swapped, 1: Indicates that you can swap. If the trailing character of a string is the same as the first character of another string, it can be joined together, asking if you can put all the strings into a string. Problem solving: If you consider a string character as the beginning U, the trailing character as the end point V, the pair with 0: is a forward edge, and for 1 is a bidirectional edge. This problem is a stroke, but divided into and without direction, so is mixed Eulerian graph (1) First, judge all occurrences of the point is not in a connected block. (2) The total number of vertices is 0 or exactly 2 for the singular vertex. (3) Build the diagram, with the maximum flow, whether full flow to determine whether it is possible.
#include <stdio.h> #include <string.h> #include <queue> #include <algorithm>using namespace std   ; #define Captype intconst int maxn = 100010;    Total number of points const int MAXM = 400010;    Total number of edges const int INF = 1<<30;struct edg{int to,next; Captype Cap,flow;}  Edg[maxm];int Eid,head[maxn];int GAP[MAXN];  The number of points for each distance (or can be considered a height) int DIS[MAXN];  The shortest distance from each point to the end Enode int CUR[MAXN];    Cur[u] indicates that from the U point can flow through the cur[u] number side int pre[maxn];void init () {eid=0; memset (head,-1,sizeof (Head));}    There are three parameters to the edge, 4 parameters void addedg (int u,int v,captype c,captype rc=0) without a forward edge {edg[eid].to=v; edg[eid].next=head[u]; Edg[eid].cap=c; edg[eid].flow=0;    head[u]=eid++; Edg[eid].to=u;    EDG[EID].NEXT=HEAD[V]; EDG[EID].CAP=RC; edg[eid].flow=0; head[v]=eid++;}    Captype maxflow_sap (int snode,int enode, int n) {//n is the total number of points including the source and sink points, this must be noted memset (Gap,0,sizeof (GAP));    memset (dis,0,sizeof (dis));    memcpy (cur,head,sizeof (head));    Pre[snode] =-1;    Gap[0]=n;  Captype ans=0;    Max Stream int U=snode; while (Dis[snoDe]<n) {//Judging from the snode point there is no flow to the next adjacent point if (u==enode) {//Find a way to increase the flow captype min=inf;            int inser;                for (int i=pre[u]; i!=-1; i=pre[edg[i^1].to])//To find the maximum amount of traffic Min if (min>edg[i].cap-edg[i].flow) {                Min=edg[i].cap-edg[i].flow;            Inser=i;                } for (int i=pre[u]; i!=-1; i=pre[edg[i^1].to]) {edg[i].flow+=min;  Edg[i^1].flow-=min;            Flow of the side that can be recycled} ans+=min;            u=edg[inser^1].to;        Continue  } bool flag = FALSE;        It is possible to determine whether an int v can flow toward the neighboring point from the U point;            for (int i=cur[u]; i!=-1; i=edg[i].next) {v=edg[i].to;                if (edg[i].cap-edg[i].flow>0 && dis[u]==dis[v]+1) {flag=true;                Cur[u]=pre[v]=i;            Break            }} if (flag) {u=v;        Continue }//If a flow adjacent point is not found above, then the distance from the starting point U (which can also be considered a height) is the minimum distance of the adjacent flow point +1 intmind= N;            for (int i=head[u]; i!=-1; i=edg[i].next) if (edg[i].cap-edg[i].flow>0 && mind>dis[edg[i].to]) {            MIND=DIS[EDG[I].TO];        Cur[u]=i;        } gap[dis[u]]--;  if (gap[dis[u]]==0) return ans; When Dis[u] The point of this distance is gone, it is impossible to find an augmented stream path from the source point//Because there is only one distance from the sink point to the current point, then from the source point to the sink point must pass the current point, but the current point is not able to find        Can flow to the point, then the inevitable flow of dis[u]=mind+1;//if a stream of adjacent points is found, the distance is the distance of the adjacent point +1, if not found, then the n+1 gap[dis[u]]++;  if (U!=snode) u=edg[pre[u]^1].to; Return an Edge} return ans;    int fath[maxn];int findfath (int x) {if (x!=fath[x]) Fath[x]=findfath (fath[x]); return fath[x];}    void linke (int x,int y) {x=findfath (x);    Y=findfath (y); Fath[x]=y;}    int main () {int t,_cas=0,n, p, in[50],out[50];    Char s[50];    scanf ("%d", &t);        while (t--) {scanf ("%d", &n);        Init ();        memset (In,0,sizeof (in));        Memset (out,0,sizeof (out));        for (int i=0; i<=26; i++) fath[i]=i; IntFlag = 1, root = 0;            for (int i=0; i<n; i++) {scanf ("%s%d", s,&p);            int u=s[0]-' a ';            int V=s[strlen (s) -1]-' a '; out[u]++;            in[v]++;            Root=u;    if (p==1) addedg (u,v,1);        The two-way building is a side linke (U,V) with a capacity of 1;        } root=findfath (root);        int cnt=0, u=-1,v=-1; for (int i=0; i<26; i++) if (in[i]| |            Out[i]) {if (Findfath (i)!=root) {flag=0; break;                } if ((In[i]+out[i]) &1) {cnt++;                if (u==-1) u=i;            else v=i;        }} if (cnt!=0&&cnt!=2) flag=0;        if (flag==0) {printf ("Case%d:poor boy!\n", ++_cas); continue;            } if (cnt==2) {out[u]++; in[v]++;  ADDEDG (u,v,1);        Constructed into a Euler ring, adding a bidirectional edge} int s=26, t = 27; for (int i=0; i<26; i++) {if (Out[i]>in[i]) addedg (S,i, (out[i]-in[i])            /2);        else if (Out[i]<in[i]) addedg (I,t, (In[i]-out[i])/2);        } MAXFLOW_SAP (S, T, t+1);            for (int i=head[s]; i!=-1; i=edg[i].next)//Determine full stream if (edg[i].cap>0 && edg[i].cap>edg[i].flow) { flag=0;        Break        } if (flag) printf ("Case%d:well done!\n", ++_cas);    else printf ("Case%d:poor boy!\n", ++_cas); }}


HDU 3472 HS BDC (mixed Eulerian graph (with maximum flow)) template

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.