POJ1637: Sightseeing tour 题解

感觉这个网络流的建图还是很妙的
欧拉回路的判定肯定是所有点的入度=出度
我们考虑刚开始随意给无向边定向,这样算出每个点的入度和出度
我们改变一条有向边的方向,会使某个点度数+2,某个点度数-2,所以点的度数的奇偶性不变
我们考虑这样建图:按照我们刚开始给无向边定的向反向连边,容量为1,对于d>0的点i,从s向i连流量为d/2的边,对于d<0的点i,从i向t连流量为-d/2的边,跑最大流判断是否满流即可
简证正确性如下:考虑一条从s到t的路径 sa1a2...ant s → a 1 → a 2 → . . . → a n → t a1 a 1 的度数会-2, an a n 的度数会+2,中间的点左边的边和右边的边同时反向,度数不变,这样走一趟相当于把路径上所有的边取反,跑网络流即判断是否存在一种取反操作使得所有点度数为0
PS:判断欧拉路径的方法
还是先随便定向,如果不是有且仅有两个奇点且一正一负则无解,否则在这两个点之间加一条边,就可以跑欧拉回路了

#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <cstdlib>
#include <utility>
#include <cctype>
#include <bitset>
#include <sstream>
#include <cmath>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#define LL long long
#define LB long double
#define x first
#define y second
#define pb push_back
#define pf push_front
#define mp make_pair
#define Pair pair<int,int>
#define pLL pair<LL,LL>
#define pii pair<double,double>
#define LOBIT(x) x & (-x)
using namespace std;

const int INF=2e9;
const LL LINF=2e16;
const int magic=348;
const int MOD=998244353;
const double eps=1e-10;
const double pi=acos(-1);

inline int getint()
{
    bool f;char ch;int res;
    while (!isdigit(ch=getchar()) && ch!='-') {}
    if (ch=='-') f=false,res=0; else f=true,res=ch-'0';
    while (isdigit(ch=getchar())) res=res*10+ch-'0';
    return f?res:-res;
}

inline LL getLL()
{
    bool f;char ch;LL res;
    while (!isdigit(ch=getchar()) && ch!='-') {}
    if (ch=='-') f=false,res=0; else f=true,res=ch-'0';
    while (isdigit(ch=getchar())) res=res*10+ch-'0';
    return f?res:-res;
}

const int MAXN=20000;

int n,e;
int d[MAXN+48];
struct Edge
{
    int x,y,type;
    inline void input() {x=getint();y=getint();type=getint();}
}edge[MAXN+48];

int head[MAXN+48],to[MAXN+48],nxt[MAXN+48],f[MAXN+48],cur[MAXN+48],tot=1,t;
inline void addedge(int s,int t,int cap)
{
    to[++tot]=t;nxt[tot]=head[s];head[s]=tot;f[tot]=cap;
    to[++tot]=s;nxt[tot]=head[t];head[t]=tot;f[tot]=0;
}

int depth[MAXN+48],q[MAXN+48],Head,Tail;
inline bool bfs()
{
    int i,x,y;
    for (i=0;i<=t;i++) depth[i]=-1;
    depth[0]=0;q[1]=0;Head=Tail=1;
    while (Head<=Tail)
    {
        x=q[Head++];
        for (i=head[x];i;i=nxt[i])
        {
            y=to[i];
            if (depth[y]==-1 && f[i])
            {
                depth[y]=depth[x]+1;
                q[++Tail]=y;
            }
        }
    }
    if (depth[t]==-1) return false; else return true;
}

inline int dfs(int x,int maxf)
{
    if (x==t) return maxf;
    int y,minf,now,ans=0;
    for (int &i=cur[x];i;i=nxt[i])
    {
        y=to[i];
        if (depth[y]==depth[x]+1 && f[i])
        {
            minf=min(maxf-ans,f[i]);
            now=dfs(y,minf);
            f[i]-=now;f[i^1]+=now;ans+=now;
        }
    }
    if (!ans) depth[x]=0;
    return ans;
}

inline int dinic()
{
    int ans=0,i;
    while (bfs())
    {
        for (i=0;i<=t;i++) cur[i]=head[i];
        ans+=dfs(0,INF);
    }
    return ans;
}

inline void Clear()
{
    tot=1;memset(head,0,sizeof(head));
    memset(d,0,sizeof(d));
}

int main ()
{
    int i,ca;ca=getint();
    while (ca--)
    {
        n=getint();e=getint();Clear();
        for (i=1;i<=e;i++)
        {
            edge[i].input();
            d[edge[i].x]--;d[edge[i].y]++;
        }
        bool f=true;
        for (i=1;i<=n;i++)
            if (d[i]%2==1) {f=false;break;}
        if (!f) {puts("impossible");continue;}
        for (i=1;i<=e;i++)
            if (!edge[i].type) addedge(edge[i].y,edge[i].x,1);
        t=n+1;int sum=0;
        for (i=1;i<=n;i++)
        {
            if (d[i]>0) addedge(0,i,d[i]/2),sum+=d[i]/2;
            if (d[i]<0) addedge(i,t,-d[i]/2);
        }
        int ans=dinic();
        if (ans==sum) puts("possible"); else puts("impossible");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值