【题解】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 ...
随机推荐
- OpenStack入门篇(七)之认证服务Keystone
一.Keystone的概述 Keystone是Openstack的组件之一,用于为Openstack家族中的其它组件成员提供统一的认证服务,包括身份验证,令牌的发放和校验,服务列表,用户权限的定义等. ...
- 有关Laravel 4 的 Homestead 安装部署的细节
对于Vagrant,我是相见恨晚的.有时候抽出几个小时的时间学会一种工具,对于将来可以节省几十甚至几百小时的时间. Vagant最大的好处就是节省了安装配置运行环境的时间,统一开发环境,同时可以最大限 ...
- Eclipse将Java项目打成jar工具包
jar包:就是别人已经写好的一些类,然后将这些类进行打包,你可以将这些jar包引入你的项目中,然后就可以直接使用这些jar包中的类和属性以及方法. jar包可分为可执行jar包和jar工具包,在这里, ...
- 433. Number of Islands【LintCode java】
Description Given a boolean 2D matrix, 0 is represented as the sea, 1 is represented as the island. ...
- CSP201512-2:消除类游戏
引言:CSP(http://www.cspro.org/lead/application/ccf/login.jsp)是由中国计算机学会(CCF)发起的"计算机职业资格认证"考试, ...
- leetcode个人题解——#43 Multiply Strings
思路:高精度乘法就可以了. 有两个错误以前没在意,1.成员属性定义时候不能进行初始化, vector<); 这样隐性调用了函数进行初始化的形式特别要注意,也是错误的: 2.容器类只有分配了空间时 ...
- 20130501-Twitter向全美用户开放广告平台Twitter Ads
腾讯科技讯(晁晖)北京时间5月1日消息,据国外媒体报道,Twitter今天向所有美国用户开放了广告平台Twitter Ads.自2012年3月发布以来,Twitter Ads只向受邀请用户开放.Twi ...
- [C++] Solve "Cannot run program "gdb": Unknown reason" error
In Mac OSX, The Issue Image: 1. Build the project on Eclipse successfully. 2. Run gdb on command lin ...
- 《linux内核分析》 第一周
20135130 王川东 计算机是如何工作的? 计算机的基本原理是存储程序和程序控制.预先要把指挥计算机如何进行操作的指令序列(称为程序)和原始数据通过输入设备输送到计算机内存贮器中.每一条指令中明 ...
- U盘安装OSX
1.插入U盘,磁盘工具,格式化U盘为Mac OS X拓展 (日志式): 2.去网站搜索recovery disk assistant,此文件大约1.1M,直接打开使用它制作启动盘,进度条完毕就完成了. ...