Solution

提供一种新思路。

首先考虑如何判断一个状态是否合法。

考虑把所有十进制长度一样的数缩成一个点。

这样的点的个数 \(\le 5\)。

蒟蒻猜了一个结论:只要满足对于所有缩出来的点的子集的点的个数 > 子集内边的个数,那么就是有解的。

这时 \(\tt \color{black}{S}\color{red}{egmentTree}\) 会下凡告诉你:这是对的!卡不掉!

但是这样只能判断可不可行啊,不能输出方案啊。。。

发现这个东西判断的时间复杂度很小,可以多次判断。

那么我们可以对于每一条边尝试一下可不可以删,然后连边(在原图上,让最后每一个长度只剩下一个点,剩下的特殊处理)。

Code

#include<bits/stdc++.h>
#define L(i, j, k) for(int i = j, i##E = k; i <= i##E; i++)
#define R(i, j, k) for(int i = j, i##E = k; i >= i##E; i--)
#define ll long long
#define db double
#define pii pair<int, int>
#define mkp make_pair
using namespace std;
const int N = 100;
const int M = 2e6 + 7;
const int inf = 1e9;
int n, m, mx, p[N], minn[N], D[M][2], sum = 0;
int a[N][N];
bool check() {
L(t, 0, (1 << mx) - 1) {
int ds = 0, bs = 0;
L(i, 1, mx) if(t & (1 << (i - 1))) ds += p[i];
if(!ds) continue;
L(i, 1, mx) if(t & (1 << (i - 1))) L(j, 1, mx) if(t & (1 << (j - 1))) bs += a[i][j];
if(bs >= ds) return 0;
}
return 1;
}
int Cnt(int x) { return x == 0 ? 0 : Cnt(x / 10) + 1; }
char sa[N], sb[N];
bool get() {
L(x, 1, mx) L(i, 1, mx) if(a[x][i]) {
if(p[x] > 1) {
a[x][i] --, p[x] --;
if(check()) return printf("%d %d\n", minn[x] + p[x], minn[i]), 1;
a[x][i] ++, p[x] ++;
}
if(p[i] > 1) {
a[x][i] --, p[i] --;
if(check()) return printf("%d %d\n", minn[x], minn[i] + p[i]), 1;
a[x][i] ++, p[i] ++;
}
}
return 0;
}
int main() {
scanf("%d", &n), mx = Cnt(n);
L(i, 1, n) p[Cnt(i)]++;
minn[1] = 1;
L(i, 2, mx) minn[i] = minn[i - 1] * 10;
L(i, 1, n - 1) scanf("%s%s", sa, sb), D[i][0] = strlen(sa), D[i][1] = strlen(sb), a[D[i][0]][D[i][1]] ++;
if(!check()) return puts("-1"), 0;
while(get()) sum ++;
L(x, 1, mx) L(i, 1, mx) if(a[x][i]) printf("%d %d\n", minn[x] + p[x] - 1, minn[i] + p[i] - 1), sum ++ ;
if(sum != n - 1) assert(0);
return 0;
}

祝大家学习愉快!


不过怎么证明那个结论是对的啊,蒟蒻思考了好久还是不懂,求解 /kel

题解 CF611H 【New Year and Forgotten Tree】的更多相关文章

  1. 【题解】CF611H New Year and Forgotten Tree

    [题解]CF611H New Year and Forgotten Tree 神题了... 题目描述 给定你一棵树,可是每个节点上的编号看不清了,只能辨别它的长度.现在用问号的个数代表每个节点编号那个 ...

  2. [cf611H]New Year and Forgotten Tree

    首先,来构造这棵树的形态 称位数相同的点为一类点,从每一类点中任选一个点,具有以下性质: 1.每一类中选出的点的导出子图连通(是一颗树) 2.每一条边必然有一个端点属于某一类中选出的点 (关于&quo ...

  3. VK Cup 2016 - Round 1 (Div. 2 Edition) C. Bear and Forgotten Tree 3 构造

    C. Bear and Forgotten Tree 3 题目连接: http://www.codeforces.com/contest/658/problem/C Description A tre ...

  4. IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2) E. Bear and Forgotten Tree 2 bfs set 反图的生成树

    E. Bear and Forgotten Tree 2 题目连接: http://www.codeforces.com/contest/653/problem/E Description A tre ...

  5. IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2) E - Bear and Forgotten Tree 2 链表

    E - Bear and Forgotten Tree 2 思路:先不考虑1这个点,求有多少个连通块,每个连通块里有多少个点能和1连,这样就能确定1的度数的上下界. 求连通块用链表维护. #inclu ...

  6. Code Forces Bear and Forgotten Tree 3 639B

    B. Bear and Forgotten Tree 3 time limit per test2 seconds memory limit per test256 megabytes inputst ...

  7. Codeforces 639B——Bear and Forgotten Tree 3——————【构造、树】

    Bear and Forgotten Tree 3 time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  8. codeforces 658C C. Bear and Forgotten Tree 3(tree+乱搞)

    题目链接: C. Bear and Forgotten Tree 3 time limit per test 2 seconds memory limit per test 256 megabytes ...

  9. VK Cup 2016 - Round 1 (Div. 2 Edition) C. Bear and Forgotten Tree 3

    C. Bear and Forgotten Tree 3 time limit per test 2 seconds memory limit per test 256 megabytes input ...

随机推荐

  1. ubutun 服务器中文设置

    在连接到服务器时,我们看不到图形界面,按照常规的安装搜狗输入法行不通,查阅相关的资料找到一篇可以解决的 sudo apt-get update 如果出现 E: 无法下载 404 Not Found I ...

  2. 熬夜肝出5大点,18张图带你彻底弄懂MySQL事务日志

    在当今社会,充斥着大量的数据.从众多APP上的账户资料到银行信用体系等个人档案,都离不开对大量数据的组织.存储和管理.而这,便是数据库存在的目的和价值.目前数据库的类型主要分为两种,一种是关系型数据库 ...

  3. 限流10万QPS、跨域、过滤器、令牌桶算法-网关Gateway内容都在这儿

    一.微服务网关Spring Cloud Gateway 1.1 导引 文中内容包含:微服务网关限流10万QPS.跨域.过滤器.令牌桶算法. 在构建微服务系统中,必不可少的技术就是网关了,从早期的Zuu ...

  4. 常见MFC函数

    1.MFC常用函数:WinExec()ExitWindowsEx()GlobalMemoryStatus()GetSystemInfo()GetSystemDirectory()GetWindowsD ...

  5. Mysql的下载,安装,远程连接,密码加密规则修改。

    第一次接触mysql,,很多地方不懂,出了很多问题.本来应该在Linux系统中安装mysql的,但是奈何各种电脑限制,所以在公司电脑的Windows service R2 系统上装了mysql数据库. ...

  6. php读取Excel文件数据

    首先先下载 PHPExcel 脚本之家下载地址:https://www.jb51.net/codes/194070.html 然后把文件丢到php脚本同级目录里面 在php文件里面写下以下代码即可 & ...

  7. ctf-misc-图片隐写术套路总结

    1.直接右键notepad打开,搜索flag,如果图片很多的话,可以写py脚本也    可以打开后搜索全部打开文件 2.是一个压缩包,改了后缀 3.图片中藏了一个二维码,用Stegsolve加几次滤镜 ...

  8. CleanMyMac X是如何进行Mac文件清理的

    Mac系统进行文件清理,一般是直接将文件拖动入"废纸篓"回收站中,然后通过清理回收站,就完成了一次文件清理的操作,但是这么做并无法保证文件被彻底删除了,有些文件通过一些安全恢复手段 ...

  9. Boom 3D支持的音乐都有什么格式

    Boom 3D作为一款专业的3D环绕音效软件,支持多种音频.视频播放格式,除了常用的MP3.WMA.WAV音频格式外,Boom 3D还支持FLAC.AAC这些比较高级的音频格式.同时,Boom 3D还 ...

  10. Jmeter测试TCP协议

    最近做了个项目是TCP协议传输数据,于是马上想试试JMeter测试TCP. 1首先需要下载第三方的TCP插件包ApacheJMeter_tcp.jar,下载步骤请自行百度. 2.下载完重新配置jmet ...