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;
}

 

上一篇:codeforces982F


下一篇:【CF1141G】Privatization of Roads in Treeland