【题解】CF611H New Year and Forgotten Tree
【题解】CF611H New Year and Forgotten Tree
神题了...
题目描述
给定你一棵树,可是每个节点上的编号看不清了,只能辨别它的长度。现在用问号的个数代表每个节点编号那个数字的长度,请你还原这一颗树,任意输出一个方案,有PSJ\(SPJ\)来检验你的正确性。无解输出一行\(-1\) 。
输入格式:
第一行一个整数\(n\)。
接下来 \(n-1\)行每行两个有问号构成的字符串,代表编号长度。
输出格式 :
若有解,直接输出\(n-1\)行,每行两个正整数,代表你还原的这颗树的边连接的两个节点。
若无解输出\(-1\)
solution_1:搜索套网络流
仔细思考一下,发现问号个数分别一样的边是本质相同的,我们把形如\((? ?\ , ??)\)这样的边先处理掉,直接连成一条链,然后把这条链看做一个点。
现在问题就变成了,给你五种不同颜色的点,相同颜色不连边,现在已知颜色两两相连的边的数目,请你构造一颗树出来。
怎么构造?可以每种颜色选定一个"关键点",先让关键点们生成一棵树,要求别的颜色连边过来必须连这一个点。我们抓出两种颜色来讨论,必须满足\(|a->b_{关键}|+|b->a_{关键}|=e[a][b]\),我们可以把所有的限制建模跑网络流,这样我们就知道从每种颜色连到另一种颜色的数目,就可以直接构造方案了。
但是有个问题,关键点之间生成的树对于答案是有影响的,所以还要先暴搜这(最多五个)点的连边情况,这个爆搜底数和指数都很小,可以当做常数。
然而我没写这种。
solution_2:Hall定理
hall定理:加入对于一个二分图存在完美匹配(已经把某一边匹配满了),设两边为\(X,Y,|X|\le|Y|\),那么在\(|X|\)中选出\(\forall k \in[0,|X|]\)个顶点,它向另一边连接的点数\(\ge k\)。
#include<bits/stdc++.h>
using namespace std;
inline int qr(){
char c=getchar();
register int ret=0,f=0;
while(not isdigit(c)) f|=c==45,c=getchar();
while(isdigit(c)) ret=ret*10+c-48,c=getchar();
return f?-ret:ret;
}
inline int qrqr(){
register char c=getchar();
register int ret=0;
while(c^'?')c=getchar();
while(c=='?')++ret,c=getchar();
return ret;
}
const int maxn=9;
int ten[maxn];
int e[maxn][maxn];
int cnt[maxn];
bool in[maxn];
#define pb(x,y) push_back(make_pair(x,y))
vector < pair< int , int > > ve;
int n,m;
inline bool chek(){
for(register int t=0,edd=(1<<m)-1;t<edd;++t){
int a(0),b(0);
for(register int i=0;i<m;++i)
if(t>>i&1) b+=cnt[i+1];
for(register int f=0;f<m;++f)
for(register int g=f;g<m;++g)
if((t>>f&1)|(t>>g&1))
a+=e[f+1][g+1];
if(a<b) return 0;
}
return 1;
}
int main(){
freopen("Manchester.in","r",stdin);
freopen("Manchester.out","w",stdout);
int sav=qr();
n=sav;
ten[1]=1;
for(register int t=2;t<=6;++t) ten[t]=ten[t-1]*10;
while(sav) sav/=10,++m;
for(register int t=1,t1,t2;t< n;++t){
t1=qrqr();t2=qrqr();
++e[t1][t2];
if(t1^t2)++e[t2][t1];
}
for(register int t=1;t<=5;++t) cnt[t]=ten[t+1]-ten[t];
cnt[m]=n-ten[m]+1;
if(not chek()) return puts("-1"),0;
--cnt[1],in[1]=1,++ten[1];
while(sav<n-1){
for(register int t=1;t<=m;++t){
if(in[t])
for(register int i=1;i<=m;++i){
if(!e[i][t] || !cnt[i]) continue;
--e[i][t],--cnt[i];
if(t^i) --e[t][i];
if(chek()){
ve.pb(ten[t]-1,ten[i]);
++ten[i];in[i]=1;++sav;
}
else {
++e[i][t],++cnt[i];
if(t^i) ++e[t][i];
}
}
}
}
for(auto t:ve)
printf("%d %d\n",t.first,t.second);
return 0;
}
【题解】CF611H New Year and Forgotten Tree的更多相关文章
- [cf611H]New Year and Forgotten Tree
首先,来构造这棵树的形态 称位数相同的点为一类点,从每一类点中任选一个点,具有以下性质: 1.每一类中选出的点的导出子图连通(是一颗树) 2.每一条边必然有一个端点属于某一类中选出的点 (关于&quo ...
- 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 ...
- 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 ...
- IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2) E - Bear and Forgotten Tree 2 链表
E - Bear and Forgotten Tree 2 思路:先不考虑1这个点,求有多少个连通块,每个连通块里有多少个点能和1连,这样就能确定1的度数的上下界. 求连通块用链表维护. #inclu ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 题解 CF611H 【New Year and Forgotten Tree】
Solution 提供一种新思路. 首先考虑如何判断一个状态是否合法. 考虑把所有十进制长度一样的数缩成一个点. 这样的点的个数 \(\le 5\). 蒟蒻猜了一个结论:只要满足对于所有缩出来的点的子 ...
随机推荐
- 利用pandas进行数据分析之一:pandas数据结构Series
Series是一种类似于一维数组的对象,又一组数据(各种Numpy数据类型)以及一组与之相关的数据标签(即是索引)组成. 可以将Series看成是一个定长的有序字段,因为它是索引值到数据值的一个映射. ...
- strults2标签s:set的用法
struts2标签<s:set></s:set>的用法.原理 http://liuna718-163-com.iteye.com/blog/1124654 我的例子: ...
- WebApi异常处理解决方案
一.使用异常筛选器捕获所有异常 首先在App_Start里面新建一个类WebApiExceptionFilterAttribute.cs,继承ExceptionFilterAttribute,重写On ...
- ubuntu14.04 flash driver 安装
直接将14.04镜像直接放到flash driver 中 然后在bios设置flash driver 优先启动 然后格出一块盘给ubuntu 安装使用 按操作要求一路点下去就可以,记得选windows ...
- Java 实现单链表反序
//单链表反序 public class SingleLinkedListReverse { public static void main(String[] args) { Node head = ...
- [转载]几个开源Javascript图形库
[转载]原文地址:http://www.cnblogs.com/webgis8/articles/1516639.html 因为Google Map项目的需要,最近一直在寻求相关的Javascript ...
- log4j日志写入数据库
# log4j写入数据库 ### 前言-----------------------------log4j是写入日志到控制台和文件很常见,但是写入到数据库不多见.做性能测试写入到数据库,统计方便些. ...
- Customize Netbeans Platform Splash Screen and About Dialog
原帖一直打不开,通过谷歌翻译找到的 http://blogs.kiyut.com/tonny/2007/10/18/customize-netbeans-platform-splash-screen- ...
- LOCAL_SHARED_LIBRARIES 与 LOCAL_LDLIBS,LOCAL_LDFLAGS的区别
LOCAL_LDLIBS :链接的库不产生依赖关系,一般用于不需要重新编译的库,如库不存在,则会报错找不到.且貌似只能链接那些存在于系统目录下本模块需要连接的库.如果某一个库既有动态库又有静态库,那么 ...
- boot2docker里报"no space left on device" error的解决方法
docker中pull远程image时:报 no space left on device virtualbox中调大虚拟内存即可.. 之前调的硬盘大小...