A9456 | Tree and Array
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
User ainta likes trees. This time he is going to make an undirected tree with $n$ vertices numbered by integers from $1$ to $n$ . The tree is weighted, so each edge of the tree will have some integer weight.
Also he has an array $t$ : $t[1],t[2],...,t[n]$ . At first all the elements of the array are initialized to $0$ . Then for each edge connecting vertices $u$ and $v$ ( $u<v$ ) of the tree with weight $c$ , ainta adds value $c$ to the elements $t[u],t[u+1],...,t[v-1],t[v]$ of array $t$ .
Let's assume that $d(u,v)$ is the total weight of edges on the shortest path between vertex $u$ and vertex $v$ . User ainta calls a pair of integers $x,y$ ( $1<=x<y<=n$ ) good if and only if $d(x,y)=t[x]+t[x+1]+...+t[y-1]+t[y]$ .
User ainta wants to make at least  good pairs, but he couldn't make a proper tree. Help ainta to find such a tree.
Also he has an array $t$ : $t[1],t[2],...,t[n]$ . At first all the elements of the array are initialized to $0$ . Then for each edge connecting vertices $u$ and $v$ ( $u<v$ ) of the tree with weight $c$ , ainta adds value $c$ to the elements $t[u],t[u+1],...,t[v-1],t[v]$ of array $t$ .
Let's assume that $d(u,v)$ is the total weight of edges on the shortest path between vertex $u$ and vertex $v$ . User ainta calls a pair of integers $x,y$ ( $1<=x<y<=n$ ) good if and only if $d(x,y)=t[x]+t[x+1]+...+t[y-1]+t[y]$ .
User ainta wants to make at least  good pairs, but he couldn't make a proper tree. Help ainta to find such a tree.
输入格式
The first line contains a single integer $n$ ( $5<=n<=10^{5}$ ).
输出格式
Print $n-1$ lines containing the description of the edges. The $i$ -th line should contain three space-separated integers $u_{i},v_{i},c_{i}$ ( $1<=u_{i}<v_{i}<=n; 1<=c_{i}<=10^{5}$ ) — two vertices connected by the edge, and the weight of the edge.
Next print  lines containing the good pairs. The $k$ -th line should contain two space-separated integers $x_{k}$ and $y_{k}$ ( $1<=x_{k}<y_{k}<=n$ ). Of course, $x_{k},y_{k}$ must be a good pair. All pairs should be distinct — that is, for all $j,k$ , $x_{j}≠x_{k}$ or $y_{j}≠y_{k}$ must be satisfied.
If there are many correct solutions, print any of them.
Next print  lines containing the good pairs. The $k$ -th line should contain two space-separated integers $x_{k}$ and $y_{k}$ ( $1<=x_{k}<y_{k}<=n$ ). Of course, $x_{k},y_{k}$ must be a good pair. All pairs should be distinct — that is, for all $j,k$ , $x_{j}≠x_{k}$ or $y_{j}≠y_{k}$ must be satisfied.
If there are many correct solutions, print any of them.
输入输出样例
输入 #1
7
输出 #1
1 4 1 1 2 2 2 3 5 3 5 3 2 6 2 6 7 3 4 5 5 6 5 7
$⌊x⌋$ is the largest integer not greater than $x$ .
You can find the definition of a tree by the following link: http://en.wikipedia.org/wiki/Tree\_(graph\_theory)
You can also find the definition of the shortest path by the following link: http://en.wikipedia.org/wiki/Shortest\_path\_problem
The tree and the array $t$ in the sample output look like this:

You can find the definition of a tree by the following link: http://en.wikipedia.org/wiki/Tree\_(graph\_theory)
You can also find the definition of the shortest path by the following link: http://en.wikipedia.org/wiki/Shortest\_path\_problem
The tree and the array $t$ in the sample output look like this:

C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted