传送门

首先可以证明一颗树合法的充分必要条件是不存在某个节点的度数为 $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的更多相关文章

  1. Codeforces Add on a Tree

    Add on a Tree time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  2. 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 题意 给出一棵树,每条边上有一个字符,字符集大小只 ...

  3. codeforces 812E Sagheer and Apple Tree(思维、nim博弈)

    codeforces 812E Sagheer and Apple Tree 题意 一棵带点权有根树,保证所有叶子节点到根的距离同奇偶. 每次可以选择一个点,把它的点权删除x,它的某个儿子的点权增加x ...

  4. codeforces 220 C. Game on Tree

    题目链接 codeforces 220 C. Game on Tree 题解 对于 1节点一定要选的 发现对于每个节点,被覆盖切选中其节点的概率为祖先个数分之一,也就是深度分之一 代码 #includ ...

  5. Codeforces E. Alyona and a tree(二分树上差分)

    题目描述: Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  6. 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 ...

  7. Codeforces 741D Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths(dsu on tree)

    感觉dsu on tree一定程度上还是与点分类似的.考虑求出跨过每个点的最长满足要求的路径,再对子树内取max即可. 重排后可以变成回文串相当于出现奇数次的字母不超过1个.考虑dsu on tree ...

  8. Codeforces.741D.Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths(dsu on tree 思路)

    题目链接 \(Description\) 给定一棵树,每条边上有一个字符(a~v).对每个节点,求它的子树中一条最长的路径,满足 路径上所有边上的字符可以重新排列成一个回文串.输出其最长长度. \(n ...

  9. Codeforces 379 F. New Year Tree

    \(>Codeforces \space 379 F. New Year Tree<\) 题目大意 : 有一棵有 \(4\) 个节点个树,有连边 \((1,2) (1,3) (1,4)\) ...

随机推荐

  1. IDEA出现Could not autowire. No beans of 'xxx' type found.解决

    Plan A File → Project Structure... Facets → Spring → 右键删除即可 Plan B File → Settings → Editor → Inspec ...

  2. 【软件工程】Beta冲刺(4/5)

    链接部分 队名:女生都队 组长博客: 博客链接 作业博客:博客链接 小组内容 恩泽(组长) 过去两天完成了哪些任务 描述 新增数据分析展示等功能API 服务器后端部署,API接口的beta版实现 展示 ...

  3. python 利用python的subprocess模块执行外部命令,获取返回值

    有时执行dos命令需要保存返回值 需要导入库subprocess import subprocess p = subprocess.Popen('ping www.baidu.com', shell= ...

  4. jenkins 基于角色的权限管理

    如何给不同的用户分配不同的项目权限呢,今天来介绍这个 1 (全局安全设置)启用角色->2新建用户->3新建jenkins 全局角色 builder  并分配如下图3中所示权限(并分配Ove ...

  5. chrome新版本flash无法在http网站上运行的解决办法

    最近遇到一个问题,就是用chrome浏览器打开网站后台以后,使用flash插件上传文件失败,提示flash初始化失败,于是打开chrome的内容设置,准备启用flash功能,打开浏览器,在地址栏中输入 ...

  6. known

    邻接表 https://blog.csdn.net/Violet_ljp/article/details/80556460 Dijkstra 算法实现原理 https://www.jianshu.co ...

  7. [Java复习] 集合框架 Collection

    Q1 Collection java的集合以及集合之间的继承关系? 数组和链表的区别? 固定长度,连续内存,不能扩展,随机访问快,插入删除慢.链表相反 List, Set, Map的区别? List, ...

  8. iOS UIBarButtonItem 固定尺寸

    自定义UIBarButtonItem的时候发现 有时候UIBarButtonItem尺寸会发生变化 通过以下方式可以很好的解决问题 [button.widthAnchor constraintEqua ...

  9. 64位win10系统无法安装.Net framework3.5的解决方法,提示无法连接internet

    1)网上有很多办法但是无法解决 2)控制面板>>疑难解答>>系统和安全性>>使用window更新解决问题 再次执行安装即可

  10. Spring Security在登录验证中增加额外数据(如验证码)

    在使用Spring Security框架过程中,经常会有这样的需求,即在登录验证时,附带增加额外的数据,如验证码.用户类型等.下面将介绍如何实现. 注:我的工程是在Spring Boot框架基础上的, ...