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. inkscope完整安装配置

    准备centos7基础系统 首先安装基础系统centos7 在安装选项那里选择base web server ,选择其他的也可以,选择mini安装会缺很多常用的软件包,后续需要一个个安装比较麻烦 关闭 ...

  2. PHP 使用gd库压缩图片并生成新图片返回全路径

    使用gd库前,先检查是否开启了gd库 phpinfo(); 如果在php配置信息里找到了gd库,那么就是开启了,没开启的自行百度如何开启php gd库 既然开启了gd库,那么直接再php里写入以下代码 ...

  3. Java8用了这么久了,Stream 流用法及语法你都知道吗?

    1.简介 Stream流 最全的用法Stream 能用来干什么?用来处理集合,通过 使用Stream API 对集合数据进行操作,就类似于使用 SQL 执行的数据库查询,Stream API 提供了一 ...

  4. .NET 5 ORM 八大实用技巧 干货 - SqlSugar ORM

    介绍 sqlsugar已经在第一时间完美兼容.NET5并且已经有人在使用了, 很多人都担心用了开源框架遇到问题无法解决,导致前功尽弃,使用SqlSugar你大可放心,除了有详细文档和几年的大量用户积累 ...

  5. Jmeter(二十七) - 从入门到精通 - Jmeter Http协议录制脚本(详解教程)

    1.简介 LoadRunner的录制功能让性能测试脚本编写对于不懂代码的人变成了一件容易上手的事,但是由于LoadRunner收费高昂,庞大,一般企业很少用,除非必须使用.Jmeter作为性能测试中的 ...

  6. linkedin第三方登陆

    到linkedin注册成为开发者:https://developer.linkedin.com/zh-cn 成功后,创建应用,填写重定向地址,得到appid和appkey 使用这个sdk https: ...

  7. js 表格上checkbox 全选

    <table class="layui-table"> <thead> <tr> <th width="75"> ...

  8. {"non_field_errors":["Unable to log in with provided credentials."]}% 无法使用提供的凭据登录

    在使用rest_framework_jwt进行登陆验证获取token的时候会报 {"non_field_errors":["Unable to log in with p ...

  9. (1)Hello World

    语出<论语·卫灵公>:子贡问为仁.子曰:"工欲善其事,必先利其器.居是邦也,事其大夫之贤者,友其士之仁者." 2020年11月终于下定决心开始 Visual C++ 的 ...

  10. 3、Spring Cloud Rest工程创建(通过IDEA创建)

    1.Rest微服务构建简介 (1).介绍 以Dept部门模块做一个微服务通用案例Consumer消费者(Client)通过REST调用Provider提供者(Server)提供的服务. (2).Myb ...