HDU 3472 混合图欧拉回路

题意:给你n个字符串,如果一个字符串的尾字符和另一个字符串的首字符相同,则两个字符串可以相连,某些字符串可以翻转,现在问你是否所有串可以变成一条链,每个字符串必须用且只能用一次。


分析:每个字符串可以看成一条边,假设首字母a,尾字母b,则有一条有向边<a,b>,如果可以翻转,则是无向边<a,b>,<b,a>,题目的要求也就是经过所有的边一次,直接混合图欧拉回路。


代码:

#pragma comment(linker,"/STACK:102400000,102400000")
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <string>
#include <math.h>
#include <queue>
#include <stack>
#include <map>
#include <set>
using namespace std;
typedef long long ll;
const int maxn=505;
const int maxm=1000005;
const int INF=1000000000;
struct EdgeNode
{
    int from;
    int to;
    int cost;
    int next;
}edge[maxm];
int head[maxn],cnt;
void add(int x,int y,int z)
{
    edge[cnt].from=x;edge[cnt].to=y;edge[cnt].cost=z;edge[cnt].next=head[x];head[x]=cnt++;
    edge[cnt].from=y;edge[cnt].to=x;edge[cnt].cost=0;edge[cnt].next=head[y];head[y]=cnt++;
}

void init()
{
    cnt=0;
    memset(head,-1,sizeof(head));
}

int S,T,n,m;
int d[maxn],gap[maxn],curedge[maxn],pre[maxn];
//curedge[]Ϊµ±Ç°»¡Êý×飬preΪǰÇýÊý×é

int sap(int S,int T,int n)  //nΪµãÊý
{
    int cur_flow,flow_ans=0,u,tmp,neck,i;
    memset(d,0,sizeof(d));
    memset(gap,0,sizeof(gap));
    memset(pre,-1,sizeof(pre));
    for(i=0;i<=n;i++)curedge[i]=head[i]; //³õÊŒ»¯µ±Ç°»¡ÎªµÚÒ»ÌõÁڜӱí
    gap[0]=n;
    u=S;
    while(d[S]<n)             //µ±d[S]>=nʱ£¬ÍøÂçÖп϶š³öÏÖÁ˶ϲã
    {
        if(u==T)
        {
            cur_flow=INF;
            for(i=S;i!=T;i=edge[curedge[i]].to)
            {                           //Ôö¹ã³É¹Š£¬Ñ°ÕÒÆ¿Ÿ±±ß
                if(cur_flow>edge[curedge[i]].cost)
                {
                    neck=i;
                    cur_flow=edge[curedge[i]].cost;
                }
            }
            for(i=S;i!=T;i=edge[curedge[i]].to)
            {                             //ÐޞķŸ¶ÉϵıßÈÝÁ¿
                tmp=curedge[i];
                edge[tmp].cost-=cur_flow;
                edge[tmp^1].cost+=cur_flow;
            }
            flow_ans+=cur_flow;
            u=neck;                     //ÏÂŽÎÔö¹ãŽÓÆ¿Ÿ±±ß¿ªÊŒ
        }
        for(i=curedge[u];i!=-1;i=edge[i].next)
            if(edge[i].cost&&d[u]==d[edge[i].to]+1)
               break;
        if(i!=-1)
        {
            curedge[u]=i;
            pre[edge[i].to]=u;
            u=edge[i].to;
        }
        else
        {
            if(0==--gap[d[u]])break;    //gapÓÅ»¯
            curedge[u]=head[u];
            for(tmp=n,i=head[u];i!=-1;i=edge[i].next)
                if(edge[i].cost)
                   tmp=min(tmp,d[edge[i].to]);
            d[u]=tmp+1;
            ++gap[d[u]];
            if(u!=S)u=pre[u];           //ÖرêºÅ²¢ÇÒŽÓµ±Ç°µãÇ°ÇýÖØÐÂÔö¹ã
        }
    }
    return flow_ans;
}

bool use[maxn];
int bin[maxn],dsc[maxn];
int find(int x)
{
    return x!=bin[x]?bin[x]=find(bin[x]):x;
}
void merge(int x,int y)
{
    int a,b;
    a=find(x);
    b=find(y);
    if(a!=b)bin[a]=b;
}
int main()
{
    int t,i,k,n,x,y,flag,sum,Q;
    char s[30];
    scanf("%d",&t);
    for(int l=1;l<=t;l++)
    {
        init(); flag=1; sum=0;
        scanf("%d",&n); S=0; T=27;
        memset(use,0,sizeof(use));
        memset(dsc,0,sizeof(dsc));
        for(i=1;i<=35;i++)bin[i]=i;
        for(i=1;i<=n;i++){
            scanf("%s%d",s,&k);
            x=s[0]-'a'+1; y=s[strlen(s)-1]-'a'+1;
            dsc[x]--; dsc[y]++;
            if(k==1)add(x,y,1);
            merge(x,y);
            use[x]=1; use[y]=1; Q=x;
        }
        printf("Case %d: ",l);
        for(i=1;i<=26;i++){
            if(use[i]&&find(i)!=find(Q)){
                flag=0;
                break;
            }
            if(dsc[i]%2){
                sum++;
                if(sum==1)x=i;
                if(sum==2)y=i;
            }
        }
        if(flag==0||!(sum==0||sum==2)){printf("Poor boy!\n");continue;}
        if(sum==2){dsc[x]--;dsc[y]++;add(x,y,1);}
        sum=0;
        for(i=1;i<=26;i++){
            if(use[i]){
                if(dsc[i]>0)add(i,T,dsc[i]/2);
                if(dsc[i]<0)add(S,i,-dsc[i]/2),sum+=-dsc[i]/2;
            }
        }
        int w=sap(S,T,T+1);
        if(w!=sum)printf("Poor boy!\n");
        else printf("Well done!\n");
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值