[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] 欧拉回路的起 ...
随机推荐
- 深入学习IOZone【转】
本文转载自:https://blog.csdn.net/werm520/article/details/7262103 深入学习IOZone 刘智朋 2011-3-29 1 ...
- mvc 之 RouteConfig配置
//这里没有使用对用的指定参数 /Day_1:表示解决方案的名称,意思是默认找到该项目解决方案目录下的controllers进行匹配 routes.MapRoute( "Default&qu ...
- Unity3D_UGUI判断鼠标或者手指是否点击在UI上
比如战斗场景,UI和3D场景同时都需要响应触摸事件,如果同时响应可能就会出现触摸UI的时候影响到了3D部分.为了解决这个问题在判断3D响应之前要先判断手指是否点击在UI上. 以前NGUI的时候都是自己 ...
- HDU 5405 Sometimes Naive(动态树)
题意 \(n\) 个节点的树,每个点有点权,\(m\) 次操作,操作分两种,修改一个节点的点权,对于一个 \((u,v)\) ,询问 \(\displaystyle\sum_{i=1}^n\sum_{ ...
- 线性回归(linear regression)
基本形式 最小二乘法估计拟合参数 最小二乘法:基于均方误差最小化来进行模型求解的方法称为“最小二乘法”(least square method) 即(左边代表 $\mathbf{\omega }$ 和 ...
- MapReduce编程:单词去重
编程实现单词去重要用到NullWritable类型. NullWritable: NullWritable 是一种特殊的Writable 类型,由于它的序列化是零长度的,所以没有字节被写入流或从流中读 ...
- ubuntu 16.04 的IP地址变更
网上google 出来的,全是让你变更 /etc/network/interfaces 这个文件. 可是,我以前设置过的静态地址,全没反映在这个文件里. 这回再变更的话,肯定也不是这个. 然后进入/e ...
- 在ionic中使用短信验证码倒计时
页面上 <button class="code" (click)="getCode()" [disabled]="!verifyCode.dis ...
- spring El
package com.wisely.heighlight_spring4.ch2.el; import java.io.IOException; import org.apache.commons. ...
- get UI URL
DATA:LV_APPL_MODEL TYPE REF TO IF_BSP_WD_APPL_MODEL. DATA:RV_URL TYPE STRING. cl_bsp_wd_appl_ ...