[cf611H]New Year and Forgotten Tree
首先,来构造这棵树的形态
称位数相同的点为一类点,从每一类点中任选一个点,具有以下性质:
1.每一类中选出的点的导出子图连通(是一颗树)
2.每一条边必然有一个端点属于某一类中选出的点
(关于“若有解,一定存在上述这种形式的解”的证明可能比较困难,大概感性理解一下?)
由于类别很少(记为$m=\lfloor\log_{10}n\rfloor+1$),$o(m^{m-2})$或$o{\frac{(m+1)m}{2}\choose m-1}$暴力确定这$m$类点中选出的点的树形态(实际差不了多少),接下来每一条边相当于可以使边端点中的一类未在连通块中加入连通块,即如果记$s_{x}$表示$x$类点剩余的点数量,之后每一条边相当于选择$s_{x}$或$s_{y}$减小1
很明显是一个网络流的模型,判一下是否满流即可,复杂度大概可过

1 #include<bits/stdc++.h>
2 using namespace std;
3 #define N 200005
4 #define M 7
5 #define oo 0x3f3f3f3f
6 #define fi first
7 #define se second
8 struct ji{
9 int nex,to,len;
10 }edge[M*M*M];
11 queue<int>q;
12 vector<pair<int,int> >ansE;
13 int V,E,n,m,head[M*M],len[N],a[M][M],id[M][M],tot[M],pos[M],now[M],f[M],d[M*M],work[M*M];
14 char s1[M],s2[M];
15 int find(int k){
16 if (k==f[k])return k;
17 return find(f[k]);
18 }
19 void add(int x,int y,int z){
20 edge[E].nex=head[x];
21 edge[E].to=y;
22 edge[E].len=z;
23 head[x]=E++;
24 if (E&1)add(y,x,0);
25 }
26 bool bfs(){
27 memset(d,oo,sizeof(d));
28 d[0]=0;
29 q.push(0);
30 while (!q.empty()){
31 int k=q.front();
32 q.pop();
33 for(int i=head[k];i!=-1;i=edge[i].nex)
34 if ((edge[i].len)&&(d[edge[i].to]==oo)){
35 d[edge[i].to]=d[k]+1;
36 q.push(edge[i].to);
37 }
38 }
39 return d[V+m+1]<oo;
40 }
41 int dfs(int k,int s){
42 if (k>V+m)return s;
43 for(int &i=work[k];i!=-1;i=edge[i].nex)
44 if ((edge[i].len)&&(d[edge[i].to]==d[k]+1)){
45 int p=dfs(edge[i].to,min(s,edge[i].len));
46 if (p){
47 edge[i].len-=p;
48 edge[i^1].len+=p;
49 return p;
50 }
51 }
52 return 0;
53 }
54 int dinic(){
55 int k,ans=0;
56 while (bfs()){
57 memcpy(work,head,sizeof(work));
58 while (k=dfs(0,oo))ans+=k;
59 }
60 return ans;
61 }
62 bool dfs(int k,int x,int y){
63 if (k>=m){
64 E=0;
65 memset(head,-1,sizeof(head));
66 for(int i=1;i<=m;i++)
67 for(int j=i;j<=m;j++){
68 add(0,id[i][j],a[i][j]);
69 add(id[i][j],V+i,oo);
70 add(id[i][j],V+j,oo);
71 }
72 for(int i=1;i<=m;i++)add(V+i,V+m+1,tot[i]);
73 if (dinic()==n-m){
74 for(int i=1;i<=m;i++)
75 for(int j=i;j<=m;j++)
76 for(int k=head[id[i][j]];k!=-1;k=edge[k].nex)
77 if (edge[k].to>V){
78 int p=edge[k].to-V;
79 for(int l=edge[k].len;l<oo;l++)ansE.push_back(make_pair(++now[p],pos[i+j-p]));
80 }
81 return 1;
82 }
83 return 0;
84 }
85 for(int i=x;i<=m;i++)
86 for(int j=y;j<=m;j++)
87 if ((a[i][j])&&(find(i)!=find(j))){
88 int ii=find(i);
89 f[ii]=j;
90 a[i][j]--;
91 ansE.push_back(make_pair(pos[i],pos[j]));
92 if (dfs(k+1,i,j))return 1;
93 f[ii]=ii;
94 a[i][j]++;
95 ansE.pop_back();
96 }
97 return 0;
98 }
99 int main(){
100 scanf("%d",&n);
101 for(int i=1;i<n;i++){
102 scanf("%s%s",s1,s2);
103 int x=strlen(s1),y=strlen(s2);
104 if (x>y)swap(x,y);
105 a[x][y]++;
106 }
107 for(int i=1;i<=n;i++){
108 len[i]=len[i/10]+1;
109 tot[len[i]]++;
110 }
111 m=len[n];
112 for(int i=1;i<=m;i++)tot[i]--;
113 pos[1]=1;
114 for(int i=2;i<=m;i++)pos[i]=pos[i-1]*10;
115 memcpy(now,pos,sizeof(now));
116 for(int i=1;i<=m;i++)f[i]=i;
117 for(int i=1;i<=m;i++)
118 for(int j=i;j<=m;j++)id[i][j]=++V;
119 if (!dfs(1,1,1))printf("-1");
120 else{
121 for(int i=0;i<ansE.size();i++)printf("%d %d\n",ansE[i].fi,ansE[i].se);
122 }
123 }
[cf611H]New Year and Forgotten Tree的更多相关文章
- 【题解】CF611H New Year and Forgotten Tree
[题解]CF611H New Year and Forgotten Tree 神题了... 题目描述 给定你一棵树,可是每个节点上的编号看不清了,只能辨别它的长度.现在用问号的个数代表每个节点编号那个 ...
- IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2) E - Bear and Forgotten Tree 2 链表
E - Bear and Forgotten Tree 2 思路:先不考虑1这个点,求有多少个连通块,每个连通块里有多少个点能和1连,这样就能确定1的度数的上下界. 求连通块用链表维护. #inclu ...
- 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 ...
- 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\). 蒟蒻猜了一个结论:只要满足对于所有缩出来的点的子 ...
随机推荐
- sqlite3 c++使用以及提高速率(一万条每秒左右)
参考来源: sqlite3的C语言使用(三):https://www.leavesongs.com/C/sqlite3_3.html sqlite插入和查询效率提高方法及测试结果: http://bl ...
- 分布式应用开发 | SpringBoot+dubbo+zookeeper实现服务注册发现 | 远程服务调用
前言 通过新建两个独立服务--提供者.消费者,模拟两个独立分布的应用,通过使用dubbo+zookeeper来实现远程服务调用. 目录 项目搭建 provider-server consumer-se ...
- Java(46)类加载器
作者:季沐测试笔记 原文地址:https://www.cnblogs.com/testero/p/15201673.html 博客主页:https://www.cnblogs.com/testero ...
- NOIP模拟80
学考+OJ改名祭 T1 邻面合并 解题思路 状压 DP ...(于是贪心竟然有 60pts 的高分?? code) 状态设计的就非常妙了,如果状态是 1 就表示是一个分割点也就是一个矩形的右边界. 那 ...
- K12教培从业者转型指南 换个赛道依然可以再创辉煌
随着"双减"政策的落地,属于K12教培机构的时代逐渐拉上帷幕,面对机会不再的K12教培行业,约70万机构和近千万的从业人员面临转型问题.压力之下,留下或离开?对广大K12教培机构从 ...
- 【Spring】Spring重要类层次图
- 【UE4 C++】UKismetSystemLibrary 源代码
// Copyright Epic Games, Inc. All Rights Reserved. #pragma once #include "CoreMinimal.h" # ...
- 【二食堂】Beta - 设计和计划
Beta设计和计划 需求再分析 根据助教.老师.用户以及各个团队PM的反馈意见,我们的项目目前有以下问题: 功能不完整 实用价值不高 两方面的缺陷,所以在Beta阶段,我们工作的中心还是完成项目规划中 ...
- 微信小程序实现上拉和下拉加载更多
在上一篇文章中,我们知道了使用 scroll-view 可以实现上拉加载更多,但是由于 scroll-view 的限制,它无法实现下拉加载更多,这篇文章我们使用 view 组件来实现 上拉和下拉加载更 ...
- hdu 5102 The K-th Distance (队列+生成法,,)
题意: N个点的一棵树.定义点u和点v的距离等于它们之间的路径(唯一的)的长度.这样我们可以得到n*(n-1)/2个距离. 将它们从小到大排序,问前K个数的和是多少. 思路: 将边长为1的树枝都入队列 ...