poj1637 判斷一個混合圖是否存在歐拉回路

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<limits.h>
#include<queue>
#define V 220
using namespace std;
struct node
{
    int to;
    int c;
    int f;
    int next;
}a[3000];
int head[V];
int tot,vi[V],vo[V];
int leve[V];
void addedge(int x,int to ,int c)
{
    a[tot].to=to;
    a[tot].c=c;
    a[tot].f=0;
    a[tot].next=head[x];
    head[x]=tot++;
    a[tot].to=x;
    a[tot].c=0;
    a[tot].f=0;
    a[tot].next=head[to];
    head[to]=tot++;
}
bool bfs(int s,int t)
{
    int x,to,i,u,v;
    memset(leve,-1,sizeof(leve));
    queue<int> q;
    q.push(s);
    leve[s]=0;
    while(!q.empty())
    {
        u=q.front();
        q.pop();
        i=head[u];
        while(i!=-1)
        {
            v=a[i].to;
            if(a[i].c>0&&leve[v]==-1)
            {
                leve[v]=leve[u]+1;
                q.push(v);
            }
            i=a[i].next;
        }
    }
    return leve[t]!=-1;
}
int dinic(int s,int t)
{
    int stack[V],last[V];
    //last數組不斷尋找該節點的上一個對應節點
    //stack數組存放上一個節點的下標
    int u,v,top=1,minf,i,temp,c,edge,sum=0;
    while(bfs(s,t))
    {
        top=1;
        stack[top]=s;
        for(i=s;i<=t;i++)
        {
            last[i]=head[i];
        }
        while(top)
        {
            u=stack[top];
            //找到匯點時
            if(u==t)
            {
                minf=INT_MAX;
                for(i=1;i<top;i++)
                {
                    edge=last[stack[i]];
                    if(minf>a[edge].c)
                    {
                        minf=a[edge].c;
                        temp=i;
                    }
                }
                for(i=1;i<top;i++)
                {
                    edge=last[stack[i]];
                    a[edge].c-=minf;
                    a[edge].f+=minf;
                    a[edge^1].c+=minf;
                    a[edge^1].f-=minf;
                }
                sum+=minf;
                top=temp;
                continue;
            }
            //the end of 找到匯點
            edge=last[u];
            while(edge!=-1)
            {
                v=a[edge].to;
                c=a[edge].c;
                if(c>0&&leve[u]+1==leve[v])
                {
                    stack[++top]=v;
                    break;
                }
                edge=a[edge].next;
            }
            last[u]=edge;
            if(edge==-1)
            {
                top--;
                if(top!=0)
                  last[stack[top]]=a[last[stack[top]]].next;
            }
        }
    }
    return sum;
}

int main()
{
    int t,k,i,j,sum=0,m,n;
    int fu,fv;
    int to,from;
    bool flag;
    scanf("%d",&t);
    while(t--)
    {
        memset(vo,0,sizeof(vo));
        memset(vi,0,sizeof(vi));
        memset(head,-1,sizeof(head));
        tot=0;
        sum=0;
        flag=true;
        scanf("%d%d",&m,&n);
        while(n--)
        {
            scanf("%d%d%d",&from,&to,&i);
            vo[from]++;
            vi[to]++;
            if(!i)
            {
                addedge(from,to,1);
            }
        }
        for(i=1;i<=m;i++)
        {
            k=vo[i]-vi[i];
            if(k%2==0)
            {
                if(k/2>0)
                {
                    addedge(0,i,k/2);
                    sum+=k/2;
                }
                else
                  {
                      addedge(i,m+1,-k/2);
                  }
            }
            else
            {
                flag=false;
                break;
            }
        }
        if(flag)
        {
            if(sum!=dinic(0,m+1))
              flag=false;
        }
        if(flag)
          printf("possible\n");
        else
          printf("impossible\n");
    }

}

發佈了7 篇原創文章 · 獲贊 3 · 訪問量 5554
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章