Codeforces 1038 E - Maximum Matching

Posted widsom

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces 1038 E - Maximum Matching相关的知识,希望对你有一定的参考价值。

E - Maximum Matching

思路:

欧拉图

定理:一个度数为奇数的点的个数小于等于2的联通图存在欧拉回路

对于这道题目的图,点的个数为4,所以最坏的情况下4个点的度数都为奇数,在这种情况下只要删去一条边就可以满足条件了

所以枚举删掉的边,跑联通图,最后判断联通图是否符合条件,复杂度:O(n^2)

代码:

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize(4)
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pi acos(-1.0)
#define LL long long
#define mp make_pair
#define pb push_back
#define ls rt<<1, l, m
#define rs rt<<1|1, m+1, r
#define ULL unsigned LL
#define pll pair<LL, LL>
#define pii pair<int, int>
#define piii pair<pii, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout);
//head

const int N = 120;
struct edge {
    int to, w, id;
};
vector<edge> g[N]; 
int d[5];
pii a[N];
bool vis[N], node[5];
LL ans, tot = 0;
void dfs(int u) {
    node[u] = true;
    for (int i = 0; i < g[u].size(); i++) {
        int id = g[u][i].id;
        if(!vis[id]) {
            vis[id] = true;
            dfs(g[u][i].to);
            tot += g[u][i].w;
        }
    }
}
int main() {
    int n, u, v, w;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        scanf("%d %d %d", &u, &w, &v);
        g[u].pb(edge{v, w, i});
        g[v].pb(edge{u, w, i});
        d[u]++;
        d[v]++;
        a[i].fi = u;
        a[i].se = v;
    }
    ans = 0;
    for (int i = 0; i <= n; i++) {
        if(i != 0 && a[i].fi == a[i].se) continue; 
        for (int j = 0; j <= n; j++) vis[j] = false;
        vis[i] = true;
        d[a[i].fi] --;
        d[a[i].se] --;
        for (int j = 1; j <= 4; j++) {
            tot = 0;
            mem(node, false);
            dfs(j);
            int cnt = 0;
            for (int k = 1; k <= 4; k++) if(node[k] && (d[k]&1)) cnt++;
            if(cnt <= 2)ans = max(ans, tot);
        }
        d[a[i].fi]++;
        d[a[i].se]++; 
    }
    printf("%lld
", ans);
    return 0;
}

 

以上是关于Codeforces 1038 E - Maximum Matching的主要内容,如果未能解决你的问题,请参考以下文章

CodeForces - 261B Maxim and Restaurant

CodeForces - 261E Maxim and Calculator

CodeForces 721D Maxim and Array

CodeForces 721D Maxim and Array

Maxim and Array CodeForces - 721D (贪心)

Codeforces 854 B Maxim Buys an Apartment 思维 水题