传送门

首先可以证明一颗树合法的充分必要条件是不存在某个节点的度数为 $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. [java]将秒数转化为“天时分秒”的格式(转贴+修改)

    public class Time { // format seconds to day hour minute seconds style // Exmplae 5000s will be form ...

  2. P3378 【模板】堆 (内含左偏树实现)

    P3378 [模板]堆 题解 其实就是一个小根堆啦,STL就可以解决,但是拥有闲情雅致的我学习了Jelly_Goat的左偏树,增加了代码长度,妙啊 Solution 1 STL STL 里面prior ...

  3. 用hugo搭建个人博客

    这几天研究了用hugo搭建个人博客. 简单的整理了一下. 1.安装hugo(windows 请查看官网介绍 https://gohugo.io/getting-started/installing/) ...

  4. leetcode312 戳气球

    动态规划 time O class Solution { public: int maxCoins(vector<int>& nums) { nums.insert(nums.be ...

  5. sed与awk

    sed 格式 sed 选项 控制命令 文件或标准输入 sed 流程: (循环打印) sed是将文件里的每一行读入模式空间进行操作, sed选项 -r 支持正则表达 -n 取消默认打印 清空当前模式空间 ...

  6. GIT管理以及运行规范

    继前天看分享的前后端分离后,又重新研究了GIT分支与各个环境的应用. 从开始使用git就一直有在网上查各种资料,查他的运行规范.但不知道是自己理解不够还是怎么的,一直用得不是很好. 根据自己的摸索,整 ...

  7. 006-数据结构-树形结构-二叉树、二叉查找树、平衡二叉查找树-AVL树

    一.概述 树其实就是不包含回路的连通无向图.树其实是范畴更广的图的特例. 树是一种数据结构,它是由n(n>=1)个有限节点组成一个具有层次关系的集合. 1.1.树的特性: 每个结点有零个或多个子 ...

  8. c++ STL之map

    map内部自建一颗红黑树(一 种非严格意义上的平衡二叉树),这颗树具有对数据自动排序的功能,所以在map内部所有的数据都是有序的,map中的元素是自动按Key升序排序,所以不能对map用sort函数: ...

  9. Maven 官网 查找&下载 jar包& pom引用 完美方案

    Maven 官网 查找&下载 jar包 & pom引用 问题描述 在我们在开发过程中,经常遇到程序中需要引用的某个版本jar包,但是在公司的私有仓库下载不到的情况. 遇到这种情况,该怎 ...

  10. 用R语言求置信区间

    用R语言求置信区间 用R语言求置信区间是很方便的,而且很灵活,至少我觉得比spss好多了. 如果你要求的只是95%的置信度的话,那么用一个很简单的命令就可以实现了 首先,输入da=c(你的数据,用英文 ...