思路:
欧拉图
定理:一个度数为奇数的点的个数小于等于2的联通图存在欧拉回路
对于这道题目的图,点的个数为4,所以最坏的情况下4个点的度数都为奇数,在这种情况下只要删去一条边就可以满足条件了
欧拉回路算法:大圈小圈法,从起点开始跑每条边,把每条遍标记一下,直到跑到某个位置不能跑了,把点如栈,最后倒着输出
所以枚举删掉的边,跑联通图,最后判断联通图是否符合条件,复杂度:O(n^2)
代码:
#pragma GCC optimize(2)#pragma GCC optimize(3)#pragma GCC optimize(4)#includeusing 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 #define pii pair #define piii pair #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);//headconst int N = 120;struct edge { int to, w, id;};vector 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;}