Codeforces 1189D2. Add on a Tree: Revolution
首先可以证明一颗树合法的充分必要条件是不存在某个节点的度数为 $2$
首先它是必要的,考虑任意一条边连接的两点如果存在某一点 $x$ 度数为 $2$ ,那么说明 $x$ 还有连一条边出去,那么连出去的那一条边和当前边的权值就永远一样
然后它是充分的,考虑进行如下操作:首先设当前边连接的两端分别为 $L,R$ ,如果 $L,R$ 只要存在某一个度数为 $1$ 显然合法
然后考虑 $L,R$ 度数不为 $1$ 的情况,那么 $L,R$ 一定分别存在两个儿子连向叶子,设为 $Lx,Ly,Rx,Ry$
设当前边的目标权值为 $x$ ,那么只要操作: $(Lx,Rx,x/2),(Ly,Ry,x/2),(Lx,Ly,-x),(Rx,Ry,-x)$ 即可,由于 $x$ 为偶数,所以 $x/2$ 一定为整数
因为这一题可以 $n^2$ ,所以直接按上面描述模拟即可
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
using namespace std;
typedef long long ll;
inline int read()
{
int x=,f=; char ch=getchar();
while(ch<''||ch>'') { if(ch=='-') f=-; ch=getchar(); }
while(ch>=''&&ch<='') { x=(x<<)+(x<<)+(ch^); ch=getchar(); }
return x*f;
}
const int N=1e5+;
int n,a[N],b[N],c[N],du[N];
int fir[N],from[N<<],to[N<<],cntt;
inline void add(int a,int b) { from[++cntt]=fir[a]; fir[a]=cntt; to[cntt]=b; }
int find(int x,int fa)
{
if(du[x]==) return x;
for(int i=fir[x];i;i=from[i])
if(to[i]!=fa) return find(to[i],x);
}
struct dat {
int x,y,z;
dat (int _x=,int _y=,int _z=) { x=_x,y=_y,z=_z; }
};
vector <dat> ans;
int main()
{
n=read();
for(int i=;i<n;i++)
{
a[i]=read(),b[i]=read(); c[i]=read();
add(a[i],b[i]); add(b[i],a[i]);
du[a[i]]++,du[b[i]]++;
}
if(n==) { printf("YES\n1\n1 2 %d\n",c[]); return ; }
for(int i=;i<n;i++)
{
if(du[a[i]]==||du[b[i]]==) { printf("NO\n"); return ; }
if(du[b[i]]==) swap(a[i],b[i]);
vector <int> L,R;
for(int j=fir[a[i]];j;j=from[j])
if(to[j]!=b[i]) L.push_back(find(to[j],a[i]));
for(int j=fir[b[i]];j;j=from[j])
if(to[j]!=a[i]) R.push_back(find(to[j],b[i]));
if(L.size()==)
ans.push_back(dat(a[i],R[],c[i]/)),
ans.push_back(dat(a[i],R[],c[i]/)),
ans.push_back(dat(R[],R[],-c[i]/));
else
ans.push_back(dat(L[],R[],c[i]/)),
ans.push_back(dat(L[],R[],c[i]/)),
ans.push_back(dat(L[],L[],-c[i]/)),
ans.push_back(dat(R[],R[],-c[i]/));
}
printf("YES\n%d\n",int(ans.size()));
for(auto A: ans) printf("%d %d %d\n",A.x,A.y,A.z);
return ;
}
Codeforces 1189D2. Add on a Tree: Revolution的更多相关文章
- Codeforces Add on a Tree
Add on a Tree time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- codeforces 741D Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths(启发式合并)
codeforces 741D Arpa's letter-marked tree and Mehrdad's Dokhtar-kosh paths 题意 给出一棵树,每条边上有一个字符,字符集大小只 ...
- codeforces 812E Sagheer and Apple Tree(思维、nim博弈)
codeforces 812E Sagheer and Apple Tree 题意 一棵带点权有根树,保证所有叶子节点到根的距离同奇偶. 每次可以选择一个点,把它的点权删除x,它的某个儿子的点权增加x ...
- codeforces 220 C. Game on Tree
题目链接 codeforces 220 C. Game on Tree 题解 对于 1节点一定要选的 发现对于每个节点,被覆盖切选中其节点的概率为祖先个数分之一,也就是深度分之一 代码 #includ ...
- Codeforces E. Alyona and a tree(二分树上差分)
题目描述: Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- codeforces 842C Ilya And The Tree
Ilya is very fond of graphs, especially trees. During his last trip to the forest Ilya found a very ...
- Codeforces 741D Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths(dsu on tree)
感觉dsu on tree一定程度上还是与点分类似的.考虑求出跨过每个点的最长满足要求的路径,再对子树内取max即可. 重排后可以变成回文串相当于出现奇数次的字母不超过1个.考虑dsu on tree ...
- Codeforces.741D.Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths(dsu on tree 思路)
题目链接 \(Description\) 给定一棵树,每条边上有一个字符(a~v).对每个节点,求它的子树中一条最长的路径,满足 路径上所有边上的字符可以重新排列成一个回文串.输出其最长长度. \(n ...
- Codeforces 379 F. New Year Tree
\(>Codeforces \space 379 F. New Year Tree<\) 题目大意 : 有一棵有 \(4\) 个节点个树,有连边 \((1,2) (1,3) (1,4)\) ...
随机推荐
- 【Redis 设置Redis使用LRU算法】
转自:http://ifeve.com/redis-lru/ 本文将介绍Redis在生产环境中使用的Redis的LRU策略,以及自己动手实现的LRU算法(php) 1.设置Redis使用LRU算法 L ...
- openapi and light-4j
light-4j项目支持openapi规范,本文介绍一下参照相关demo做的上传功能. openapi.yaml,按照规范编写内容,/openapi/swagger可以查看对应的swagger页面,A ...
- JS基础_数组的方法
常用的方法 1.push:向数组的末尾添加一个或更多元素,并返回新的长度. 将要添加的元素作为方法的参数传递,这些元素将会自动添加到数组的末尾 var a=[1,2,3]; var r = a.pus ...
- 20191121-5 Scrum立会报告+燃尽图 02
此作业要求参见https://edu.cnblogs.com/campus/nenu/2019fall/homework/10066 一.小组情况 组长:贺敬文组员:彭思雨 王志文 位军营 徐丽君队名 ...
- ES节点分析
ES集群概念 集群 一个集群cluster由一个或者多个节点组成,具有相同的cluster.name,协同工作,分项数据和负载. 当有新的节点加入或者删除了一个节点时,集群回感知到并能够平衡数据. E ...
- antd源码分析之——标签页(tabs 1.组件结构)
由于ant Tabs组件结构较复杂,共分三部分叙述,本文为目录中第一部分(高亮) 目录 一.组件结构 antd代码结构 rc-ant代码结构 1.组件树状结构 2.Context使用说明 3.rc-t ...
- R语言:怎么进行异常检测
a <- try(as.Date('2017-02-30'),silent = T) 当silent为F是,错误消息还是会返回 怎么检测a是否出错呢:if('try-error' %in% cl ...
- Python - 排序( 插入, 冒泡, 快速, 二分 )
插入排序 算法分析 两次循环, 大循环对队列中的每一个元素拿出来作为小循环的裁定对象 小循环对堆当前循环对象在有序队列中寻找插入的位置 性能参数 空间复杂度 O(1) 时间复杂度 O(n^2) 详细代 ...
- java IO流的API
常用的IO流API有:[InputStream.OutputStream] [FileInputStream.FileOutputStream] [BufferedInputStream.Buffer ...
- Laplacian eigenmap 拉普拉斯特征映射
下面是实验室大牛师兄自己写的一段总结,主要内容是Laplacian Eigenmap中的核心推导过程. 有空还是多点向这位师兄请教,每次都会捡到不少金子. Reference : <Laplac ...