传送门

首先可以证明一颗树合法的充分必要条件是不存在某个节点的度数为 $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. centos与debian网卡

    debian    /etc/network/interfaces # This file describes the network interfaces available on your sys ...

  2. 定时从linux获取信息放到windows上

    环境:windows上代码路径下存放:WinSCP-5.13.8-Setup.exe 第一步:test.txt   拉取脚本的txt文本 解析:存放从linux路径下拉取所需源文件zyy_count. ...

  3. 提交本地文件至gitlab已有的项目中(更新gitlab)

    gitlab代码更新 gitlab官网 1.安装git git官网 官网下载安装,安装过程一直next即可(路径自己选) 2.clone至本机 格式:git clone url(可转到指定目录克隆) ...

  4. 百度地图API--百度地图底色选择

    可选择底色列表<select id="stylelist" onchange="changeMapStyle(this.value)"> <o ...

  5. centos6安装sshpass

    跳转机需要装这个 #!/bin/bash yum -y install gcc-c++ openssh-clients curl -o sshpass.tar.gz http://sourceforg ...

  6. 微信小程序之状态管理A

    其实这个标题 不是很对 主要是最近小程序项目中 有这么一个状态 所有商品都共用一个商品详情页面  大概就是这样子  为了公司 保险起见,一些展示的内容已经处理 但是无伤大雅 就是这么两个按钮 左侧粉色 ...

  7. 加载自定义目录下的springmvc.xml配置文件

    在默认情况下:springmvc框架的配置文件必须叫<servlet-name>-servlet.xml 且必须放在/WEB-INF/目录下,我们可以在web.xml文件中,为Dispat ...

  8. mock的使用

    mock的重要性 mock就是对于某些不容易构造或者不容易获取的对象,用一个虚拟的对象来创建的方法.项目开发和测试过程中,遇到以下的情况时,就需要模拟结果返回. 1.当另一方接口或服务还未完成,阻碍项 ...

  9. Oracle CDC (Change Data Capture)更新数据捕获——Asynchronous HotLog Mode(附带简单的kettle任务实现数据同步)

    Performing Asynchronous HotLog Publishing Step 1   Source Database DBA: Set the database initializat ...

  10. centos7最小安装怎么安装防火墙

    CentOS 7.0默认使用的是firewall作为防火墙,需要事先关闭. 关闭firewall: 1 2 3 systemctl stop firewalld.service systemctl d ...