CF #541div2 F
题目本质:并查集的链式合并
解决方法1:
类似哈夫曼树,叶节点们为真点,其余造一些虚的父节点,使得dfs这棵树的时候,先进行并查合并的点一定是兄弟节点因而紧挨着被输出,巧妙达到了效果。
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cctype>
#include <climits>
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <string>
#include <sstream>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <list>
#include <fstream>
#define ri readint()
#define gc getchar()
#define R(x) scanf("%d", &x)
#define W(x) printf("%d\n", x)
#define init(a, b) memset(a, b, sizeof(a))
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define irep(i, a, b) for (int i = a; i >= b; i--)
#define ls p << 1
#define rs p << 1 | 1
using namespace std; typedef double db;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
const int inf = 0x3f3f3f3f;
const ll INF = 1e18; inline int readint() {
int x = , s = , c = gc;
while (c <= ) c = gc;
if (c == '-') s = -, c = gc;
for (; isdigit(c); c = gc)
x = x * + c - ;
return x * s;
} const int maxn = 15e4 + ;
int n, fa[maxn << ], l[maxn << ], r[maxn << ]; inline int find(int v) {
return v == fa[v] ? v : fa[v] = find(fa[v]);
} void dfs(int cur) {
if (!l[cur] && !r[cur]){
printf("%d ", cur);
return;
}
if (l[cur]) dfs(l[cur]);
if (r[cur]) dfs(r[cur]);
} int main() {
n = ri;
rep(i, , n) {
fa[i] = i;
}
rep(i, , n - ) {
int a = ri, b = ri;
a = find(a), b = find(b);
fa[a] = fa[b] = fa[n + i] = n + i;
l[n + i] = a, r[n + i] = b;
}
dfs(n + n - );
return ;
}
解决方法2:
正常地用数组记录链,l和r记录真实的左右顺序,并查集式的getl和getr记录这个链上的最左端和最右端,两个集合并时一接。貌似直接把常规并查集的father数组扔了……
大佬代码:
int n,m,i,j,k,a[],l[],r[],fl[],fr[],x,y;
int gfl(int x){if (fl[x]==x) return x;return fl[x]=gfl(fl[x]);}
int gfr(int x){if (fr[x]==x) return x;return fr[x]=gfr(fr[x]);}
int main()
{
read(n);
rep(i,n)
{
fl[i]=fr[i]=l[i]=r[i]=i;
}
rep(i,n-)
{
read(x);read(y);
x=gfr(x);y=gfl(y);
r[x]=y;l[y]=x;
fr[x]=y;fl[y]=x;
}
x=gfl();
rep(i,n)
{
printf("%d ",x);
x=r[x];
}
return ;
}
CF #541div2 F的更多相关文章
- CF 633 F. The Chocolate Spree 树形dp
题目链接 CF 633 F. The Chocolate Spree 题解 维护子数答案 子数直径 子数最远点 单子数最长直径 (最长的 最远点+一条链) 讨论转移 代码 #include<ve ...
- CF #271 F Ant colony 树
题目链接:http://codeforces.com/contest/474/problem/F 一个数组,每一次询问一个区间中有多少个数字可以整除其他所有区间内的数字. 能够整除其他所有数字的数一定 ...
- CF 494 F. Abbreviation(动态规划)
题目链接:[http://codeforces.com/contest/1003/problem/F] 题意:给出一个n字符串,这些字符串按顺序组成一个文本,字符串之间用空格隔开,文本的大小是字母+空 ...
- CF 1138 F. Cooperative Game
F. Cooperative Game 链接 题意: 有10个玩家,开始所有玩家在home处,每次可以让一些玩家沿着边前进一步,要求在3(t+c)步以内,到达终点. 分析: 很有意思的一道题.我们构造 ...
- CF 1041 F. Ray in the tube
F. Ray in the tube 链接 题意: 有两条平行于x轴的直线A,B,每条直线上的某些位置有传感器.你需要确定A,B轴上任意两个整点位置$x_a$,$x_b$,使得一条光线沿$x_a→x_ ...
- 【Cf #502 F】The Neutral Zone
本题把$log$化简之后求得就是每个质数$f$前的系数,求系数并不难,难点在于求出所有的质数. 由于空间限制相当苛刻,$3e8$的$bitset$的内存超限,我们考虑所有的除了$2$和$3$以外的质数 ...
- CF 868 F. Yet Another Minimization Problem
F. Yet Another Minimization Problem http://codeforces.com/contest/868/problem/F 题意: 给定一个长度为n的序列.你需要将 ...
- CF 1051 F. The Shortest Statement
F. The Shortest Statement http://codeforces.com/contest/1051/problem/F 题意: n个点,m条边的无向图,每次询问两点之间的最短路. ...
- CF 1042 F. Leaf Sets
F. Leaf Sets http://codeforces.com/contest/1042/problem/F 题意: 将所有的叶子节点分配到尽量少的集合,一个可行的集合中两两叶子节点的距离< ...
随机推荐
- codeforces D. Toy Sum 解题报告
题目链接:http://codeforces.com/problemset/problem/405/D 题目意思:从 1 - 1000000 中选择 n 个数:x1,x2,...,xn,对 x1-1, ...
- 万亿级日志与行为数据存储查询技术剖析(续)——Tindex是改造的lucene和druid
五.Tindex 数果智能根据开源的方案自研了一套数据存储的解决方案,该方案的索引层通过改造Lucene实现,数据查询和索引写入框架通过扩展Druid实现.既保证了数据的实时性和指标自由定义的问题,又 ...
- touch实现滑动删除
请用chrome手机模式查看或者在手机上查看(转载请注明出处) <!DOCTYPE html> <html> <head> <meta charset=&qu ...
- BZOJ_1296_[SCOI2009]粉刷匠_DP
BZOJ_1296_[SCOI2009]粉刷匠_DP Description windy有 N 条木板需要被粉刷. 每条木板被分为 M 个格子. 每个格子要被刷成红色或蓝色. windy每次粉刷,只能 ...
- Introduction to Multi-Threaded, Multi-Core and Parallel Programming concepts
https://katyscode.wordpress.com/2013/05/17/introduction-to-multi-threaded-multi-core-and-parallel-pr ...
- Ubuntu18.04安装和配置Java JDK11并卸载自带OpenJDK
卸载自带的OpenJDK方法: 在终端执行命令:sudo apt-get remove openjdk* Ubuntu18.04安装JDK11.0.2 前面步骤一样,可参考:https://www.c ...
- IDL(Interactive Data Language——交互式数据语言)
Interactive Data Language——交互式数据语言 目前,图像处理常用的ENVI就是用IDL开发的经典软件 广泛支持的平台: Microsoft Windows Open VMS S ...
- easyui更改messager的OkCancel按钮为(中文)确定取消
jquery-easyui默认情况下,消息框的按钮文字是英文的OK Cancel,但可以通过提供的方法进行修改,如: $.extend($.messager.defaults,{ ok:" ...
- 洛谷1303 A*B Problem 解题报告
洛谷1303 A*B Problem 本题地址:http://www.luogu.org/problem/show?pid=1303 题目描述 求两数的积. 输入输出格式 输入格式: 两个数 输出格式 ...
- win10系统,mysql-installer-community-5.7.19.0.msi安装
1.进入官网找到自己所需的安装包:https://dev.mysql.com/ ,路径:DOWNLOAD-->MYSQL Community Edition(GRL)-->MYSQL o ...