【题解】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\). 蒟蒻猜了一个结论:只要满足对于所有缩出来的点的子 ...
随机推荐
- BASE64Decoder的引用
project---->properties--->Libraries--->JRE System Library--->Access rules--->Edit---& ...
- lucene 查询
csdn blog - Lucene 3.0 的Query Parser(查询语法) ibm developerWorks - 使用 Apache Lucene 2.4.1 搜索文本 osch ...
- eclipse日志
注意包别引入错: import org.slf4j.Logger; import org.slf4j.LoggerFactory; private static final Logger log = ...
- iOS SDK具体解释之NSCopying协议
原创blog,转载请注明出处 http://blog.csdn.net/hello_hwc?viewmode=contents 欢迎关注我的iOS SDK具体解释专栏 http://blog.csdn ...
- FPGA研发之道(25)-管脚
管脚是FPGA重要的资源之一,FPGA的管脚分别包括,电源管脚,普通I/O,配置管脚,时钟专用输入管脚GCLK等. 本文引用地址:http://www.eepw.com.cn/article/2664 ...
- gcc支持的标签
#include <stdio.h> #include <time.h> int main(/*int argc, char const *argv[]*/) { void * ...
- OCR 即 光学字符识别
OCR (Optical Character Recognition,光学字符识别)是指电子设备(例如扫描仪或数码相机)检查纸上打印的字符,通过检测暗.亮的模式确定其形状,然后用字符识别方法将形状翻译 ...
- 【算法拾遗(java描写叙述)】--- 插入排序(直接插入排序、希尔排序)
插入排序基本思想 每次将一个待排序的记录按其keyword大小插入到前面已经拍好序的子文件的适当位置,直到全部记录插入完毕为止. 直接插入排序 基本思想 直接插入排序的基本操作是将一个记录插入到已排好 ...
- 如何给unity3d工程加入依赖的android工程
最近在忙着接平台的事,需要接入各种各样的android平台sdk来发布.在接sdk的时候遇到了这样的一个情况,有点麻烦,所以纪录一下. 有些sdk的接入是提供jar包,这样的可以简单地将jar包制作成 ...
- [转]MVC设计模式
MVC模式(Model-View-Controller)是软件工程中的一种软件架构模式,把软件系统分为三个基本部分:模型(Model).视图(View)和控制器(Controller). MVC模式最 ...