Codeforces 1038 E - Maximum Matching
阿新 • • 發佈:2018-09-15
ems sign 存在 scan ons bit col \n printf
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; #definefi 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> #definemem(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\n", ans); return 0; }
Codeforces 1038 E - Maximum Matching