【题解】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\). 蒟蒻猜了一个结论:只要满足对于所有缩出来的点的子 ...
随机推荐
- Google Chrome 浏览器的检索语言设置
解决为何从一开始安装Google Chrome 浏览器的时候,使用Google 搜索,结果都是日文的问题. 藏的比较隐蔽,没法在“设置”那里修改. 1.问题:搜索内容均是日文: 2.注意到右边有一个“ ...
- asp.net web系统开发浏览器和前端工具
1. Firefox浏览器+firebug插件 下载安装Firefox浏览器后,在菜单-附加组件-扩展中,搜索firebug,下载长得像甲虫一样的安装. 在web调试中,直接点击右上角的虫子,即可调出 ...
- es模板
Index Templatesedit Index templates allow you to define templates that will automatically be applied ...
- unity, get Canvas Scaler referenceResolution
需要using UnityEngine.UI; 然后就可以访问到CanvasScaler组件. float width=GetComponent<CanvasScaler> ().refe ...
- Mybatis学习-1(转自 csdn- http://my.csdn.net/hupanfeng 的文章)
简介 MyBatis的前身叫iBatis,本是apache的一个开源项目, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBa ...
- Redis_发布订阅(基础)
目录 前言 生产者和消费者 发布和订阅 Java实现 注意 前言 随着业务复杂, 业务的项目依赖关系增强, 使用消息队列帮助系统降低耦合度.发布订阅(pub/sub)是一种消息通信模式,主要目的是解除 ...
- 李洪强iOS开发之基于彻底解耦合的实验性iOS架构
基于彻底解耦合的实验性iOS架构 这周我决定做一个关于彻底解耦合的应用架构的实验.我想探究的主题是: “如果所有的应用内通讯都通过一个事件流来完成会怎么样?” 我构造了一个待办事项应用,因为这是我 ...
- Java 扫描包下所有类(包括jar包)
package com.MyUtils.file; [java] view plain copy import java.io.File; import java.io.FileFilter; imp ...
- [Android]通知栏与操作栏的高度-State Bar & Navigation Bar
1.通知栏 public static int getStatusBarHeight() { Resources resources = Resources.getSystem(); int reso ...
- mysql 索引优化,索引建立原则和不走索引的原因
第一:选择唯一性索引 唯一性索引的值是唯一的,可以更快捷的通过该索引来确定某条记录. 2.索引的列为where 后面经常作为条件的字段建立索引 如果某个字段经常作为查询条件,而且又有较少的重复列或者是 ...