首先,来构造这棵树的形态

称位数相同的点为一类点,从每一类点中任选一个点,具有以下性质:

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的更多相关文章

  1. 【题解】CF611H New Year and Forgotten Tree

    [题解]CF611H New Year and Forgotten Tree 神题了... 题目描述 给定你一棵树,可是每个节点上的编号看不清了,只能辨别它的长度.现在用问号的个数代表每个节点编号那个 ...

  2. IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2) E - Bear and Forgotten Tree 2 链表

    E - Bear and Forgotten Tree 2 思路:先不考虑1这个点,求有多少个连通块,每个连通块里有多少个点能和1连,这样就能确定1的度数的上下界. 求连通块用链表维护. #inclu ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 题解 CF611H 【New Year and Forgotten Tree】

    Solution 提供一种新思路. 首先考虑如何判断一个状态是否合法. 考虑把所有十进制长度一样的数缩成一个点. 这样的点的个数 \(\le 5\). 蒟蒻猜了一个结论:只要满足对于所有缩出来的点的子 ...

随机推荐

  1. css新增属性之边框

    css3新增属性 边框属性 背景属性 文字属性 颜色属性 边框属性 属性 说明 border-radius 设置边框圆角 border-image 设置图像边框 border-shadow 设置边框阴 ...

  2. mysql创建库

    建库 GBK: create database test2 DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci; UTF8: CREATE DATABAS ...

  3. Java初步学习——2021.10.10每日总结,第五周周日

    (1)今天做了什么: (2)明天准备做什么? (3)遇到的问题,如何解决? 今天继续学习菜鸟教程java字符串实例 5.字符串反转--reverse方法 public class Main { pub ...

  4. 2021年1月-第02阶段-前端基础-HTML+CSS进阶-VS Code 软件

    软件安装 VSCode软件 能够安装 VS Code 能够熟练使用 VS Code 软件 能够安装 VS Code 最常用的插件 1. VS Code简介 1.1 VS Code 简介 Visual ...

  5. 【二食堂】Alpha - Scrum Meeting 4

    Scrum Meeting 4 例会时间:4.14 12:30 - 12:50 进度情况 组员 昨日进度 今日任务 李健 1. 主页面的搭建工作issue 1. 完成主页搭建**issue2. 与后端 ...

  6. 热身训练2 The All-purpose Zero

    The All-purpose Zero 简要题意:  长度为n的数组,每个数字为S[i],$0$是一种很神奇的数字,你想要的,它都可以变! 问这个序列的最长上升子序列长度为多少? 分析: 我们将除了 ...

  7. DDD领域驱动设计-设计规范-Ⅵ

    不以规矩,不能成方圆.                                                                     -战国·邹·孟轲<孟子·离娄章句上 ...

  8. 加法运算替代 牛客网 程序员面试金典 C++ Python

    加法运算替代 牛客网 程序员面试金典 题目描述 请编写一个方法,实现整数的乘法.减法和除法运算(这里的除指整除).只允许使用加号. 给定两个正整数int a,int b,同时给定一个int type代 ...

  9. 字典树(Trie)

    终于学会字典树了,真开心(然后就滚过来写总结了). 首先,字典树到底是个什么东西呢?请看下面这段话: 字典树,常被用来保存与查找大量的字符串,它利用了字符串之间的公共前缀来节约时间,但它的空间花费较大 ...

  10. DeWeb第2个通用化模块:主控模块。 手机/电脑自适应。通过修改配置文件即可实现进入不同模块

    演示: https://delphibbs.com/main.dw 也可以通过 https://delphibbs.com/login.dw 采用admin/123456登录后自动进入 开发环境和源代 ...