欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2023-12-26 16:53:39
Euler Circuit UVA - 10735题意&&思路传送门1传送门2 (里面有讲什么是欧拉回路)AC#include #include #include #include #include #include #include #include

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;
}

本文地址:https://blog.csdn.net/qq_45377553/article/details/109625956

上一篇:

下一篇: