题目本质:并查集的链式合并

解决方法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的更多相关文章

  1. CF 633 F. The Chocolate Spree 树形dp

    题目链接 CF 633 F. The Chocolate Spree 题解 维护子数答案 子数直径 子数最远点 单子数最长直径 (最长的 最远点+一条链) 讨论转移 代码 #include<ve ...

  2. CF #271 F Ant colony 树

    题目链接:http://codeforces.com/contest/474/problem/F 一个数组,每一次询问一个区间中有多少个数字可以整除其他所有区间内的数字. 能够整除其他所有数字的数一定 ...

  3. CF 494 F. Abbreviation(动态规划)

    题目链接:[http://codeforces.com/contest/1003/problem/F] 题意:给出一个n字符串,这些字符串按顺序组成一个文本,字符串之间用空格隔开,文本的大小是字母+空 ...

  4. CF 1138 F. Cooperative Game

    F. Cooperative Game 链接 题意: 有10个玩家,开始所有玩家在home处,每次可以让一些玩家沿着边前进一步,要求在3(t+c)步以内,到达终点. 分析: 很有意思的一道题.我们构造 ...

  5. CF 1041 F. Ray in the tube

    F. Ray in the tube 链接 题意: 有两条平行于x轴的直线A,B,每条直线上的某些位置有传感器.你需要确定A,B轴上任意两个整点位置$x_a$,$x_b$,使得一条光线沿$x_a→x_ ...

  6. 【Cf #502 F】The Neutral Zone

    本题把$log$化简之后求得就是每个质数$f$前的系数,求系数并不难,难点在于求出所有的质数. 由于空间限制相当苛刻,$3e8$的$bitset$的内存超限,我们考虑所有的除了$2$和$3$以外的质数 ...

  7. CF 868 F. Yet Another Minimization Problem

    F. Yet Another Minimization Problem http://codeforces.com/contest/868/problem/F 题意: 给定一个长度为n的序列.你需要将 ...

  8. CF 1051 F. The Shortest Statement

    F. The Shortest Statement http://codeforces.com/contest/1051/problem/F 题意: n个点,m条边的无向图,每次询问两点之间的最短路. ...

  9. CF 1042 F. Leaf Sets

    F. Leaf Sets http://codeforces.com/contest/1042/problem/F 题意: 将所有的叶子节点分配到尽量少的集合,一个可行的集合中两两叶子节点的距离< ...

随机推荐

  1. platform_set_drvdata和platform_get_drvdata用法【转】

    本文转载自:http://www.cnblogs.com/wangxianzhen/archive/2013/04/09/3009530.html 在用到Linux设备驱动的platform框架时,常 ...

  2. 牛逼的This使用

    今天看到一个很不错的this使用demo: package com.toov5.Reordering; class Message1{ private Channel channel; private ...

  3. codeforces 467B Fedor and New Game 解题报告

    题目链接:http://codeforces.com/contest/467/problem/B 题目意思:有 m + 1 个 player 和 n 种类型的 soldiers.每个player被赋予 ...

  4. UEBA——通过用户画像识别安全威胁

    UEBA and Machine Learning - Download Free Guide for CISOs‎ Adinfo.niara.com/UEBA/Guide-For-CISOs‎ Le ...

  5. php排序方法之插入排序

    //插入排序法 $arr = array(3,55,45,2,67,76,6.7,-65,85,4); function insertSort($arr){ for ( $i=0; $i<cou ...

  6. git 错误 Reinitialized existing Git repository in /**/***/ 和refusing to merge unrelated histories

    报错一: 这句话的意思是 在路径 /Users/jackma/Downloads/lotteryTicket 2/.git/ 现有的Git存储库初始化 ➜ lotteryTicket 2 git:(m ...

  7. [SHOI 2007] 善意的投票

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1934 [算法] 首先 , 选择睡觉的人和不选择睡觉的人构成两个集合 这启发我们用最小 ...

  8. js中this 的四种用法

    this 在函数执行时,this 总是指向调用该函数的对象.要判断 this 的指向,其实就是判断 this 所在的函数属于谁. 在<javaScript语言精粹>这本书中,把 this  ...

  9. iOS设备屏幕分辨率分布

    iOS设备屏幕分辨率比较单一,960*640是iPhone4和4s的分辨率,占比67.4%;1024*768是iPad1和iPad2的分辨率,占比22.5%;480*320是iPhone3和3gs的分 ...

  10. A - Vasya and Socks

    A - Vasya and Socks Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64 ...