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 = ;
struct edge {
int to, w, id;
};
vector<edge> g[N];
int d[];
pii a[N];
bool vis[N], node[];
LL ans, tot = ;
void dfs(int u) {
node[u] = true;
for (int i = ; 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 = ; 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 = ;
for (int i = ; i <= n; i++) {
if(i != && a[i].fi == a[i].se) continue;
for (int j = ; j <= n; j++) vis[j] = false;
vis[i] = true;
d[a[i].fi] --;
d[a[i].se] --;
for (int j = ; j <= ; j++) {
tot = ;
mem(node, false);
dfs(j);
int cnt = ;
for (int k = ; k <= ; k++) if(node[k] && (d[k]&)) cnt++;
if(cnt <= )ans = max(ans, tot);
}
d[a[i].fi]++;
d[a[i].se]++;
}
printf("%lld\n", ans);
return ;
}

Codeforces 1038 E - Maximum Matching的更多相关文章

  1. Codeforces Round #508 (Div. 2) E. Maximum Matching(欧拉路径)

     E. Maximum Matching 题目链接:https://codeforces.com/contest/1038/problem/E 题意: 给出n个项链,每条项链左边和右边都有一种颜色(范 ...

  2. [codeforces 508E]Maximum Matching

    题目:Maximum Matching 传送门:http://codeforces.com/contest/1038/problem/E 分析: 一个块拥有{color1,val,color2},两个 ...

  3. [Codeforces Round #508 (Div. 2)][Codeforces 1038E. Maximum Matching]

    前几天给舍友讲这题的时候感觉挺有意思的,就贴上来吧... 题目链接:1038E - Maximum Matching 题目大意:有\(n\)个棒子,每个条两端有颜色\(c1,c2\)以及他的价值\(v ...

  4. Codeforces 1038E Maximum Matching

    可能写了个假算法 假设定义:含有一个欧拉路的图为类欧拉图 欧拉路的定义:一个无向连通图中,存在一条路径对所有边都遍历且仅遍历一次:判断方法:该连通图中度为奇数的点的个数不能超过2,即为0或者2 题目解 ...

  5. CodeForces 173C Spiral Maximum 记忆化搜索 滚动数组优化

    Spiral Maximum 题目连接: http://codeforces.com/problemset/problem/173/C Description Let's consider a k × ...

  6. CodeForces 173C Spiral Maximum (想法、模拟)

    Spiral Maximum Time Limit:3000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Sub ...

  7. CF1038E Maximum Matching 搜索/区间DP

    题目传送门:http://codeforces.com/problemset/problem/1038/E 题意:给出$N$个方块,每个方块有左右两种颜色$a,b$(可以翻转使左右两种颜色交换)和一个 ...

  8. CodeForces 1060 B Maximum Sum of Digits

    Maximum Sum of Digits You are given a positive integer n. Let S(x)S(x) be sum of digits in base 10 r ...

  9. codeforces 484B B. Maximum Value(二分)

    题目链接: B. Maximum Value time limit per test 1 second memory limit per test 256 megabytes input standa ...

随机推荐

  1. bzoj2120 / P1903 [国家集训队]数颜色 / 维护队列(带修改莫队)

    P1903 [国家集训队]数颜色 / 维护队列 带修改的莫队 在原有指针$(l,r)$上又添加了时间指针$t$ 贴一段dalao的解释 带修改的莫队,和原版莫队相比,多了一个时间轴 原版莫队是将区间( ...

  2. 细数php里的那些“坑”

    Part 1 Grammer 尽管PHP的语法已经很松散,写起来很“爽”.但是对于学过 Java 的“完全面向对象程序员“来说,PHP程序设计语言里,还是有一些的坑的.下面请让我来盘点一下. Pars ...

  3. hdu 1811 Rank of Tetris - 拓扑排序 - 并查集

    自从Lele开发了Rating系统,他的Tetris事业更是如虎添翼,不久他遍把这个游戏推向了全球. 为了更好的符合那些爱好者的喜好,Lele又想了一个新点子:他将制作一个全球Tetris高手排行榜, ...

  4. Android Studio报错view is not constrained

    在活动上面创建了两个按钮,在Design上看上去是两个按钮分开的,run一下,按钮就重合在一起了,而且一直报错,这个时候再去看一下Design,两个按钮重在一块,只显示一个按钮.如下图: button ...

  5. Jordan 块的几何

    设 $V$ 是复数域 $\mathbb{C}$ 上的 $n$ 维线性空间, $\varphi$ 是 $V$ 上的线性变换, $A\in M_n(\mathbb{C})$ 是 $\varphi$ 在某组 ...

  6. Bootstrap3基础 caret 辅助类样式 下拉的小三角

      内容 参数   OS   Windows 10 x64   browser   Firefox 65.0.2   framework     Bootstrap 3.3.7   editor    ...

  7. 【定制Android系统】Android O 在ROM中添加自己的 so 库(1)——Android.mk 与 Android.bp 的区别【转】

    本文转载自: 版权声明:本文为博主原创文章,转载时请注明原作者及出处.    https://blog.csdn.net/u014248312/article/details/82020204需求:在 ...

  8. 用vim + xdebug 来追踪thinkphp的执行过程

    tree命令的使用几个有实际应用的参数 -a 这是默认的 -d: 只显式目录, 不需要显式目录下的文件 -L: 列出显式的深度. 当前目录下的所有东西为第一级... 在tp下, 有多个Common但是 ...

  9. Hyper

    https://github.com/zeit/hyper https://gist.github.com/coco-napky/404220405435b3d0373e37ec43e54a23 Ho ...

  10. Python中的open和codecs.open

    最近老被编码困扰,多次折腾之后,感觉python的编解码做得挺好的,只要了解下边的流程,一般都能解决 input文件(gbk, utf-8...) ----decode-----> unicod ...