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(欧拉路径)的更多相关文章

  1. Codeforces Round #508 (Div. 2)

    Codeforces Round #508 (Div. 2) http://codeforces.com/contest/1038 A #include<bits/stdc++.h> us ...

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

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

  3. 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 ...

  4. 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 ...

  5. Codeforces Round #508 (Div. 2) D. Slime

    D. Slime 题目链接:https://codeforces.com/contest/1038/problem/D 题意: 给出两个数,然后每次可以对相邻的两个数合并,比如x,y,那么合并过后就是 ...

  6. Codeforces Round #172 (Div. 2) D. Maximum Xor Secondary 单调栈应用

    http://codeforces.com/contest/281/problem/D 要求找出一个区间,使得区间内第一大的数和第二大的数异或值最大. 首先维护一个单调递减的栈,对于每个新元素a[i] ...

  7. 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 ...

  8. Codeforces Round #445 Div. 1 C Maximum Element (dp + 组合数学)

    题目链接: http://codeforces.com/contest/889/problem/C 题意: 给你 \(n\)和 \(k\). 让你找一种全排列长度为\(n\)的 \(p\),满足存在下 ...

  9. Codeforces Round #599 (Div. 2) A. Maximum Square 水题

    A. Maximum Square Ujan decided to make a new wooden roof for the house. He has

随机推荐

  1. 为什么Python在列表和元组的末尾允许使用逗号?

    Python 允许您在列表,元组和字典的末尾添加一个尾随逗号: [1, 2, 3,] ('a', 'b', 'c',) d = { "A": [1, 5], "B&quo ...

  2. CSS3自定义字体

    原文摘自:https://www.cnblogs.com/moqiutao/archive/2015/12/23/5070463.html 总节: 1) 定义字体标准格式: @font-face { ...

  3. 试用Markdown来写东西

    试用Markdown来写东西 前言 之前有过一段时间的写东西的习惯,但是后来因为各种原因(主要是因为自己懒惰拖延),所以一直没有写,现在想再开始写,目的很明确,就是发现很多时候,写作能够很好的练习自己 ...

  4. 饥饿的小易(枚举+广度优先遍历(BFS))

    题目描述 小易总是感觉饥饿,所以作为章鱼的小易经常出去寻找贝壳吃.最开始小易在一个初始位置x_0.对于小易所处的当前位置x,他只能通过神秘的力量移动到 4 * x + 3或者8 * x + 7.因为使 ...

  5. Matlab 图象操作函数讲解

    h = imrect;pos = getPosition(h); 这个函数用来获取图象上特定区域的坐标,其中pos的返回值中有四个参数[xmin,ymin,width,height],特定区域的左上角 ...

  6. 基于freeRTOS定时器实现闹钟(定时)任务

    基于freeRTOS定时器实现闹钟(定时)任务 在智能硬件产品中硬件中,闹钟定时任务是基本的需求.一般通过APP设置定时任务,从云端或者是APP直连硬件将闹钟任务保存在硬件flash中,硬件运行时会去 ...

  7. ServiceStack.Ormlit 使用Insert的时候自增列不会被赋值

    Insert签名是这样的,将第2个参数设置为true就会返回刚插入的自增列ID了,然后可以手工赋值到对象上面去 public static long Insert<T>(this IDbC ...

  8. 有关WCSF的几点整理

    本文示例代码 一.CreateNew Attribute实现属性注入 Steps: 1/ aspx创建某个服务的属性. 2/ 为其添加[CreateNew] Attribute. 3/ 页面继承自Mi ...

  9. 从hive导入到oracle(Hcatalog)

    1.使用catalog的情况下: sqoop export --table tableName2 \ #oracle表 --connect jdbc:oracle:thin:@127.0.0.1:15 ...

  10. 自测之Lesson15:TCP&UDP网络编程

    题目:编写一个TCP通信的程序. 实现代码: #include <stdio.h> #include <sys/socket.h> #include <unistd.h& ...