zoukankan      html  css  js  c++  java
  • Luogu P3377【模板】左偏树(可并堆)

    采用了jls的左偏树写法,非常好写...

    额外用一个并查集维护集合关系即可,注意路径压缩。

    #include <bits/stdc++.h>
    
    using namespace std;
    typedef long long LL;
    
    #define LOG(...) fprintf (stderr, __VA_ARGS__)
    #define pb push_back
    #define mp make_pair
    #define SZ(x) ((int)(x).size())
    #define ALL(x) (x).begin(), (x).end()
    
    const int INF = 0x3f3f3f3f, N = 100005;
    const LL INFL = 0x3f3f3f3f3f3f3f3fll;
    
    int n, m, rt[N], cnt, f[N], a[N]; 
    struct HeapNode {
    	int w, ls, rs, sz;
    	HeapNode (int w=0, int ls=0, int rs=0, int sz=0): w(w), ls(ls), rs(rs), sz(sz){}
    }e[N];
    
    int find (int x) {
    	return x == f[x] ? f[x] : f[x] = find(f[x]);
    }
    int merge (int x, int y) {
    	if (!x || !y) return x+y;
    	if (e[x].w > e[y].w || (e[x].w == e[y].w && x > y)) swap(x, y);
    	e[x].rs = merge(e[x].rs, y);
    	if (e[e[x].rs].sz > e[e[x].ls].sz) swap(e[x].ls, e[x].rs);
    	e[x].sz = e[e[x].rs].sz + 1;
    	return x; 
    }
    int main() {
    #ifdef LiM_817
    	double cl = clock();
    	freopen ("input.txt", "r", stdin);
    #endif
    	
    
    	scanf ("%d%d", &n, &m);
    	for (int i = 1; i <= n; i++) {
    		rt[i] = ++cnt; f[i] = i; 
    		int w; scanf ("%d", &w);
    		a[i] = w; 
    		e[rt[i]] = (HeapNode) {w, 0, 0, 1}; 
    	}
    	
    	while (m--) {
    		int op, x, y;
    		scanf ("%d%d", &op, &x);
    		if (op == 1) {
    			scanf ("%d", &y);
    			if (a[x] == -1 || a[y] == -1) continue;
    			x = find(x), y = find(y);
    			rt[x] = merge(rt[x], rt[y]);
    			f[y] = x; 
    		}
    		else if (op == 2) {
    			if (a[x] == -1) {
    				puts("-1");
    				continue;
    			}
    			int t = find(x);
    			printf ("%d
    ", e[rt[t]].w);
    			a[rt[t]] = -1; 
    			rt[t] = merge(e[rt[t]].ls, e[rt[t]].rs);
    		}
    	}
    #ifdef LiM_817
    	cerr << fixed << setprecision(2) << "Time ellapsed: " << ((double)(clock() - cl) / CLOCKS_PER_SEC) << "s
    ";
    #endif
    	return 0;
    }
    
  • 相关阅读:
    并发编程概述
    学习笔记-ResNet网络
    学习笔记-反向传播算法
    学习笔记-canny边缘检测
    学习笔记-霍夫变换
    GitHub访问速度慢的一种优化方法
    C#开源定时回调库PETimer的使用
    C#开源网络通信库PESocket的使用
    XML在C#与Unity3D中的实战运用
    Unity本地持久化类Playerprefs使用详解
  • 原文地址:https://www.cnblogs.com/LiM-817/p/11934607.html
Copyright © 2011-2022 走看看