【题解】CF#611 H-New Year and Forgotten Tree
有趣啊~手玩一下这棵树,发现因为连边只对相连点的位数有限制,我们可以认为是在往一棵已经有 m 个结点的树上挂叶子结点直到满足要求。(m = log(10) n)。注意由于 m 超级无敌小,我们可以直接爆搜初始树,然后 dinic 二分图匹配即可。(网络流:一边的点表示限制,另一边的点表示位数。每一条限制可以删去一个节点, 检验一下是否能够删完即可)。
#include <bits/stdc++.h>
using namespace std;
#define maxn 300000
#define INF 9999999
int n, m, cal[maxn], num[maxn], cur[maxn];
int tot, mark[][], rec[][], lev[maxn];
int S, T, Q[maxn], d[maxn], deg[maxn];
char s1[maxn], s2[maxn];
priority_queue <int, vector <int>, greater <int> > q; int read()
{
int x = , k = ;
char c; c = getchar();
while(c < '' || c > '') { if(c == '-') k = -; c = getchar(); }
while(c >= '' && c <= '') x = x * + c - '', c = getchar();
return x * k;
} struct edge
{
int cnp, to[maxn], last[maxn], head[maxn], f[maxn], F[maxn];
edge() { cnp = ; }
void add(int u, int v, int fl)
{
// cout << "*****" << u << " " << v << " " << fl << endl;
to[cnp] = v, f[cnp] = F[cnp] = fl, last[cnp] = head[u], head[u] = cnp ++;
to[cnp] = u, f[cnp] = F[cnp] = , last[cnp] = head[v], head[v] = cnp ++;
}
}E1; struct node
{
int u, v;
}id[maxn]; bool bfs()
{
queue <int> q;
memset(lev, , sizeof(lev)); lev[S] = ;
q.push(S);
while(!q.empty())
{
int u = q.front(); q.pop();
for(int i = E1.head[u]; i; i = E1.last[i])
{
int v = E1.to[i];
if(E1.f[i] && !lev[v])
{
lev[v] = lev[u] + ;
q.push(v);
}
}
if(lev[T]) return ;
}
return ;
} int dfs(int u, int nf)
{
if(u == T || !nf) return nf;
int tf = ;
for(int i = E1.head[u]; i; i = E1.last[i])
{
int v = E1.to[i];
if(!E1.f[i] || lev[v] != lev[u] + ) continue;
int af = dfs(v, min(E1.f[i], nf));
tf += af, nf -= af;
E1.f[i] -= af, E1.f[i ^ ] += af;
}
return tf;
} int dinic()
{
int nf = ;
while(bfs()) nf += dfs(S, INF);
return nf;
} void dfs2(int u)
{
for(int i = E1.head[u]; i; i = E1.last[i])
{
int v = E1.to[i]; if(!E1.f[i ^ ]) continue;
if(u > m && u <= tot + m && v >= && v <= m)
for(int j = ; j <= E1.f[i ^ ]; j ++)
{
int t = u - m; t = (id[t].u == v) ? id[t].v : id[t].u;
printf("%d %d\n", num[t], cur[v] ++);
}
if(u == S) dfs2(v);
}
} void Search(int now)
{
if(now >= m - )
{
int t = ;
for(int i = ; i <= m; i ++) d[i] = deg[i];
for(int i = ; i <= m; i ++) if(!d[i]) q.push(i);
memset(mark, , sizeof(mark));
while(!q.empty() && t <= m - )
{
int u = q.top(); q.pop();
int x = u, y = Q[t]; if(x > y) swap(x, y);
mark[x][y] = ; d[Q[t]] --;
if(!d[Q[t]]) q.push(Q[t]); t ++;
}
if(q.size() >= )
{
int x = q.top(); q.pop(); int y = q.top(); q.pop();
mark[x][y] = ;
}else if(q.size() >= ) q.pop();
for(int i = ; i < E1.cnp; i ++) E1.f[i] = E1.F[i];
for(int i = E1.head[S]; i; i = E1.last[i])
{
int v = E1.to[i]; if(!v) continue; v -= m;
int x = id[v].u, y = id[v].v;
if(x > y) swap(x, y);
E1.f[i] -= mark[x][y];
if(E1.f[i] < ) return;
}
if(dinic() == n - m)
{
for(int i = ; i <= m; i ++)
for(int j = i + ; j <= m; j ++)
if(mark[i][j]) printf("%d %d\n", num[i], num[j]);
for(int i = ; i <= m; i ++) cur[i] ++;
dfs2(S);
exit();
}
return;
}
for(int i = ; i <= m; i ++)
deg[i] ++, Q[now] = i, Search(now + ), deg[i] --;
} int main()
{
n = read(); int t = n;
while(t) { m ++; t /= ; }
for(int i = , l = ; i <= m; l *= , i ++) num[i] = cur[i] = l;
for(int i = , l = ; i < m; l *= , i ++) cal[i] = l * - num[i];
cal[m] = n - num[m] + ; S = , T = m * m + m + ;
for(int i = ; i < n; i ++)
{
scanf("%s%s", s1 + , s2 + );
int l1 = strlen(s1 + ), l2 = strlen(s2 + );
if(l1 > l2) swap(l1, l2); rec[l1][l2] ++;
}
for(int i = ; i <= m; i ++)
for(int j = i; j <= m; j ++)
{
id[++ tot].u = i, id[tot].v = j;
E1.add(S, tot + m, rec[i][j]);
E1.add(tot + m, i, INF); E1.add(tot + m, j, INF);
}
for(int i = ; i <= m; i ++) E1.add(i, T, cal[i] - );
Search();
printf("-1\n");
return ;
}
【题解】CF#611 H-New Year and Forgotten Tree的更多相关文章
- 【题解】CF611H New Year and Forgotten Tree
[题解]CF611H New Year and Forgotten Tree 神题了... 题目描述 给定你一棵树,可是每个节点上的编号看不清了,只能辨别它的长度.现在用问号的个数代表每个节点编号那个 ...
- CF 1045 H. Self-exploration 解题报告
CF 1045 H. Self-exploration 考虑到串的结构一定是 1...0....1....0.....1... 这样的,而\(01\)与\(10\)在转折点交替出现 首先串长一定是\( ...
- 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 ...
- 【CodeForces】914 H. Ember and Storm's Tree Game 动态规划+排列组合
[题目]H. Ember and Storm's Tree Game [题意]Zsnuoの博客 [算法]动态规划+排列组合 [题解]题目本身其实并不难,但是大量干扰因素让题目显得很神秘. 参考:Zsn ...
- 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 ...
随机推荐
- Mybatis JPA-集成方案+源码
2018-04-18 update 当前文章已过时,请访问代码仓库查看当前版本wiki. github https://github.com/cnsvili/mybatis-jpa gitee htt ...
- 搜索引擎ElasticSearch系列(三): ElasticSearch2.4.4 bigdesk插件安装
一:ElasticSearch bigdesk插件简介 bigdesk是elasticsearch的一个集群监控工具,可以通过它来查看es集群的各种状态,如:cpu.内存使用情况,索引数据.搜索情况, ...
- dbtool一bug跟踪记
注:这篇日志是好多年前,我还在从兴公司时写的.现在都从从兴公司离职很久了,从兴也没落了,可惜.看了一下,虽然出现了部分代码,但不至于泄漏什么机密,查bug过程的原理也有可以让新手借鉴的地方,就原文照搬 ...
- 使用IntelRealScene设备结合Cocos引擎实现体感游戏开发
英特尔开发人员专区原文地址 Cocos游戏开发引擎对于广大开发者来说都比较熟悉,Intel RealScene是什么呢,简单理解是一种特殊的摄像头,可以捕捉用户的手势,面部表情等,进而实现AR,VR的 ...
- 阿里云服务器Centos上Apache安装SSL证书配置Https
首先我们先去阿里云申请一个免费的SSL证书(https://common-buy.aliyun.com/?spm=5176.7968328.1266638..5e971232BzMSp5&co ...
- 高可用Kubernetes集群-3. etcd高可用集群
五.部署高可用etcd集群 etcd是key-value存储(同zookeeper),在整个kubernetes集群中处于中心数据库地位,以集群的方式部署,可有效避免单点故障. 这里采用静态配置的方式 ...
- html的背景样式图片
背景图片 如果背景图片小于当前的div的情况下 默认的是将平铺充满元素 background-image 设置背景图片. background-repeat 设置是否及如何重复背景图片. repeat ...
- 【第八章】MySQL数据库备份—逻辑备份
一.数据库备份 1.命令简介: # mysqldump -h 服务器 -u用户名 -p密码 数据库名 > 备份文件.sql1)关于数据库名: -A, --all-databases ...
- Spring Task中的定时任务无法注入service的解决办法
1.问题 因一个项目(使用的是Spring+SpringMVC+hibernate框架)需要在spring task定时任务中调用数据库操作,在使用 @Autowired注入service时后台报错, ...
- java 乐观锁 vs 悲观锁
在数据库的锁机制中介绍过,数据库管理系统(DBMS)中的并发控制的任务是确保在多个事务同时存取数据库中同一数据时不破坏事务的隔离性和统一性以及数据库的统一性. 悲观锁其实就是 完全同步 比如 sync ...