Codeforces Round #611 (Div. 3)——F. DIY Garland

Codeforces Round #611 (Div. 3)——F. DIY Garland

Polycarp has decided to decorate his room because the New Year is soon. One of the main decorations that Polycarp will install is the garland he is going to solder himself.

Simple garlands consisting of several lamps connected by one wire are too boring for Polycarp. He is going to solder a garland consisting of n lamps and n−1 wires. Exactly one lamp will be connected to power grid, and power will be transmitted from it to other lamps by the wires. Each wire connectes exactly two lamps; one lamp is called the main lamp for this wire (the one that gets power from some other wire and transmits it to this wire), the other one is called the auxiliary lamp (the one that gets power from this wire). Obviously, each lamp has at most one wire that brings power to it (and this lamp is the auxiliary lamp for this wire, and the main lamp for all other wires connected directly to it).

Each lamp has a brightness value associated with it, the i-th lamp has brightness 2i. We define the importance of the wire as the sum of brightness values over all lamps that become disconnected from the grid if the wire is cut (and all other wires are still working).

Polycarp has drawn the scheme of the garland he wants to make (the scheme depicts all n lamp and n−1 wires, and the lamp that will be connected directly to the grid is marked; the wires are placed in such a way that the power can be transmitted to each lamp). After that, Polycarp calculated the importance of each wire, enumerated them from 1 to n−1 in descending order of their importance, and then wrote the index of the main lamp for each wire (in the order from the first wire to the last one).

The following day Polycarp bought all required components of the garland and decided to solder it — but he could not find the scheme. Fortunately, Polycarp found the list of indices of main lamps for all wires. Can you help him restore the original scheme?

Input
The first line contains one integer n (2≤n≤2⋅105) — the number of lamps.

The second line contains n−1 integers a1, a2, …, an−1 (1≤ai≤n), where ai is the index of the main lamp for the i-th wire (wires are numbered in descending order of importance).

Output
If it is impossible to restore the original scheme, print one integer −1.

Otherwise print the scheme as follows. In the first line, print one integer k (1≤k≤n) — the index of the lamp that is connected to the power grid. Then print n−1 lines, each containing two integers xi and yi (1≤xi,yi≤n, xi≠yi) — the indices of the lamps connected by some wire. The descriptions of the wires (and the lamps connected by a wire) can be printed in any order. The printed description must correspond to a scheme of a garland such that Polycarp could have written the list a1, a2, …, an−1 from it. If there are multiple such schemes, output any of them.

Example
inputCopy
6
3 6 3 1 5
outputCopy
3
6 3
6 5
1 3
1 4
5 2
Note
The scheme for the first example (R denotes the lamp connected to the grid, the numbers on wires are their importance values):

在这里插入图片描述
题意: 给你边和边的父节点,边以这条边的子树的总权重来排序输出,每个点上的权值是2的i次方。然后让你推出原本的树是长什么样的。

思路: 我们可以给每个父节点一个深度,然后把所有叶子节点都入优先队列(小顶堆),把给你的边逆序处理,因为越后面的说明子树越小,最小的正好和堆顶的叶子节点匹配。然后把父节点的深度减一,如果深度为0,说明这时这个父节点也变成叶子节点了,就入队。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int deep[300007];
int tou[300007],wei[300007];
int fa[300007];
struct node
{
      int x;
       friend bool operator < (node a, node b)
      {
             return a.x > b.x; //结构体中,x小的优先级高
      }
};
priority_queue<node>s;
int main(){
	int n;
	cin>>n;
	for(int i=1;i<n;i++){
		cin>>fa[i];
		deep[fa[i]]++;
	}
	for(int i=1;i<=n;i++)
	{
		if(deep[i]==0){
			node xx;
			xx.x=i;
			s.push(xx);
		}
	}
	int cnt=n;
	for(int i=n-1;i>=1;i--)
	{
		node id=s.top();
		s.pop();
		wei[--cnt]=id.x;
		tou[cnt]=fa[i];
		deep[fa[i]]--;
		if(deep[fa[i]]==0){
			node a;
			a.x=fa[i];
			s.push(a);
		}
	}
	printf("%d\n",fa[1]);
	for(int i=1;i<n;i++){
		printf("%d %d\n",wei[i],tou[i]);
	}
	return 0;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值