CF1710D Recover the Tree
CF1710D Recover the Tree
题意
根据题意构造出一棵合法的树。
有 \(n\) 个点。\(a_{i,j}=\{0,1\},i \le j\) 表示编号在 \([i,j]\) 的点是否连通。
\(n \le 2000\)。
保证有解。
思路
显然是先考虑小区间,然后合并到大区间。
思考只有 \(a_{1,n}=1\) 的情况怎么做。
\(i,i+1\) 不能连边。\(i,i+k(k>1)\) 可以连边。但是不可以有更小的区间连通。
一种构造方案是连 \((2,n),(1,i),3 \le i \le n-1\)。
当 \(n=3\) 时无解。
考虑一般情况。
考虑按顺序一个一个加点。现在加到点 \(n\)。
由于树的连通块形式,如果 \([l_1,r_1],[l_2,r_2]\) 连通。且 \(l_2 \le r_1\),那么 \([l_2,r_1],[l_1,r_2]\) 也是连通的。即交和并都是连通的。
那么此时 \([1,n-1]\) 已经有了若干个极大的连通区间。
假设 \(a_{i,n}=1\),如何合并连通块。
参考之前的构造,首先连 \((i,n)\),如果中间还有多个连通块,然后连 \(n\) 和第二块连通块的头,然后连 \(i\) 和其他连通块的尾。
小到大枚举 \(n\),从大到小枚举 \(i\) 容易证明是对的。怎么想到的我就不知道了。
时间复杂度 \(O(n^2)\)。
code
好写的。
#include<bits/stdc++.h>
#define sf scanf
#define pf printf
#define rep(x,y,z) for(int x=y;x<=z;x++)
#define per(x,y,z) for(int x=y;x>=z;x--)
using namespace std;
typedef long long ll;
namespace wing_heart {
constexpr int N=2e3+7;
#define gc getchar
typedef pair<int,int> pii;
#define fi first
#define se second
int t;
int n;
char a;
vector<int> vec[N];
bool cmp (int a,int b) { return a>b; }
void main() {
sf("%d",&t);
while(t--) {
sf("%d",&n);
rep(i,1,n) vec[i].clear();
rep(i,1,n) rep(j,i,n) {
a=gc();
while(a!='1' && a!='0') a=gc();
if(a=='1' && j!=i) vec[j].push_back(i);
}
rep(i,1,n) sort(vec[i].begin(),vec[i].end(),cmp);
stack<pii> que;
rep(r,1,n) {
que.push({r,r});
for(int x : vec[r]) {
vector<pii> tmp;
if(que.top().fi<=x) continue;
while(que.size() && que.top().se>=x) tmp.push_back(que.top()), que.pop();
pf("%d %d\n",x,r);
if(tmp.size()>2) pf("%d %d\n",r,tmp[tmp.size()-2].fi);
rep(i,1,(int)tmp.size()-3) pf("%d %d\n",x,tmp[i].se);
que.push({tmp.back().fi,r});
}
}
}
}
}
int main() {
#ifdef LOCAL
freopen("in.txt","r",stdin);
freopen("my.out","w",stdout);
#endif
wing_heart :: main();
}
CF1710D Recover the Tree的更多相关文章
- [Swift]LeetCode1028. 从先序遍历还原二叉树 | Recover a Tree From Preorder Traversal
We run a preorder depth first search on the root of a binary tree. At each node in this traversal, w ...
- [LeetCode] 1028. Recover a Tree From Preorder Traversal 从先序遍历还原二叉树
We run a preorder depth first search on the rootof a binary tree. At each node in this traversal, we ...
- 【leetcode】1028. Recover a Tree From Preorder Traversal
题目如下: We run a preorder depth first search on the root of a binary tree. At each node in this traver ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- [LeetCode] Recover Binary Search Tree 复原二叉搜索树
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- [Leetcode][JAVA] Recover Binary Search Tree (Morris Inorder Traversal)
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- Recover Binary Search Tree
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- 【leetcode】Recover Binary Search Tree
Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...
- [Leetcode] Recover Binary Search Tree
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- Recover Binary Search Tree [LeetCode]
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
随机推荐
- leetcode 54 螺旋数组
简介 忘不了, 这是华为面试官给我的面试题, 但是我没有在1分钟内做出来. 或许那个时候面试官本来就不想要一个人. 使用模拟的方法. 使用一个visited数组, 判断是否走到边界, 只有四个方向: ...
- ETL数据集成丨PostgreSQL数据迁移至Hive数据库
PostgreSQL数据迁移至Hive数据库 在现代企业数据架构中,将数据从关系型数据库如PostgreSQL迁移到分布式数据仓库系统如Hive,是一项至关重要的任务,旨在实现数据的高效存储.处理与分 ...
- SciTech-BigDataAIML-Tensorflow-Introduction to graphs and tf.function
Graphs are data structures that contain: a set of tf.Operation objects, which representing units of ...
- SciTech-EE-Virtual Electronics Lab: How to Create an Oscilloscope Using Python and ADALM2000
https://wiki.analog.com/university/tools/m2k Virtual Electronics Lab: How to Create an Oscilloscope ...
- SciTech-EECS-PWM(Pulse Width Modulation脉宽调制): 工作原理+特点+应用场景+实例: 将Digital Signal(数字信号) 转换为高精密的调制控制信号
- redis过期机制及注意事项
按Redis官网说法:对于设置了过期时间的key,有2种清理机制,原文如下: How Redis expires keys Redis keys are expired in two ways: a ...
- 免费的PPT模版--九五小庞
PPT模板: www.1ppt.com/moban/ 行业PPT模板:www.1ppt.com/hangye/ 节日PPT模板:www.1ppt.com/jieri/ ...
- win11专业版打开安全中心变成应用商店的问题
有一位雨林木风官网的小伙伴,反馈这么一个问题,当他用win11专业版系统的时候,点击安全中心时,会有提示要求您启动应用商店,不知道是哪里出了问题!面对这个问题,很多人不知道如何解决.然后本期win11 ...
- Win11纯净版电脑开机就死机到问题
有电脑基地的用户说他升级更新win11纯净版系统,用了一段时间以后,不知道咋了,突然发现电脑开机就死机了,怎么都进不去系统,那这个问题要如何解决呢?其实,这可能是因为我们安装的应用程序出现冲突,或是驱 ...
- [浅谈 Unity 内存管理]
链接:https://www.notion.so/Unity-f79bb1d4ccfc483fbd8f8eb859ae55fe 视频链接:https://www.bilibili.com/video/ ...