302454: CF472D. Design Tutorial: Inverse the Problem

Memory Limit:256 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Design Tutorial: Inverse the Problem

题意翻译

给出一棵节点为n的带权树。计算它两两点对之间的距离很容易。现在给出“两两点对之间的距离”,问是否对应一棵树? n<=2000; 输入: 第一行:一个n。 第二至n-1行:n个两两点对之间的距离。 输出: 只有一行,输出YES或NO。

题目描述

There is an easy way to obtain a new task from an old one called "Inverse the problem": we give an output of the original task, and ask to generate an input, such that solution to the original problem will produce the output we provided. The hard task of Topcoder Open 2014 Round 2C, InverseRMQ, is a good example. Now let's create a task this way. We will use the task: you are given a tree, please calculate the distance between any pair of its nodes. Yes, it is very easy, but the inverse version is a bit harder: you are given an $ n×n $ distance matrix. Determine if it is the distance matrix of a weighted tree (all weights must be positive integers).

输入输出格式

输入格式


There is an easy way to obtain a new task from an old one called "Inverse the problem": we give an output of the original task, and ask to generate an input, such that solution to the original problem will produce the output we provided. The hard task of Topcoder Open 2014 Round 2C, InverseRMQ, is a good example. Now let's create a task this way. We will use the task: you are given a tree, please calculate the distance between any pair of its nodes. Yes, it is very easy, but the inverse version is a bit harder: you are given an $ n×n $ distance matrix. Determine if it is the distance matrix of a weighted tree (all weights must be positive integers).

输出格式


If there exists such a tree, output "YES", otherwise output "NO".

输入输出样例

输入样例 #1

3
0 2 7
2 0 9
7 9 0

输出样例 #1

YES

输入样例 #2

3
1 2 7
2 0 9
7 9 0

输出样例 #2

NO

输入样例 #3

3
0 2 2
7 0 9
7 9 0

输出样例 #3

NO

输入样例 #4

3
0 1 1
1 0 1
1 1 0

输出样例 #4

NO

输入样例 #5

2
0 0
0 0

输出样例 #5

NO

说明

There is an easy way to obtain a new task from an old one called "Inverse the problem": we give an output of the original task, and ask to generate an input, such that solution to the original problem will produce the output we provided. The hard task of Topcoder Open 2014 Round 2C, InverseRMQ, is a good example. Now let's create a task this way. We will use the task: you are given a tree, please calculate the distance between any pair of its nodes. Yes, it is very easy, but the inverse version is a bit harder: you are given an $ n×n $ distance matrix. Determine if it is the distance matrix of a weighted tree (all weights must be positive integers).

Input

题意翻译

给出一棵节点为n的带权树。计算它两两点对之间的距离很容易。现在给出“两两点对之间的距离”,问是否对应一棵树? n<=2000; 输入: 第一行:一个n。 第二至n-1行:n个两两点对之间的距离。 输出: 只有一行,输出YES或NO。

加入题单

算法标签: