1003E. Tree Constructing(构造)

You are given three integers n, d and k

.

Your task is to construct an undirected tree on n

vertices with diameter d and degree of each vertex at most k

, or say that it is impossible.

An undirected tree is a connected undirected graph with n1

edges.

Diameter of a tree is the maximum length of a simple path (a path in which each vertex appears at most once) between all pairs of vertices of this tree.

Degree of a vertex is the number of edges incident to this vertex (i.e. for a vertex u

it is the number of edges (u,v) that belong to the tree, where v

is any other vertex of a tree).

Input

The first line of the input contains three integers n

, d and k ( 1n,d,k4105

).

Output

If there is no tree satisfying the conditions above, print only one word "NO" (without quotes).

Otherwise in the first line print "YES" (without quotes), and then print n1

lines describing edges of a tree satisfying the conditions above. Vertices of the tree must be numbered from 1 to n

. You can print edges and vertices connected by an edge in any order. If there are multiple answers, print any of them.1

Examples
Input
Copy
6 3 3
Output
Copy
YES
3 1
4 1
1 2
5 2
2 6
Input
Copy
6 2 3
Output
Copy
NO
Input
Copy
10 4 3
Output
Copy
YES
2 9
2 10
10 3
3 1
6 10
8 2
4 3
5 6
6 7
Input
Copy
8 5 3
Output
Copy
YES
2 5
7 2
3 7
3 1
1 6
8 7
4 3

题意

让你构造一个节点数为n,直径为d,且所有点的度数均小于k的树。

题解

我们先用1~(n+1)把直径做出来,然后用dfs去添加其他结点,我们可以在第i个结点去挂上一棵深度不超过min(i-1,d-i+1)的树,注意在生成这棵树的时候每个结点的度数不能超过k。总结点数==n时,及时结束。

那么什么时候无解呢,①节点数小于等于直径(这样直径都搞不出来),②直径>1且每个点的度数不能超过2,③n个结点没有全部挂上去。

#include<cstdio>
#include<cstring>
#include<vector>
#include<iostream>
using namespace std;
int n,d,k;
int id;
vector< pair<int,int> >ans;
void DFS(int u,int dep,int maxd){
	if(dep==maxd) return;
	for(int i=0;i<k-1-(dep==0) && id<n;i++){
		ans.push_back(make_pair(u,++id));
		DFS(id,dep+1,maxd);
	}
}
int main(){
	cin>>n>>d>>k;
	if(n<=d||d>1&&k<2){
		cout<<"NO"<<endl;
		return 0;
	}
	for(int i=1;i<=d;i++)
	   ans.push_back(make_pair(i,i+1));
	id=d+1;
	for(int i=2;i<=d;i++) DFS(i,0,min(i-1,d-i+1));
	if(id<n) cout<<"NO"<<endl;
	else{
		cout<<"YES"<<endl;
		for(int i=0;i<ans.size();i++)
		   cout<<ans[i].first<<" "<<ans[i].second<<endl;
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
The error message you're encountering is from a Linux package manager, typically APT (Advanced Package Tool) which is used by Ubuntu and its derivatives, while `yum` is commonly associated with the RPM (Red Hat Package Manager) system found in CentOS, Fedora, or RHEL. The error "E: Unable to locate package yum" indicates that the system is looking for a package called `yum`, but it can't find it in the repository. Here's what's happening: - `Reading package lists... Done` means APT is gathering information about available packages. - `Building dependency tree... Done` follows by constructing the list of dependencies for the requested package(s). - `Reading state information... Done` checks if the necessary packages are installed or if there are any issues. The problem arises because `yum` is not a package managed by APT; it's a separate package manager used mainly on Red Hat-based systems. To resolve this issue on an Ubuntu system: 1. **Identify the appropriate package manager:** Since you're using APT, you need to install software through it, not `yum`. 2. **Install missing package (if applicable):** If you're trying to install something that should be installed via APT, make sure you're using apt-get or apt instead. 3. **Update the repositories:** Run `sudo apt update` to ensure your package list is up-to-date. 4. **Install a Red Hat package manager (optional):** If you actually need `yum`, consider installing `apt-transport-https` and `epel-release` for accessing the Extra Packages for Enterprise Linux (EPEL) repository, which may have `yum`. Then use `sudo apt install yum` or `sudo yum install yum`.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值