Euler Circuit UVA - 10735(欧拉回路 + 网络流建模)

Euler Circuit UVA - 10735

题意&&思路

传送门1
传送门2 (里面有讲什么是欧拉回路)

AC

#include <iostream>
#include <queue>
#include <cstring>
#include <vector>
#include <string>
#include <cstdio>
#include <algorithm>
#include <stack>
#define sz(a) (int)a.size()
#define pb push_back
#define mst(x,a) memset(x,a,sizeof(x))
#define fzhead EDGE(int u, int v, int c, int f)
#define fzbody from(u), to(v), cap(c), flow(f)
#define fori(i,x,y) for(int i=(x); i<(y); i++)
#define For(i,x,y) for(int i=(x); i<=(y); i++)
using namespace std;
const int maxn = 100+10;
const int maxm = 0;
const int INF =0x3f3f3f3f;
struct EDGE{
    int from, to, cap, flow;
    EDGE(){}
    fzhead:fzbody{}
};
struct Dinic{
    int n, m, s, t;
    vector<EDGE>edges;
    vector<int>G[maxn];
    bool vis[maxn];
    int d[maxn];
    int cur[maxn];
    ///
    void init(int n){
        this->n = n;
        fori(i,0,n)G[i].clear();
        edges.clear();
    }
    void AddEdge(int from, int to, int cap){
        edges.pb({from, to, cap, 0});
        edges.pb({to, from, 0, 0});
        m=edges.size();
        G[from].pb(m-2);
        G[to].pb(m-1);
    }
    bool bfs(){
        mst(vis,0);
        queue<int>q;
        q.push(s);
        d[s] = 0;
        vis[s] = 1;
        while(!q.empty()){
            int x = q.front(); q.pop();
            fori(i, 0, sz(G[x])){
                EDGE& e = edges[G[x][i]];
                if(!vis[e.to] && e.cap > e.flow){
                    vis[e.to] = 1;
                    d[e.to] = d[x] + 1;
                    q.push(e.to);
                }
            }
        }
        return vis[t];
    }
    int dfs(int x, int a){
        if(x == t || a == 0)return a;
        int flow = 0, f;
        for(int& i = cur[x]; i<sz(G[x]); i++){
            EDGE& e = edges[G[x][i]];
            if(d[x] + 1 == d[e.to] && (f = dfs(e.to, min(a, e.cap - e.flow)))>0) {
                e.flow += f;
                edges[G[x][i]^1].flow -= f;
                flow += f;
                a -= f;
                if(a == 0)break;
            }
        }
        return flow;
    }
    ///
    int Maxflow(int s ,int t){
        this->s = s; this->t = t;
        int flow = 0;
        while(bfs()){
            mst(cur,0);
            flow += dfs(s, INF);
        }
        return flow;
    }
}dinic;
int n,m;
int st, ed;
int in[maxn], out[maxn], ma[maxn][maxn];
void init(){
    mst(in, 0); mst(out, 0); mst(ma, 0);
    cin>>n>>m;
    st = 0, ed = n+1;
    int u,v;
    dinic.init(n+5);
    string op;
    For(i,1,m){
        cin>>u>>v>>op;
        in[v]++; out[u]++;
        if(op[0]=='U')dinic.AddEdge(u,v,1);
        else ma[u][v]++;
    }
}
bool judge(){
    int ok = 1;
    int sum = 0;
    For(i,1,n){
        if((in[i] + out[i]) & 1)return false;
        if(in[i] == out[i])continue;
        ok = 0;
        if(out[i] > in[i]) dinic.AddEdge(st, i, (out[i] - in[i]) / 2), sum +=(out[i] - in[i]) / 2;
        else dinic.AddEdge(i, ed, (in[i] - out[i]) / 2);
//        if(in[i] > out[i])dinic.AddEdge(st, i, (in[i]-out[i])/2), sum += (in[i]-out[i])/2;
//        else dinic.AddEdge(i, ed, (out[i]-in[i])/2);
    }
//    cout<<"OK------flow------sum---"<<endl;
//    int flow = dinic.Maxflow(st,ed);
//    cout<<flow<<' '<<sum<<endl;
    if(ok)return true;
    else return (dinic.Maxflow(st, ed) == sum);
}
stack<int> ans;
void euler(int u){
    For(v,1,n){
        if(ma[u][v]){
            ma[u][v]--;
            euler(v);
            ans.push(v);
        }
    }
}
void print(){
    for(int i=0; i<sz(dinic.edges); i += 2){
        EDGE& e = dinic.edges[i];
        if(e.from == st) continue;
        if(e.to == ed) continue;
        if(e.flow)ma[e.to][e.from]++;///ma[e.from][e.to]++;
        else ma[e.from][e.to]++;///ma[e.to][e.from]++;
    }
    while(!ans.empty())ans.pop();
    euler(1);
    cout<<1;//ans.push(1);
    while(!ans.empty()){
        cout<<' '<<ans.top();
        ans.pop();
    }
    cout<<endl;
}
const string no="No euler circuit exist";
int main()
{
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int tt;cin>>tt;
    while(tt--){
        init();
        if(!judge())cout<<no<<endl;
        else print();
        cout<<endl;
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值