3 6 aloha 0 arachnid 0 dog 0 gopher 0 tar 1 tiger 0 3 thee 1 earn 0 nothing 0 2 pat 1 acm 0
Case 1: Well done! Case 2: Well done! Case 3: Poor boy!HintIn the first case, the word “tar” is still meaningful when reversed, and love8909 can make a list as “aloha-arachnid-dog-gopher-rat-tiger”. In the second case, the word “thee” is still meaningful when reversed, and love8909 can make a list as “thee-earn-nothing”. In the third case, no lists can be created.
#include<stdio.h> #include<string.h> #include<queue> #include<algorithm> using namespace std; #define captype int const int MAXN = 100010; //点的总数 const int MAXM = 400010; //边的总数 const int INF = 1<<30; struct EDG{ int to,next; captype cap,flow; } edg[MAXM]; int eid,head[MAXN]; int gap[MAXN]; //每种距离(或可认为是高度)点的个数 int dis[MAXN]; //每个点到终点eNode 的最短距离 int cur[MAXN]; //cur[u] 表示从u点出发可流经 cur[u] 号边 int pre[MAXN]; void init(){ eid=0; memset(head,-1,sizeof(head)); } //有向边 三个参数,无向边4个参数 void addEdg(int u,int v,captype c,captype rc=0){ 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是包括源点和汇点的总点个数,这个一定要注意 memset(gap,0,sizeof(gap)); memset(dis,0,sizeof(dis)); memcpy(cur,head,sizeof(head)); pre[sNode] = -1; gap[0]=n; captype ans=0; //最大流 int u=sNode; while(dis[sNode]<n){ //判断从sNode点有没有流向下一个相邻的点 if(u==eNode){ //找到一条可增流的路 captype Min=INF ; int inser; for(int i=pre[u]; i!=-1; i=pre[edg[i^1].to]) //从这条可增流的路找到最多可增的流量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; //可回流的边的流量 } ans+=Min; u=edg[inser^1].to; continue; } bool flag = false; //判断能否从u点出发可往相邻点流 int v; 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; } //如果上面没有找到一个可流的相邻点,则改变出发点u的距离(也可认为是高度)为相邻可流点的最小距离+1 int Mind= 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; //当dis[u]这种距离的点没有了,也就不可能从源点出发找到一条增广流路径 //因为汇点到当前点的距离只有一种,那么从源点到汇点必然经过当前点,然而当前点又没能找到可流向的点,那么必然断流 dis[u]=Mind+1;//如果找到一个可流的相邻点,则距离为相邻点距离+1,如果找不到,则为n+1 gap[dis[u]]++; if(u!=sNode) u=edg[pre[u]^1].to; //退一条边 } 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; int flag = 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); //双向的则建一条容量为1的边 linke(u,v); } 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); //构造成欧拉环,添加的是双向边 } 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)//判断满流 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(混合欧拉图(使用最大流))模板
原文地址:http://blog.csdn.net/u010372095/article/details/46584951