混合图的欧拉回路

混合图是指既有有向边又有无向边的一类图

我们一般处理无向边是将它拆成两条有向边,而在求欧拉回路时不能这么做,因为一条边只能经过一次

因此用网络流解决这个问题

黑书p324讲了两种做法

我写的第二种

首先将无向边任意定向,将其当做一条有向边,判断这个图是否可能存在欧拉回路(对于每个点,abs(入度-出度)是偶数)

接下来构建网络:

保留原来的无向边,方向为你定的方向,流量为1

对于点i

如果出度>入度,从st连一条边到i,流量为(出度-入度)/2

如果入度>出度,从i连一条边到ed,流量为(入度-出度)/2

对于这样的一个网络,跑网络流

最后判断是否满流(st连出的边是否都增广完毕)

是的话就存在欧拉回路 将那些没有被增广的弧反向,就得到了一个欧拉回路

否则不存在欧拉回路


POJ1637 裸欧拉回路


//Lib
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<ctime>

#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<queue>
using namespace std;
//Macro
#define rep(i,a,b) for(int i=a,tt=b;i<=tt;++i)
#define rrep(i,a,b) for(int i=a,tt=b;i>=tt;--i)
#define erep(i,e,x) for(int i=x;i;i=e[i].next)
#define irep(i,x) for(__typedef(x.begin()) i=x.begin();i!=x.end();i++)
#define read() (strtol(ipos,&ipos,10))
#define sqr(x) ((x)*(x))
#define pb push_back
#define PS system("pause");
typedef long long ll;
typedef pair<int,int> pii;
const int oo=~0U>>1;
const double inf=1e20;
const double eps=1e-6;
string name="",in=".in",out=".out";
//Var
struct E
{
	int next,node,cap;
}e[5000];
struct ED
{
	int a,b,d;
}edge[1008];
queue<int> q;
int h[208],tot,dis[208],gap[208];
int n,m,st,ed,T,din[208],dout[208];
void add(int a,int b,int c)
{
	e[++tot].next=h[a];e[tot].node=b;e[tot].cap=c;h[a]=tot;
	e[++tot].next=h[b];e[tot].node=a;e[tot].cap=0;h[b]=tot;
}
void Init()
{
	memset(h,0,sizeof h);tot=1;
	memset(din,0,sizeof din);
	memset(dout,0,sizeof dout);
	scanf("%d%d",&n,&m);int a,b,c;
	rep(i,1,m)
	{
		scanf("%d%d%d",&a,&b,&c);
		dout[a]++,din[b]++;
		edge[i].a=a;edge[i].b=b;edge[i].d=c;
	}
}
bool BFS()
{
	memset(dis,-1,sizeof dis);
	rep(i,1,n+2)gap[i]=h[i];
	int x,y;q.push(ed);dis[ed]=0;bool flag=false;
	while(!q.empty())
	{
		x=q.front();q.pop();
		erep(i,e,h[x])
		{
			y=e[i].node;
			if(dis[y]!=-1||!e[i^1].cap)continue;
			if(y==st)flag=true;
			dis[y]=dis[x]+1;
			q.push(y);
		}
	}
	return flag;
}
int DFS(int now,int low)
{
	if(now==ed)return low;
	int ret=low,tmp,y;
	for(int i=gap[now];i;i=e[i].next)
	{
		y=e[i].node;
		if(!e[i].cap||dis[y]!=dis[now]-1)continue;
		tmp=DFS(y,min(low,e[i].cap));
		e[i].cap-=tmp;
		e[i^1].cap+=tmp;
		low-=tmp;
		if(!low)break;
		gap[now]=e[i].next;
	}
	if(low==ret)dis[now]=-1;
	return ret-low;
}
bool Work()
{
	st=n+1,ed=n+2;
	rep(i,1,n)if(abs(dout[i]-din[i])&1)return false;
	rep(i,1,n)
		if(dout[i]>din[i])add(st,i,dout[i]-din[i]>>1);
		else if(din[i]>dout[i])add(i,ed,din[i]-dout[i]>>1);
	rep(i,1,m)if(!edge[i].d)add(edge[i].a,edge[i].b,1);
	while(BFS())
		while(DFS(st,oo));
	erep(i,e,h[st])if(e[i].cap)return false;
	return true;
}
int main()
{
//freopen((name+in).c_str(),"r",stdin);
//freopen((name+out).c_str(),"w",stdout);	
	for(scanf("%d",&T);T;--T)
	{
		Init();
		puts(Work()?"possible":"impossible");
	}
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值