public class unionfind2 implements UF { int[] parent; int[] rank; public unionfind2(int n) { parent=new int[n]; rank=new int[n]; for(int i=0;i<parent.length;i++) { parent[i]=i; rank[i]=1; } } private int find(int i) { while(i!=parent[i]) { parent[i]=…
之前在CSDN看到一篇很受欢迎的讲解并查集的博文,其中自然用到了路径压缩: int pre[1000]; int find(int x){ int root = x; while(pre[root]!=root) root = pre[root]; int i = x,j; while(i!=root){ //path compress j = pre[i]; pre[i] = root; i = j; } return root; } void join(int x,int y){ int f…
Magina Problem Description Magina, also known as Anti-Mage, is a very cool hero in DotA (Defense of the Ancient).If you have no idea of him, here is some brief introduction of his legendary antecedents:Twin sons to the great Prophet, Terrorblade and…
Rank of Tetris Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5415    Accepted Submission(s): 1514 Problem Description 自从Lele开发了Rating系统,他的Tetris事业更是如虎添翼,不久他遍把这个游戏推向了全球. 为了更好的符合那些爱好者的喜好,Lele又想了…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1811 求一堆数据的拓扑序. 处理:x>y就是x到y一条边,x<y就是y到x一条边.关键问题是处理x=y的情况. 假如x=y,就有问题了.假如不处理的话,可能会被当成少处理一个点而使结果编程UNCERTAIN.所以我们考虑用并查集来解决这个问题. 选谁当祖先?题中又给了一个其他的量叫做RP值,这个RP值的规律是序号越大RP值越大.这样我们可以在合并的时候,尽可能地将RP值大的数当成本集合的祖先. /…
#include <stdio.h> #include <string.h> #include <vector> #include <queue> using namespace std; struct node//边 { int a, b;//顶点 char ch;//运算符 }c[]; vector<];//map数组存贮邻接表 (大佬都是这么开数组的) ], fa[];//in数组表示入度,fa[i]表示顶点i所在集合的根节点 int find(…
一. 离线Tarjan算法 LCA问题(lowest common ancestors):在一个有根树T中.两个节点和 e&sig=3136f1d5fcf75709d9ac882bd8cfe0cd" alt="">的近期公共祖先.指的是二者的公共祖先中深度最高的节点. 给定随意两个树中的节点,求它们的近期公共祖先. 对于二分查找树.二叉树,能够用普通的dfs实现.但对于多叉树.查询次数频繁的情况下.离线Tarjan算法的长处就显现出来了.因为对树上全部节点仅仅进…
/* * UnionFind.h * 有两种实现方式,QuickFind和QuickUnion * QuickFind: * 查找O(1) * 合并O(n) * QuickUnion:(建议使用) * 查找O(logn)可优化至O(a(n)),a(n)<5 * 合并O(logn)可优化至O(a(n)),a(n)<5 * Created on: 2020年2月13日 * Author: LuYonglei */ //由于QuickFind平均时间复杂度不理想,所以本文件只用QuickUnion来…
题目链接:http://poj.org/problem?id=1456 Supermarket Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13736   Accepted: 6206 Description A supermarket has a set Prod of products on sale. It earns a profit px for each product x∈Prod sold by a d…
1110: 传输网络 Time Limit: 3 Sec  Memory Limit: 512 MBSubmit: 43  Solved: 18[Submit][Status][Web Board] [Edit] Description Byteland国家的网络单向传输系统可以被看成是以首都Bytetown为中心的有向树,一开始只有Bytetown建有基站,所有其他城市的信号都是从Bytetown传输过来的.现在他们开始在其他城市陆续建立了新的基站,命令“C x“代表在城市x建立了一个新的基站…