[cf1038E][欧拉路]
2 seconds
256 megabytes
standard input
standard output
You are given nn blocks, each of them is of the form [color11 |value|color22 ], where the block can also be flipped to get [color22 |value|color11 ].
A sequence of blocks is called valid if the touching endpoints of neighboring blocks have the same color. For example, the sequence of three blocks A, B and C is valid if the left color of the B is the same as the right color of the A and the right color of the B is the same as the left color of C.
The value of the sequence is defined as the sum of the values of the blocks in this sequence.
Find the maximum possible value of the valid sequence that can be constructed from the subset of the given blocks. The blocks from the subset can be reordered and flipped if necessary. Each block can be used at most once in the sequence.
The first line of input contains a single integer nn (1≤n≤1001≤n≤100 ) — the number of given blocks.
Each of the following nn lines describes corresponding block and consists of color1 value and color2 (1≤color1,color2≤4 1≤value≤100000).
Print exactly one integer — the maximum total value of the subset of blocks, which makes a valid sequence.
6
2 1 4
1 2 4
3 4 4
2 8 3
3 16 3
1 32 2
63
7
1 100000 1
1 100000 2
1 100000 2
4 50000 3
3 50000 4
4 50000 4
3 50000 3
300000
4
1 1000 1
2 500 2
3 250 3
4 125 4
1000
In the first example, it is possible to form a valid sequence from all blocks.
One of the valid sequences is the following:
[4|2|1] [1|32|2] [2|8|3] [3|16|3] [3|4|4] [4|1|2]
The first block from the input ([2|1|4] →→ [4|1|2]) and second ([1|2|4] →→ [4|2|1]) are flipped.
In the second example, the optimal answers can be formed from the first three blocks as in the following (the second or the third block from the input is flipped):
[2|100000|1] [1|100000|1] [1|100000|2]
In the third example, it is not possible to form a valid sequence of two or more blocks, so the answer is a sequence consisting only of the first block since it is the block with the largest value.
题意:有n个木棒,每个木棒两段有两种颜色,总颜色数<=4,两个木棒颜色一样的一段可以连接起来,得到的新木棒的价值为vi+vj,求拼接后得到的新木棒的最大价值
题解:可以把颜色看成点,木棒看成边,那么由于每种木棒只能使用一次,所以最终结果就是求一条权值和最大的欧拉路(每条边只经过一次的路径)。1)如果图中的度为奇数的点的个数<=2,那么该图是个欧拉图,权值和也就是该图的权值和,2)而本题的点数只有4个,所以如果不是欧拉图,那么度为奇数的点一定是4个,则此时要删掉一条连接两个度为奇数的边,则可以枚举这条要被删掉的边从而得到最大值(注意这个图不一定是连通图,所以要用dfs标记得到所有连通块)
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
using namespace std;
//#define io_test
#define debug(x,y) cout<<x/2+1<<"#######"<<y/2+1<<"####"<<dp[x]<<endl;
vector<int>g[];
struct edge{
int x;
int y;
int q;
int val;
int nex;
}e[];
int vis[];
int cnt,head[],bk[],d[];
void adde(int x1,int y1,int z1){
e[cnt].x=x1;
e[cnt].y=y1;
e[cnt].val=z1;
e[cnt].q=;
e[cnt].nex=head[x1];
head[x1]=cnt++;
e[cnt].x=y1;
e[cnt].y=x1;
e[cnt].val=z1;
e[cnt].q=;
e[cnt].nex=head[y1];
head[y1]=cnt++;
}
void dfs(int u,int col){
vis[u]=col;
for(int i=head[u];i!=-;i=e[i].nex){
int v=e[i].y;
if(e[i].q)continue;
if(!vis[v]){
dfs(v,col);
}
}
}
int oula(){
int cnt=;
int ans=;
memset(vis,,sizeof(vis));
for(int i=;i<=;i++){
if(!vis[i]){
cnt++;
dfs(i,cnt);
int sum=;
int odd=;
for(int j=;j<=;j++){
if(vis[j]==cnt){
sum+=bk[j];
if(d[j]%)odd++;
}
}
if(odd<=)ans=max(ans,sum);
}
}
return ans;
}
int main()
{
#ifdef io_test
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif // io_test
int n;
int sum=;
scanf("%d",&n);
memset(head,-,sizeof(head));
for(int i=;i<=n;i++){
int c1,v1,c2;
scanf("%d%d%d",&c1,&v1,&c2);
adde(c1,c2,v1);
d[c1]++;
d[c2]++;
bk[c2]+=v1;
}
int ss=oula();
if(ss){
printf("%d\n",ss);
}
else{
int ans=;
for(int i=;i<=;i++){
if(d[i]&){
for(int j=head[i];j!=-;j=e[j].nex){
int v=e[j].y;
d[i]--;
d[v]--;
e[j].q=;
e[j^].q=;
ans=max(ans,oula()-e[j].val);
d[i]++;
d[v]++;
e[j].q=;
} }
}
printf("%d\n",ans);
}
return ;
}
[cf1038E][欧拉路]的更多相关文章
- 洛谷P1341 无序字母对[无向图欧拉路]
题目描述 给定n个各不相同的无序字母对(区分大小写,无序即字母对中的两个字母可以位置颠倒).请构造一个有n+1个字母的字符串使得每个字母对都在这个字符串中出现. 输入输出格式 输入格式: 第一行输入一 ...
- POJ1386Play on Words[有向图欧拉路]
Play on Words Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 11846 Accepted: 4050 De ...
- hdu1161 欧拉路
欧拉路径是指能从一个点出发能够“一笔画”完整张图的路径:(每条边只经过一次而不是点) 在无向图中:如果每个点的度都为偶数 那么这个图是欧拉回路:如果最多有2个奇数点,那么出发点和到达点必定为该2点,那 ...
- UVA10054The Necklace (打印欧拉路)
题目链接 题意:一种由彩色珠子组成的项链.每个珠子的两半由不同的颜色组成.相邻的两个珠子在接触的地方颜色相同.现在有一些零碎的珠子,需要确定他们是否可以复原成完整的项链 分析:之前也没往欧拉路上面想, ...
- 洛谷 P1341 无序字母对 Label:欧拉路 一笔画
题目描述 给定n个各不相同的无序字母对(区分大小写,无序即字母对中的两个字母可以位置颠倒).请构造一个有n+1个字母的字符串使得每个字母对都在这个字符串中出现. 输入输出格式 输入格式: 第一行输入一 ...
- POJ 1637 Sightseeing tour (混合图欧拉路判定)
Sightseeing tour Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6986 Accepted: 2901 ...
- hihocoder 1181 欧拉路.二
传送门:欧拉路·二 #1181 : 欧拉路·二 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 在上一回中小Hi和小Ho控制着主角收集了分散在各个木桥上的道具,这些道具其 ...
- hiho48 : 欧拉路·一
时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho最近在玩一个解密类的游戏,他们需要控制角色在一片原始丛林里面探险,收集道具,并找到最后的宝藏.现在他们控制的 ...
- hdu5883 The Best Path(欧拉路)
题目链接:hdu5883 The Best Path 比赛第一遍做的时候没有考虑回路要枚举起点的情况导致WA了一发orz 节点 i 的贡献为((du[i] / 2) % 2)* a[i] 欧拉回路的起 ...
随机推荐
- BZOJ 1269 文本编辑器editor(伸展树)
题意 https://www.lydsy.com/JudgeOnline/problem.php?id=1269 思路 伸展树(\(\text{splay}\))功能比较齐全的模板,能较好的体现 \( ...
- Webpack + vue 搭建
前言: 为何使用webpack? 为何相对于gulp&grunt更有优势 WebPack(前往官网)可以看做是模块打包机:直接分析项目结构,找到JavaScript模块以及其它的一些浏览器不能 ...
- Git Github的区别 & Pycharm使用GitHub
首先:git和github功能很强大,随着使用深入,我将随时填充,更新这篇文章,记录随时遇到的新的问题和感悟. 第一次知道github是看廖雪峰的课程,所谓版本管理,之前在辉煌科技用的是SVN,了解一 ...
- sublime汉化
1.打开sublime使用快捷键 shift+ctrl+p调出package control; 2.键入Package Control:install package 会弹出一个输入框,然后再搜索lo ...
- 启动xampp出错,Port 80 in use by "Unable to open process" with PID 4!
启动xampp出错,Port 80 in use by "Unable to open process" with PID 4! 环境:windows10 80端口被PID为4的应 ...
- 学习笔记60—SPSS
一.直方图上显示曲线图:打开SPSS ----> 导入数据 ----> 描述统计 ----> 分析 ----> 频率 ----> 图表 ----> 直方图(勾上在直 ...
- mysql数据库通过二进制 -【恢复数据记录】
1.修改配置文件 vi /etc/my.cnf log-bin = binlog systemctl restart mysqld mysql -uroot -p123456 mysql> sh ...
- mvn dependency:tree的用法
一.参考文档 https://maven.apache.org/plugins/maven-dependency-plugin/examples/resolving-conflicts-using-t ...
- 把旧系统迁移到.Net Core 2.0 日记 (20) --使用MiniProfiler for .NET
要查看页面耗时,EFCore的性能. 安装 MiniProfiler.AspNetCore.Mvc 还有 MiniProfiler.EntityFrameworkCore Install-Packa ...
- 【问题】用ant编译时,提示编码utf为不可映射字符
分析:eclipse默认的编码为gbk,而ant里的build.xml文件里定义的为utf-8格式.两者格式不统一. 建议:将工程的编码改成utf-8的格式,一般java工程也建议为utf-8格式.