[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] 欧拉回路的起 ...
随机推荐
- i.MX6UL -- PWM用户空间使用方法【转】
本文转载自:https://blog.csdn.net/u014486599/article/details/53010114 i.MX6UL -- PWM用户空间使用方法 开发平台: 珠海鼎芯D51 ...
- Flutter - BottomNavigationBar底部导航栏切换后,状态丢失。底部
import 'package:flutter/material.dart'; import './pages/home_page.dart'; import './pages/book_page.d ...
- Java 基础 - 集合
Set 集合 Set 集合通常不能记住元素的添加顺序.Set 集合不允许包含相同的元素. HashSet 是 Set 接口的实现类,按照 Hash 算法存储集合中的元素.HashSet 不是同步的,如 ...
- sql添加一个list的查询条件
编程中往往会有需要对某个list的值进行查询的需求,而将一个list作为查询条件,我所知道的有两种方法: 1.for循环遍历,每次循环一个sql,每次查list中一个条件的数据,最后累加 ...(最基 ...
- loj#3 -Copycat
原题链接:https://loj.ac/problem/3 题目描述: --- Copycat 内存限制:256 MiB 时间限制:1000 ms 输入文件: copycat.in 输出文件: cop ...
- 将php-fpm添加至service服务
1. 使用命令:cd /usr/local/php/etc,进入etc目录,编辑 php-fpm.conf 文件,将 ;pid = run/php-fpm.pid 前面的分号去掉 2. 重启php- ...
- VR外包团队—国内首家VR虚拟现实主题公园即将在北京推出
期,美国“The VOID”.澳洲“Zero Latency”两大虚拟现实主题乐园让许多爱好者兴奋至极,门票据说都已经预约到明年2月!在如此巨大的商机面前,谁将抢到国内VR虚拟现实主题公园第一块蛋糕? ...
- EPEL源-是什么全称
EPEL源 EPEL (Extra Packages for Enterprise Linux)是基于Fedora的一个项目,为“红帽系”的操作系统提供额外的软件包,适用于RHEL.CentOS和 ...
- MySQL中case then用法
1.查询图书价格,若价格为null,则显示unknown,若价格为10到20, 则显示10 to 20 SELECT price, CASE WHEN price='null' THEN 'UnKno ...
- Cocos Creator 使用protobufjs
Win7 + Creator 2.0.0 + protobufjs 6.8.8 1.下载安装protobufjs npm install -g protobufjs 可以看到protobufjs安装在 ...