HDU 3472 混合图欧拉回路

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


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


代码:

#pragma comment(linker,"/STACK:102400000,102400000")
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
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ʱ£¬ÍøÂçÖп϶š³öÏÖÁ˶ϲã
    {
        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;
}


你可能感兴趣的:(欧拉回路)