New Roads CodeForces - 746G (树,构造)

大意:构造n结点树, 高度$i$的结点有$a_i$个, 且叶子有k个.

先确定主链, 然后贪心放其余节点.

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#include <bitset>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl ‘\n‘
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
//head

const int N = 1e6+10;
int n, tot, k, t;
int a[N], fa[N];
vector<int> g[N];

int main() {
	scanf("%d%d%d", &n, &t, &k);
	REP(i,1,t) scanf("%d", a+i);
	a[0] = 1;
	REP(i,0,t) REP(j,1,a[i]) g[i].pb(++tot);
	for (int x:g[1]) fa[x]=1;
	REP(i,2,t) fa[g[i][0]]=g[i-1][0];
	int res = n-k-t;
	if (res<0) return puts("-1"),0;
	REP(i,2,t) {
		REP(j,1,a[i]-1) {
			if (res&&j<=a[i-1]-1) {
				fa[g[i][j]] = g[i-1][j], --res;
			}
			else fa[g[i][j]] = g[i-1][0];
		}
	}
	if (res) return puts("-1"),0;
	printf("%d\n", n);
	REP(i,2,n) printf("%d %d\n", i,fa[i]);hr;
}

原文地址:https://www.cnblogs.com/uid001/p/10625345.html

时间: 2024-07-31 15:58:44

New Roads CodeForces - 746G (树,构造)的相关文章

Codeforces Round #423 (Div. 2) C 思维,并查集 或 线段树 D 树构造,水

Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) C. String Reconstruction   思维,并查集 或 线段树 题意:一个字符串被删除了,但给出 n条信息,要还原出可能的字典序最小的字符串.信息有:字符串ti,ki个位置xi,表明原本的字符串在xi位置是以字符串ti开头的. tags:惨遭 fst,一开始把所有字符串都存下来,排序做的,结果爆内存了.. 方法1: 考虑并查集,对于字符串 ti,在位置xi,

Fools and Roads CodeForces - 191C

Fools and Roads CodeForces - 191C 题意:给出一棵n个节点的树,还有树上的k条简单路径(用路径的两个端点u和v表示),求树上的各条边被这些简单路径经过的总次数. 方法: 一开始想了很久..想要在倍增求lca的同时统计边经过的次数..然而发现这样子可以统计,但是统计的值拆不开...没有办法在合适时间内得到答案...并没有思路.. 想了很久发现,这其实就是个简单的树上差分,只要记录一下每个节点i到根节点路径上所有边都需要加的权值sum[i]就行了. 对于每一组(u,v

CodeForces 26C Parquet 构造题

题目链接:点击打开链接 #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <math.h> #include <set> using namespace std; #define N 105 int n,m,a,b,c; char s[N][N]; set<char>myset; bool inm

根据中序遍历和后序遍历树构造二叉树

样例: 给出树的中序遍历: [1,2,3] 和后序遍历: [1,3,2] 返回如下的树: 2 /  \ 1   3 借鉴上一篇<前序遍历和中序遍历树构造二叉树>,我们知道中序遍历为左->中->右,后序遍历为左->右->中.于是后序遍历的最后一个值即为根节点的值,根据这个值我们在中序遍历中找到根节点左子树和右子树的值,递归构造左子树和右子树即可. /** * Definition of TreeNode: * class TreeNode { * public: * in

LintCode(72)中序遍历和后序遍历树构造二叉树

题目 中序遍历和后序遍历树构造二叉树 根据中序遍历和后序遍历树构造二叉树 样例 给出树的中序遍历: [1,2,3] 和后序遍历: [1,3,2] 返回如下的树: 2 /  \ 1    3 分析 递归解决. Python代码 """ Definition of TreeNode: class TreeNode: def __init__(self, val): self.val = val self.left, self.right = None, None "&q

72 中序遍历和后序遍历树构造二叉树

原题网址:https://www.lintcode.com/problem/construct-binary-tree-from-inorder-and-postorder-traversal/description 描述 根据中序遍历和后序遍历树构造二叉树 你可以假设树中不存在相同数值的节点 您在真实的面试中是否遇到过这个题?  是 样例 给出树的中序遍历: [1,2,3] 和后序遍历: [1,3,2] 返回如下的树: 2 /  \ 1    3 标签 二叉树 思路:要建立二叉树,首先要建立根

codeforces 671C Ultimate Weirdness of an Array 线段树+构造

题解上说的很清楚了,我照着写的,表示膜拜题解 然后时间复杂度我觉得应该是O(nlogn),虽然常数略大,预处理和倒着扫,都是O(nlogn) #include <stdio.h> #include <string.h> #include <algorithm> #include <math.h> #include <vector> using namespace std; typedef long long LL; const int N = 2

CodeForces - 343D 树链剖分

题目链接:http://codeforces.com/problemset/problem/343/D 题意:给定一棵n个n-1条边的树,起初所有节点权值为0,然后m个操作. 1 x:把x为根的子树的点的权值修改为1: 2 x:把x结点到根路径上的点修改为0: 3 x:查询结点x的值. 思路:树链剖分. 对于操作1,子树操作记录下当前点为根时,dfs的最后一个点的id是多少(endid[]),然后就可以把子树操作用区间来维护了. 对于操作2,树链剖分. 对于3操作,线段树单点查询. #defin

Codeforces Round #425 (Div. 2) Problem D Misha, Grisha and Underground (Codeforces 832D) - 树链剖分 - 树状数组

Misha and Grisha are funny boys, so they like to use new underground. The underground has n stations connected with n - 1 routes so that each route connects two stations, and it is possible to reach every station from any other. The boys decided to h