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 题意: 将所有的叶子节点分配到尽量少的集合,一个可行的集合中两两叶子节点的距离< ...
随机推荐
- 转载-jmeter进阶功能
在这此对新版本jmeter的学习+温习的过程,发现了一些以前不知道的功能,所以,整理出来与大分享.本文内容如下. 如何使用英文界面的jmeter 如何使用镜像服务器 Jmeter分布式测试 启动Deb ...
- jvm调试
https://www.usenix.org/legacy/events/jvm01/full_papers/russell/russell_html/index.html
- javascript ajax和jquery ajax
一 进行ajax步骤: 1 获取dom值 2发送ajax请求 3返回成功进行前端逻辑处理 二 原生javascript的ajax <!DOCTYPE html> <html> ...
- Hihocoder #1095 : HIHO Drinking Game (微软苏州校招笔试)( *【二分搜索最优解】)
#1095 : HIHO Drinking Game 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Little Hi and Little Ho are playin ...
- Codeforces Round #379 (Div. 2) E. Anton and Tree —— 缩点 + 树上最长路
题目链接:http://codeforces.com/contest/734/problem/E E. Anton and Tree time limit per test 3 seconds mem ...
- UVA10129 Play on Words —— 欧拉回路
题目链接:https://vjudge.net/problem/UVA-10129 代码如下: // UVa10129 Play on Words // Rujia Liu // 题意:输入n个单词, ...
- SSL handshake_decode_error
查看布署在Amazon服务器上的日志时,发现如下错误: generated SERVER ALERT: Fatal - Handshake Failure - handshake_decode_err ...
- Python模块:os
OS模块常用用法: os.name() #判断当前使用的系统环境,windows则返回 ‘nt’,Linux则返回‘posix’ os.getcwd() #显示当前目录 os.listdir() #以 ...
- 数据表示Numpy
1 基本 1.1 基本介绍 掌握表示, 清洗, 统计和展示数据的能力 Numpy, Matplotlib, Pandas, Projects 摘要: 有损的提取数据特征的过程 可以将一组数据, 摘要出 ...
- 在KitKat(Android 4.4.2) 推送网址给手机
弱者才会回避问题. 最近想把网址推送给手机实现后台下载,打算故技重施,用短信传送然后中断广播的方法实现隐蔽传送.试了半天发现怎么现在拦不住短信了.查了一下才发现原来Android4.4增加了一个安全机 ...