【题解】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.Net 配置
假设我们现在有这样的需求,要对学生信息进行管理 学生表有要以下要求 字段名称 数据类型 说明 stuNo 字符 学号,该列必填,为主键递增 stuName 字符 学生姓名,该列必填,要考虑姓氏可能是两 ...
- Oracle数据库备份与还原命令
http://www.2cto.com/database/201305/210262.html
- 十几行代码带你用Python批量实现txt转xls,方便快捷
前天看到后台有一兄弟发消息说目前自己有很多txt 文件,领导要转成xls文件,问用python怎么实现,我在后台简单回复了下,其实完成这个需求方法有很多,因为具体的txt格式不清楚,当然如果是有明确分 ...
- 基于腾讯云CLB实现K8S v1.10.1集群高可用+负载均衡
概述: 最近对K8S非常感兴趣,同时对容器的管理等方面非常出色,是一款非常开源,强大的容器管理方案,最后经过1个月的本地实验,最终决定在腾讯云平台搭建属于我们的K8S集群管理平台~ 采购之后已经在本地 ...
- US Customs bond DDP 船运
客户提供目的港门点地址,提供美国进口产品的关税税率基本上就可以了关于ISF信息到时候你发给老外让老外填填好就可以了BAND 货值*0.575%POA 货值*0.335%这二个费用如果国内付就付了,国 ...
- 使用appcmd命令创建iis站点及应用程序池
参考文章:iis7 appcmd的基础命令及简单用法 验证环境:Windows 7 IIS7 AppCmd.exe工具所在目录 C:\windows\sytstem32\inetsrv\目录下, ...
- 深入react技术栈解读
1. react实现virtual DOM ,如果要改变页面的内容,还是需要执行DOM操作,比原生操作DOM多了virtualDOM的操作(计算,对比等), 应该是更耗性能??? 2. react特点 ...
- AOP:jdk的动态代理
1.文件结构 2.建立接口 package com.wangcf.manager; public interface IUserManager { public void add(); public ...
- tomcat启动时,报java.io.EOFException
在启动Tomcat的时候突然报出IO异常,具体异常如下图 在网上找了解决方法,测试了好几种,都不行,到最后看了一个博客解决了,在此记录一下. 百度了下,网上都是说去Tomcat目录下:将tomcat5 ...
- python 动态获取当前运行的类名和函数名的方法
一.使用内置方法和修饰器方法获取类名.函数名 python中获取函数名的情况分为内部.外部,从外部的情况好获取,使用指向函数的对象,然后用__name__属性 复制代码代码如下: def a():pa ...