【题解】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 ...
随机推荐
- async+await 让界面飞,让双手爽
.net 4.5已经发布很久了,但是一直也没有静下心来好好的研究微软给开发者带来的喜悦. 今天我将简单的介绍下 async + await 这对搭档的出现,如何让频繁假死的界面飞起来(其实只是不再阻塞 ...
- Python中的解决中文字符编码的问题
python3中str默认为Unicode的编码格式 python2中str默认为bytes类型的编码格式 Unicode是一32位编码格式,不适合用来传输和存储,所以必须转换成utf-8,gbk等等 ...
- sklearn中的交叉验证(Cross-Validation)
这个repo 用来记录一些python技巧.书籍.学习链接等,欢迎stargithub地址sklearn是利用python进行机器学习中一个非常全面和好用的第三方库,用过的都说好.今天主要记录一下sk ...
- C语言零碎知识点
1. int整形在64位和32位计算机中都占4个字节. 指针在64位占8个字节,32位占4个字节. 2. 数组下标从0开始,a[0]开始,链表下标从1开始,a[1]开始. 3. 条件运算符(con ...
- 1.hive介绍及安装配置
1.Hive介绍 数据库OLTP 在线事务处理 数据仓库OLAP 在线分析处理 延迟高 类sql方式(HQL) 使用sql方式,用来读写,管理位于分布式存储系统上的大型数据集的数据仓库技术 hive是 ...
- Centos上搭建git服务
1.安装Git $ yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel perl-devel $ yum ...
- php中注释有关内容
//单行注释 /*多行注释*/ /** 文档注释 (注意 文档注释与前面的那个多行注释不同)文档注释可以和特定的程序元素相关联 例如 类 函数 常量 变量方法 问了将文档注释与元素相关联 只需要在元素 ...
- VMware提示无法打开内核设备 \\.\Global\vmx86: 系统找不到指定的文件解决方案
1.右键单击[我的电脑],选择[管理] 2.在[服务]中找到VMware Workstation Server服务右键启动
- c# 加载图片 正在被占用问题
问题情境:图片文件加载到pdf中,程序没有退出,再次加载该图片文件,提示被占用. 解决办法: 1.加载文件会锁定该文件,fromfile方法会导致占用内存较大,不使用该方法. FileStream f ...
- Alpha-9
前言 失心疯病源9 团队代码管理github 站立会议 队名:PMS 530雨勤(组长) 今天完成了那些任务 熬夜肝代码 代码签入github 明天的计划 最好能够完成对接环节 准备展示内容 还剩下哪 ...