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. P3924 康娜的线段树(期望)

    P3924 康娜的线段树 看起来$O(nlogn)$可过其实由于巨大常数是无法通过的 $O(nlogn)$:70pts 我们手玩样例发现 线段树上某个节点的期望值$f[o]=(f[lc]+f[rc]) ...

  2. 20145319 《计算机病毒》动态分析lab3-2

    20145319 <计算机病毒>动态分析lab3-2(实践六) 实践过程 基础分析 拿到恶意代码时,首先使用PE ID打开,查看其中一些基础信息以及观察该恶意代码是否加壳,来确定下一步分析 ...

  3. SSM集成activiti6.0错误集锦(一)

    项目环境 Maven构建 数据库:Orcle12c 服务器:Tomcat9 <java.version>1.8</java.version> <activiti.vers ...

  4. CSS的再深入(更新中···)

    在上一章我们提到了一个新的概念,叫做块级样式,讲到这里就要科普一下: 标签又分为两种: (1)块级标签 元素特征:会独占一行,无论内容多少,可以设置宽高··· (2)内敛标签(又叫做行内标签) 元素特 ...

  5. Django框架(六) Django之模板继承

    模版导入和继承 模版导入 一个页面只能继承一个模板,如何解决了?如何使用多个模板,或者引入其他页面 <% include "a.html" %> 可以引用多次 模板,i ...

  6. 【lg1799】数列

    题目描述 虽然msh长大了,但她还是很喜欢找点游戏自娱自乐.有一天,她在纸上写了一串数字:1,l,2,5,4.接着她擦掉了一个l,结果发现剩下l,2,4都在自己所在的位置上,即1在第1位,2在第2位, ...

  7. 5. 多重背包问题 II 【用二进制优化】

    多重背包问题 II 描述 有 NN 种物品和一个容量是 VV 的背包. 第 ii 种物品最多有 sisi 件,每件体积是 vivi,价值是 wiwi. 求解将哪些物品装入背包,可使物品体积总和不超过背 ...

  8. x盒子

    0换成1切回

  9. Docker 使用Dockerfile构建tomcat镜像

    Dockerfile概念: 镜像的定制实际上就是定制每一层所添加的配置.文件.如果我们可以把每一层修改.安装.构建.操作的命令都写入一个脚本,用这个脚本来构建.定制镜像,那么之前提及的无法重复的问题. ...

  10. (zhuan) Evolution Strategies as a Scalable Alternative to Reinforcement Learning

    Evolution Strategies as a Scalable Alternative to Reinforcement Learning this blog from: https://blo ...