Codeforces 1188A 构造
题意:给你一颗树,树的边权都是偶数,并且边权各不相同。你可以选择树的两个叶子结点,并且把两个叶子结点之间的路径加上一个值(可以为负数),问是否可以通过这种操作构造出这颗树?如果可以,输出构造方案。初始树的边权都是0。
思路:A1很简单,只要判断是否有度数为2的点就可以了。对于A2, 由于边权各不相同,所以A1的结论同样适用。现在我们来构造一组答案。官方题解的构造方式是这样的:我们假设要让一个节点u到叶子结点v的路径都加上一个值x,并且知道叶子结点l1, l2都可以到达u,我们执行以下操作:v到l1的路径加上x / 2, v到l2的路径加上x / 2, l1 到 l2的路径加上-x / 2,这样除了u到v的路径,其它路径的值没有变(太菜了,想不到。。。)。那么,我们从树根开始,从上到下逐个构造边权即可。
由于n只有1000,所以实现方式有两种。
第一种很暴力,赋值操作直接暴力加,复杂度O(n ^ 2)。
代码:
#include <bits/stdc++.h>
#define pii pair<int, int>
#define LL long long
using namespace std;
const int maxn = 1010;
vector<pii> G[maxn];
vector<int> son[maxn];
LL add[maxn];
struct node {
int x, y;
LL z;
};
vector<node> ans;
int root = 1;
int f[maxn];
void adde(int x, int y, int z) {
G[x].push_back(make_pair(y, z));
G[y].push_back(make_pair(x, z));
}
int dfs(int x, int fa) {
f[x] = fa;
for (auto y : G[x]) {
if(y.first== fa) continue;
int tmp = dfs(y.first, x);
son[x].push_back(tmp);
}
if(G[x].size() == 1) {
return x;
}
return son[x][0];
}
void update(int x, int p, int val) {
while(x != p) {
add[x] += val;
x = f[x];
}
}
void dfs1(int x, int fa) {
int cnt = 0;
if(x == root) {
int y = G[x][0].first;
if(G[y].size() == 1) {
ans.push_back((node){x, y, G[x][0].second});
return;
}
LL tmp = G[x][0].second;
ans.push_back((node){son[y][0], root, tmp / 2});
ans.push_back((node){son[y][1], root, tmp / 2});
ans.push_back(node{son[y][0], son[y][1], -tmp / 2});
dfs1(y, x);
} else {
for (auto y : G[x]) {
if(y.first == fa) continue;
LL tmp = y.second - add[y.first];
int tmp1;
if(cnt == 0) tmp1 = 1;
else tmp1 = 0;
ans.push_back((node){son[x][cnt], root, tmp / 2});
ans.push_back((node){son[x][cnt], son[x][tmp1], tmp / 2});
ans.push_back(node{root, son[x][tmp1], -tmp / 2});
update(son[x][cnt], x, tmp);
dfs1(y.first, x);
cnt++;
}
}
}
int main() {
int n;
int x, y, z;
scanf("%d", &n);
for (int i = 1; i < n; i++) {
scanf("%d%d%d", &x, &y, &z);
adde(x, y, z);
}
for (int i = 1; i <= n; i++) {
if(G[i].size() == 2) {
printf("NO\n");
return 0;
}
}
for (int i = 1; i <= n; i++) {
if(G[i].size() == 1) {
root = i;
break;
}
}
dfs(root, -1);
dfs1(root, -1);
printf("YES\n");
printf("%d\n", ans.size());
for (int i = 0; i < ans.size(); i++) {
printf("%d %d %lld\n", ans[i].x, ans[i].y, ans[i].z);
}
}
第二种用了类似树剖中重儿子的思想,我们给一颗子树中决定一个优先级最高的叶子结点,这样加的操作是这个叶子结点到它的祖先的路径上进行的,其它的路径没有影响,这样累加影响的时候,如果这个叶子结点,把前面的影响累加上,否则不加。复杂度O(n)。
代码:
#include <bits/stdc++.h>
#define pii pair<int, int>
#define LL long long
using namespace std;
const int maxn = 1010;
vector<pii> G[maxn];
vector<int> son[maxn];
LL add[maxn];
struct node {
int x, y;
LL z;
};
vector<node> ans;
int root = 1;
int v[maxn];
void adde(int x, int y, int z) {
G[x].push_back(make_pair(y, z));
G[y].push_back(make_pair(x, z));
} int dfs(int x, int fa) {
for (auto y : G[x]) {
if(y.first== fa) continue;
int tmp = dfs(y.first, x);
son[x].push_back(tmp);
}
if(G[x].size() == 1) {
v[x] = x;
return x;
}
v[x] = son[x][0];
return son[x][0];
} void dfs1(int x, int fa, int tot) {
int cnt = 0;
if(x == root) {
int y = G[x][0].first;
if(G[y].size() == 1) {
ans.push_back((node){x, y, G[x][0].second});
return;
}
LL tmp = G[x][0].second;
ans.push_back((node){son[y][0], root, tmp / 2});
ans.push_back((node){son[y][1], root, tmp / 2});
ans.push_back(node{son[y][0], son[y][1], -tmp / 2});
dfs1(y, x, 0);
} else {
for (auto y : G[x]) {
if(y.first == fa) continue;
LL tmp = y.second;
if(v[y.first] == v[x]) tmp -= tot;
int tmp1;
if(cnt == 0) tmp1 = 1;
else tmp1 = 0;
ans.push_back((node){son[x][cnt], root, tmp / 2});
ans.push_back((node){son[x][cnt], son[x][tmp1], tmp / 2});
ans.push_back(node{root, son[x][tmp1], -tmp / 2});
dfs1(y.first, x, y.second);
cnt++;
}
}
} int main() {
int n;
int x, y, z;
scanf("%d", &n);
for (int i = 1; i < n; i++) {
scanf("%d%d%d", &x, &y, &z);
adde(x, y, z);
}
for (int i = 1; i <= n; i++) {
if(G[i].size() == 2) {
printf("NO\n");
return 0;
}
}
for (int i = 1; i <= n; i++) {
if(G[i].size() == 1) {
root = i;
break;
}
}
dfs(root, -1);
dfs1(root, -1, 0);
printf("YES\n");
printf("%d\n", ans.size());
for (int i = 0; i < ans.size(); i++) {
printf("%d %d %lld\n", ans[i].x, ans[i].y, ans[i].z);
}
}
两份代码中为了实现方便,都找了一个度为1的点为根。
Codeforces 1188A 构造的更多相关文章
- B - Save the problem! CodeForces - 867B 构造题
B - Save the problem! CodeForces - 867B 这个题目还是很简单的,很明显是一个构造题,但是早训的时候脑子有点糊涂,想到了用1 2 来构造, 但是去算这个数的时候算错 ...
- Johnny Solving CodeForces - 1103C (构造,图论)
大意: 无向图, 无重边自环, 每个点度数>=3, 要求完成下面任意一个任务 找一条结点数不少于n/k的简单路径 找k个简单环, 每个环结点数小于n/k, 且不为3的倍数, 且每个环有一个特殊点 ...
- Codeforces 746G(构造)
G. ...
- C - Long Beautiful Integer codeforces 1269C 构造
题解: 这里的m一定是等于n的,n为数最大为n个9,这n个9一定满足条件,根据题目意思,前k个一定是和原序列前k个相等,因此如果说我们构造出来的大于等于原序列,直接输出就可以了,否则,由于后m-k个一 ...
- Dividing the numbers CodeForces - 899C (构造)
大意: 求将[1,n]划分成两个集合, 且两集合的和的差尽量小. 和/2为偶数最小差一定为0, 和/2为奇数一定为1. 显然可以通过某个前缀和删去一个数得到. #include <iostrea ...
- Codeforces 772C 构造 数学 + dp + exgcd
首先我们能注意到两个数x, y (0 < x , y < m) 乘以倍数互相可达当且仅当gcd(x, m) == gcd(y, m) 然后我们可以发现我们让gcd(x, m)从1开始出发走 ...
- Jzzhu and Apples CodeForces - 449C (构造,数学)
大意: 求从[1,n]范围选择尽量多的数对, 使得每对数的gcd>1 考虑所有除2以外且不超过n/2的素数p, 若p倍数可以选择的有偶数个, 直接全部划分即可 有奇数个的话, 余下一个2*p不划 ...
- Gluttony CodeForces - 892D (构造,思维)
题面: You are given an array a with n distinct integers. Construct an array b by permuting a such that ...
- Necklace CodeForces - 613C (构造)
链接 大意: 给定n种珠子, 第i种有$a_i$个, 求将珠子穿成项链, 使得能使切开后的项链回文的切口尽量多 若有一种以上珠子为奇数, 显然不能为回文, 否则最大值一定是$gcd(a_1,a_2,. ...
随机推荐
- SQL必知必会学习笔记
2.5 select SELECT 要返回的列或表达式 是FROM 从中检索数据的表 仅在从表选择数据时使用WHERE 行级过滤 ...
- NetCore中的环境变量的值取自于哪里?
环境 操作系统 win10 IIS 10 net core 2.2 ,net core 3.0 分别生成了三个环境变量的配置文件: 以及测试代码: public void Configure(IApp ...
- 【leetcode】1017. Convert to Base -2
题目如下: Given a number N, return a string consisting of "0"s and "1"s that represe ...
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 GSM Base Station Identification (点在多边形内模板)
In the Personal Communication Service systems such as GSM (Global System for Mobile Communications), ...
- S1 Python 基础
定义规范 声明变量 name = "Alex Li" 变量定义规则 变量名只能是 字母.数字或下划线的任意组合 变量名的第一个字符不能是数字 以下关键字不能声明为变量名['and' ...
- C# 实现软件注册功能
相信很多初学编程的人都会对这个注册功能很感兴趣,我也不例外,刚学asp.net时,竞找不到这方面的实例,结果自己参考微软的一些文档自己做了一个,其实我做的这个注册功能很简单,读取计算机的CPU序列号, ...
- 一片关于Bootstarp4的文章
一.Bootstarp Bootstrap 是全球最受欢迎的前端组件库,用于开发响应式布局.移动设备优先的 WEB 项目.可以让你快速的排版,不用在写那些繁杂的样式.Bootstrap是开源免费的,设 ...
- xunit的assert常用部分方法解释
布尔:True(返回bool的方法或者字段) 判断是否为trueFalse(返回bool的方法或者字段) 判断是否为false 字符串(区分大小写):Equal(期待结果,实际结果) 判断输出值和期待 ...
- ExecutorException: A query was run and no Result Maps were found for the Mapped Statement ‘com.win.mall.dao.CartMapper.test’. It’s likely that neither a Result Type nor a Result Map was specified.
ExecutorException: A query was run and no Result Maps were found for the Mapped Statement 'com.win.m ...
- Java学习之多态---类成员变化
类成员 一.成员变量 编译时:变量(f)所属类(Fu)中是否有成员变量,有:编译成功,没有:编译失败 运行时:变量(f)所属类(Fu)中是否有成员变量,运行该类(Fu)中的成员变量 class Fu ...