本文涉及知识点
P7807 魔力滋生
题目背景
Source:八仙敬酒,这是可以点的。
- 吕洞宾------醉酒提壶力千钧;
- 铁拐李------旋肘膝撞醉还真;
- 汉钟离------跌步抱坛兜心顶;
- 蓝采和------单提敬酒拦腰破;
- 张果老------醉酒抛杯踢连环;
- 曹国舅------仙人敬酒锁喉扣;
- 韩湘子------擒腕击胸醉吹箫;
- 何仙姑------弹腰献酒醉荡步。
题目描述
现有一个 n n n 个点的树 T T T,满足任意一个结点的所连接的结点个数不超过 2 2 2。
现在依次对结点 u = 1 ∼ n u=1\sim n u=1∼n 进行操作:
- 随机一个整数 x ( ≥ k ) x(\ge k) x(≥k);
- 新建 x x x 个结点,每个结点与 u u u 之间连一条边。
显然操作完成后仍是一棵树 T ′ T' T′,其结点数为 m = n + ∑ x m=n+\sum x m=n+∑x。
已知操作后的树 T ′ T' T′ 及其结点数 m m m,请还原原树 T T T,若有多种方案,输出 任意一组 使得 n \color{black}n n 最大 的。
值得注意的是,我们进行还原和输出时,只关心树的形状,而不关心结点的相对编号。
输入格式
第一行输入两个正整数 m , k m,k m,k,表示树 T ′ T' T′ 的结点数、随机数 x x x 的下限。
第 2 ∼ m 2\sim m 2∼m 行,每行输入两个正整数 u , v u,v u,v,表示树 T ′ T' T′ 中的一条边。
输出格式
第一行输出一个正整数 n n n,表示构造出树 T T T 的结点数。
第 2 ∼ n 2\sim n 2∼n 行,每行输出两个正整数 u , v u,v u,v,表示树 T T T 中的一条边。
输出的 u , v u,v u,v 必须满足: 1 ≤ u , v ≤ n 1\le u,v\le n 1≤u,v≤n。
输入输出样例 #1
输入 #1
5 1
1 2
1 3
1 4
1 5
输出 #1
1
输入输出样例 #2
输入 #2
7 0
1 2
1 3
1 4
1 5
1 6
1 7
输出 #2
3
1 2
1 3
输入输出样例 #3
输入 #3
9 1
1 2
2 3
1 4
1 5
2 6
2 7
3 8
3 9
输出 #3
3
1 2
2 3
说明/提示
样例说明
样例 # 1 \#1 #1 中,只有结点 1 1 1 可能在树 T T T 中:它对应的 x x x 是 4 4 4。
样例 # 2 \#2 #2 中,结点 1 , 2 , 3 1,2,3 1,2,3 在树 T T T 中:结点 1 1 1 对应的 x x x 是 4 4 4,结点 2 , 3 2,3 2,3 对应的 x x x 是 0 0 0。
样例 # 3 \#3 #3 中,结点 1 , 2 , 3 1,2,3 1,2,3 在树 T T T 中:它们随机的 x x x 均为 2 2 2。

样例 # 3 \#3 #3 给出一张示意图,图中红色结点表示树 T T T 中的结点,图中所有结点都在树 T ′ T' T′ 上。
数据范围
| Subtask | Score | x = x= x= |
|---|---|---|
| 1 1 1 | 30 30 30 | 0 0 0 |
| 2 2 2 | 30 30 30 | 1 1 1 |
| 3 3 3 | 40 40 40 | |
| 4 4 4 | 0 0 0 | Hack |
说明:Subtask4 为不计分 Hack 数据,只有通过全部的 Subtask 1 ∼ 4 1\sim4 1∼4 才算 AC。
对于 100 % 100\% 100% 的数据: 1 ≤ m ≤ 10 5 , k ∈ [ 0 , m ) 1\le m\le10^5,k\in[0,m) 1≤m≤105,k∈[0,m),数据输入保证有解。
后记
极光魔花好可爱 ∼ \sim ∼

树的直径 离散化
性质一 :度数<=2的树是链。
性质二 :1个节点的树,根节点度数为0。其它度数1或2。如果答案是0节点0边,改成1节点0边。
k > 0,除性质二外。度数1的节点是新加的。删除节点和对应的边。对节点离散化,并对边做响应修改。
k =0,树的直径就是初始链。树的直径,可以两轮BFS。
k等于0,也可以:
如果一条边的一个端点度数>2,一个端点度数为1,删除。
链的非端点加的边一定会被删除。链的端点加的边,会保留一条,其它删除。链的端点各加一条边,也是本题的解。
也可以不记录点数,只记录边数。点数=边数+1。
代码
核心代码
cpp
#include <iostream>
#include <sstream>
#include <vector>
#include<map>
#include<unordered_map>
#include<set>
#include<unordered_set>
#include<string>
#include<algorithm>
#include<functional>
#include<queue>
#include <stack>
#include<iomanip>
#include<numeric>
#include <math.h>
#include <climits>
#include<assert.h>
#include<cstring>
#include<list>
#include <bitset>
using namespace std;
template<class T1, class T2>
std::istream& operator >> (std::istream& in, pair<T1, T2>& pr) {
in >> pr.first >> pr.second;
return in;
}
template<class T1, class T2, class T3 >
std::istream& operator >> (std::istream& in, tuple<T1, T2, T3>& t) {
in >> get<0>(t) >> get<1>(t) >> get<2>(t);
return in;
}
template<class T1, class T2, class T3, class T4 >
std::istream& operator >> (std::istream& in, tuple<T1, T2, T3, T4>& t) {
in >> get<0>(t) >> get<1>(t) >> get<2>(t) >> get<3>(t);
return in;
}
template<class T = int>
vector<T> Read() {
int n;
cin >> n;
vector<T> ret(n);
for (int i = 0; i < n; i++) {
cin >> ret[i];
}
return ret;
}
template<class T = int>
vector<T> ReadNotNum() {
vector<T> ret;
T tmp;
while (cin >> tmp) {
ret.emplace_back(tmp);
if ('\n' == cin.get()) { break; }
}
return ret;
}
template<class T = int>
vector<T> Read(int n) {
vector<T> ret(n);
for (int i = 0; i < n; i++) {
cin >> ret[i];
}
return ret;
}
template<int N = 1'000'000>
class CInBuff
{
public:
inline CInBuff() {}
inline CInBuff<N>& operator>>(char& ch) {
FileToBuf();
ch = *S++;
return *this;
}
inline CInBuff<N>& operator>>(int& val) {
FileToBuf();
int x(0), f(0);
while (!isdigit(*S))
f |= (*S++ == '-');
while (isdigit(*S))
x = (x << 1) + (x << 3) + (*S++ ^ 48);
val = f ? -x : x; S++;//忽略空格换行
return *this;
}
inline CInBuff& operator>>(long long& val) {
FileToBuf();
long long x(0); int f(0);
while (!isdigit(*S))
f |= (*S++ == '-');
while (isdigit(*S))
x = (x << 1) + (x << 3) + (*S++ ^ 48);
val = f ? -x : x; S++;//忽略空格换行
return *this;
}
template<class T1, class T2>
inline CInBuff& operator>>(pair<T1, T2>& val) {
*this >> val.first >> val.second;
return *this;
}
template<class T1, class T2, class T3>
inline CInBuff& operator>>(tuple<T1, T2, T3>& val) {
*this >> get<0>(val) >> get<1>(val) >> get<2>(val);
return *this;
}
template<class T1, class T2, class T3, class T4>
inline CInBuff& operator>>(tuple<T1, T2, T3, T4>& val) {
*this >> get<0>(val) >> get<1>(val) >> get<2>(val) >> get<3>(val);
return *this;
}
template<class T = int>
inline CInBuff& operator>>(vector<T>& val) {
int n;
*this >> n;
val.resize(n);
for (int i = 0; i < n; i++) {
*this >> val[i];
}
return *this;
}
template<class T = int>
vector<T> Read(int n) {
vector<T> ret(n);
for (int i = 0; i < n; i++) {
*this >> ret[i];
}
return ret;
}
template<class T = int>
vector<T> Read() {
vector<T> ret;
*this >> ret;
return ret;
}
private:
inline void FileToBuf() {
const int canRead = m_iWritePos - (S - buffer);
if (canRead >= 100) { return; }
if (m_bFinish) { return; }
for (int i = 0; i < canRead; i++)
{
buffer[i] = S[i];//memcpy出错
}
m_iWritePos = canRead;
buffer[m_iWritePos] = 0;
S = buffer;
int readCnt = fread(buffer + m_iWritePos, 1, N - m_iWritePos, stdin);
if (readCnt <= 0) { m_bFinish = true; return; }
m_iWritePos += readCnt;
buffer[m_iWritePos] = 0;
S = buffer;
}
int m_iWritePos = 0; bool m_bFinish = false;
char buffer[N + 10], * S = buffer;
};
template<class T = int>
class CDiscretize //离散化
{
public:
CDiscretize(vector<T> nums)
{
sort(nums.begin(), nums.end());
nums.erase(std::unique(nums.begin(), nums.end()), nums.end());
m_nums = nums;
for (int i = 0; i < nums.size(); i++)
{
m_mValueToIndex[nums[i]] = i;
}
}
int operator[](const T value)const
{
auto it = m_mValueToIndex.find(value);
if (m_mValueToIndex.end() == it)
{
return -1;
}
return it->second;
}
int size()const
{
return m_mValueToIndex.size();
}
vector<T> m_nums;
protected:
unordered_map<T, int> m_mValueToIndex;
};
class Solution {
public:
vector<pair<int, int>> Ans(const int M,const int K,vector<pair<int,int>>& edge) {
const int iOtherNeed = (0 == K) ? 2 : 0;
vector<int> deg(M);
for (auto& [u, v] : edge) {
u--, v--;
deg[u]++; deg[v]++;
}
vector<pair<int, int>> nedge;
vector<int> ps;
for (auto& [u, v] : edge) {
if (deg[u] > deg[v]) { swap(u, v); }
if ((1 == deg[u]) && (deg[v] > iOtherNeed)) {
if (0 == K)
{
deg[u]--, deg[v]--;
}
continue;
}
nedge.emplace_back(u, v);
ps.emplace_back(u);
ps.emplace_back(v);
}
CDiscretize dis(ps);
for (auto& [u, v] : nedge) {
u = dis[u] + 1;
v = dis[v] + 1;
}
return nedge;
}
};
int main() {
#ifdef _DEBUG
freopen("a.in", "r", stdin);
#endif // DEBUG
ios::sync_with_stdio(0); cin.tie(nullptr);
CInBuff<> in;
int M,K;
in >> M >> K;
auto edge = in.Read<pair<int, int>>(M-1);
#ifdef _DEBUG
//printf("M=%d,K=%d", M,K);
//Out(edge, "edge");
//Out(d, "d=");
//Out(B, "B=");
//Out(strs2, ",strs2=");
//Out(que, ",que=");
/*Out(que, "que=");*/
#endif // DEBUG
auto res = Solution().Ans(M,K,edge);
cout << res.size()+1 << "\n";
for (const auto& [u, v] : res) {
cout << u << " " << v << "\n";
}
return 0;
}
单元测试
cpp
int M, K;
vector<pair<int, int>> edge;
TEST_METHOD(TestMethod01)
{
M = 2, K = 0, edge = { {1,2} };
auto res = Solution().Ans(M, K, edge);
AssertEx(1, (int)res.size());
}
TEST_METHOD(TestMethod02)
{
M = 2, K = 1, edge = { {1,2} };
auto res = Solution().Ans(M, K, edge);
AssertEx(0, (int)res.size());
}
TEST_METHOD(TestMethod03)
{
M = 4, K = 1, edge = { {1,2},{2,3},{3,4} };
auto res = Solution().Ans(M, K, edge);
AssertEx(1, (int)res.size());
}
TEST_METHOD(TestMethod11)
{
M = 7, K = 0,edge={ {1,2},{1,3},{1,4},{1,5},{1,6},{1,7} };
auto res = Solution().Ans(M,K,edge);
AssertEx(2, (int)res.size());
}
TEST_METHOD(TestMethod12)
{
M = 9, K = 1,edge={ {1,2},{2,3},{1,4},{1,5},{2,6},{2,7},{3,8},{3,9} };
auto res = Solution().Ans(M, K, edge);
AssertEx(2, (int)res.size());
}
扩展阅读
| 我想对大家说的话 |
|---|
| 工作中遇到的问题,可以按类别查阅鄙人的算法文章,请点击《算法与数据汇总》。 |
| 学习算法:按章节学习《喜缺全书算法册》,大量的题目和测试用例,打包下载。重视操作 |
| 有效学习:明确的目标 及时的反馈 拉伸区(难度合适) 专注 |
| 闻缺陷则喜(喜缺)是一个美好的愿望,早发现问题,早修改问题,给老板节约钱。 |
| 子墨子言之:事无终始,无务多业。也就是我们常说的专业的人做专业的事。 |
| 如果程序是一条龙,那算法就是他的是睛 |
| 失败+反思=成功 成功+反思=成功 |
视频课程
先学简单的课程,请移步CSDN学院,听白银讲师(也就是鄙人)的讲解。
https://edu.csdn.net/course/detail/38771
如何你想快速形成战斗了,为老板分忧,请学习C#入职培训、C++入职培训等课程
https://edu.csdn.net/lecturer/6176
测试环境
操作系统:win7 开发环境: VS2019 C++17
或者 操作系统:win10 开发环境: VS2022 C++17
如无特殊说明,本算法用**C++**实现。