Codeforces Round #508 (Div. 2) E. Maximum Matching(欧拉路径)
E. Maximum Matching
题目链接:https://codeforces.com/contest/1038/problem/E
题意:
给出n个项链,每条项链左边和右边都有一种颜色(范围1~4),然后每条项链都有对应的价值。
现在你可以任意改变项链的位置,也可以交换左右两边的颜色,问怎么做才能得到最大的价值。一条项链得到价值,就要求其左边的颜色和左边的项链右边颜色相等,并且右边的颜色和右边项链左边的颜色相等。
题解:
分析就可以发现这个题就是找一条权值最大的欧拉路径(每条边刚好经过一次),我们这样构造,将两边的颜色看作点,然后价值看作边上的权值并且连接这两个点。
由于存在欧拉路径的充要条件就是度数为奇数的点不超过两个,这个题目中最多只有四个点,所以我们只需要枚举去掉一条边然后来跑欧拉路径就是了。
具体细节见代码吧:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = ;
int n, sum;
int d[N], vis[N], del[N],check[N];
struct node {
int id, v, val;
};
vector <node> g[N], edges[N];
void Euler(int u, int fa) {
check[u]=;
for(auto v : g[u]) {
if(del[v.id] || vis[v.id])
continue ;
vis[v.id]=;
sum += v.val;
Euler(v.v, u);
}
}
bool Can() {
int cnt = ;
for(int i = ; i <= ; i++) {
if(check[i] && (d[i] & ))
cnt++;
}
return cnt <= ;
}
void init(){
memset(vis, , sizeof(vis));
memset(check,,sizeof(check));
sum = ;
}
int main() {
ios::sync_with_stdio(false);
cin.tie();
cin >> n;
for(int i = ; i <= n; i++) {
int c1, c2, v;
cin >> c1 >> v >> c2;
g[c1].push_back(node{i, c2, v});
g[c2].push_back(node{i, c1, v});
d[c1]++;
d[c2]++;
edges[i].push_back(node{c1, c2, v});
}
int ans = ;
for(int i = ; i <= ; i++) {
init();
Euler(i, -);
if(Can())
ans = max(ans, sum);
}
for(int i = ; i <= n; i++) {
del[i - ] = ;
del[i] = ;
d[edges[i][].id]--;
d[edges[i][].v]--;
for(int j = ; j <= ; j++) {
init();
Euler(j, -);
if(Can()){
ans = max(ans, sum);
}
}
d[edges[i][].id]++;
d[edges[i][].v]++;
}
cout << ans;
return ;
}
Codeforces Round #508 (Div. 2) E. Maximum Matching(欧拉路径)的更多相关文章
- Codeforces Round #508 (Div. 2)
Codeforces Round #508 (Div. 2) http://codeforces.com/contest/1038 A #include<bits/stdc++.h> us ...
- [Codeforces Round #508 (Div. 2)][Codeforces 1038E. Maximum Matching]
前几天给舍友讲这题的时候感觉挺有意思的,就贴上来吧... 题目链接:1038E - Maximum Matching 题目大意:有\(n\)个棒子,每个条两端有颜色\(c1,c2\)以及他的价值\(v ...
- Codeforces Round #221 (Div. 1) B. Maximum Submatrix 2 dp排序
B. Maximum Submatrix 2 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset ...
- Codeforces Round #276 (Div. 1) B. Maximum Value 筛倍数
B. Maximum Value Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/484/prob ...
- Codeforces Round #508 (Div. 2) D. Slime
D. Slime 题目链接:https://codeforces.com/contest/1038/problem/D 题意: 给出两个数,然后每次可以对相邻的两个数合并,比如x,y,那么合并过后就是 ...
- Codeforces Round #172 (Div. 2) D. Maximum Xor Secondary 单调栈应用
http://codeforces.com/contest/281/problem/D 要求找出一个区间,使得区间内第一大的数和第二大的数异或值最大. 首先维护一个单调递减的栈,对于每个新元素a[i] ...
- Codeforces Round #276 (Div. 1)B. Maximum Value 筛法
D. Maximum Value You are given a sequence a consisting of n integers. Find the maximum possible ...
- Codeforces Round #445 Div. 1 C Maximum Element (dp + 组合数学)
题目链接: http://codeforces.com/contest/889/problem/C 题意: 给你 \(n\)和 \(k\). 让你找一种全排列长度为\(n\)的 \(p\),满足存在下 ...
- Codeforces Round #599 (Div. 2) A. Maximum Square 水题
A. Maximum Square Ujan decided to make a new wooden roof for the house. He has
随机推荐
- react项目总结
1.基本框架 1.react+react-router4+redux3.7.2 2.css预编译使用sass 3.数据请求使用axios(原本是使用fetch,结果在ios10下报错) 4.ui组件库 ...
- (转) GEM透视阴影贴图
转载:小道 透视阴影贴图(Perspective Shadow Maps, PSMs)是由Stamminger和Drettakis在 SIGGRAPH 2002上提出的一种阴影贴图(Shadow Ma ...
- linux NULL 的定义
#undef NULL #if defined(__cplusplus) #define NULL 0 #else #define NULL ((void *)0) #endif
- logisitic回归
线性回归目的是找到一条直线(或者超平面)尽可能地接近所有的训练数据点,而对数几率回归的目的是找到一条直线(或者超平面)尽可能地分开两种不同类别的数据点. 对数几率回归感觉更像是一个分类问题.https ...
- 《C++面试知识点》
[动态内存] 1. 由内置指针管理的动态内存(即new和delete管理动态内存),直到被显式释放之前它都是存在的.假设该指针变量被销毁,那该内存将不会自动释放(即所谓的“内存泄漏”). 2. 可以用 ...
- c# word 删除指定内容
1.首先简单的是获取得到的range,直接rangge.delete() 2.文本框的删除: foreach (Microsoft.Office.Interop.Word.Shape shape in ...
- python学习第一天-语法学习
1.python简介 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,Guido开始写能够解释Python语言语法的解释器.Python这个名字,来自 ...
- iOS 出现错误reason: image not found的解决方案
在制作framework时遇到真机运行时导致的reason: image not found允许崩溃的问题,下面是我的解决方案: 首先我们分析一下出现这种情况的原因,原因就是framework找不到镜 ...
- 3dContactPointAnnotationTool开发日志(二)
今天看的时候发现其实www的方式是可以根据指定路径读取本地图片到Image中的.也就是昨天提到的第二种方式. 随便选了个图片做示范: 修改后的代码如下: using System.Collec ...
- 【week2】燃尽图
燃尽图(burn down chart)是在项目完成之前,对需要完成的工作的一种可视化表示.燃尽图有一个Y轴(工作)和X轴(时间).理想情况下,该图表是一个向下的曲线,随着剩余工作的完成,“烧尽”至零 ...