HDU4496 D-City【基础并查集】】的更多相关文章

Problem Description Luxer is a really bad guy. He destroys everything he met.  One day Luxer went to D-city. D-city has N D-points and M D-lines. Each D-line connects exactly two D-points. Luxer will destroy all the D-lines. The mayor of D-city wants…
A Bug's Life Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 13709    Accepted Submission(s): 4449 Problem Description Background Professor Hopper is researching the sexual behavior of a rare…
http://poj.org/problem?id=2236 由于发生了地震,有关组织组把一圈电脑一个无线网,但是由于余震的破坏,所有的电脑都被损坏,随着电脑一个个被修好,无线网也逐步恢复工作,但是由于硬件的限制,一台电脑和另一台电脑能够相连当他们之间的距离小于d,或者还有一台电脑当中介,分别与两台电脑相连. 在修复的过程中,工作者会有两种操作,修复电脑和询问电脑a和电脑b是否相连.当询问的时候输出答案. 因为输入数据很大,需要快速判断电脑a和电脑b相连,所以自然想到用并查集. 初始时候 全部电…
http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=45522 给定一棵树的n个节点,每个节点标号在1到n之间,1是树的根节点,有如下两种操作: M v :把编号为v的节点标记. Q v :查询与v节点距离最近的被标记的点的编号.最初只有根节点被标记. 输入n-1个数表示,每一个数是当前第i个数的父节点,输出对于每个查询得到标号的总和. 并查集与树的结合,在输入的时候就可以根据父节点的关系建立并查集,1的父亲是1. 还有把编号为…
题目链接:http://poj.org/problem?id=2236 题目大意:城市网络由n台电脑组成,因地震全部瘫痪,现在进行修复,规定距离小于等于d的电脑修复之后是可以直接相连 进行若干操作,O a,修复编号为a的电脑,S a,b  询问a,b电脑能否联系 思路分析:并查集,只是和并条件变了,首先要已经被修复(vis数组)其次距离要小于d,并查集搞完之后 询问的时候只需要看他们是不是有着相同的根节点即可. 代码: #include <iostream> #include <cstd…
An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wireless network with the lap computers, but an unexpected aftershock attacked, all computers in the network were all broken. The computers are repaired…
思路:逆向并查集,逆向加入每一条边即可.在获取联通块数量的时候,直接判断新加入的边是否合并了两个集合,如果合并了说明联通块会减少一个,否则不变. AC代码 #include <cstdio> #include <cmath> #include <cctype> #include <algorithm> #include <cstring> #include <utility> #include <string> #incl…
Ivan had string s consisting of small English letters. However, his friend Julia decided to make fun of him and hid the string s. Ivan preferred making a new string to finding the old one. Ivan knows some information about the string s. Namely, he re…
#include <stdio.h> #include <string.h> ], g[]; int find(int x) //并查集的查找,找到共同的父亲 { if (f[x] != x) f[x] = find(f[x]); return f[x]; } int main() { ; while (scanf("%d%d", &a, &b) != EOF) { , t = ; && b<) break; ; i <…
HDU1213How Many Tables Problem Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Ignatius wants to know how many tables he needs at least. You have to notice that not all the friends know each other, and all…
City Park 题目连接: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=122283#problem/F Description Porto is blessed with a beautiful city park. The park, in the western section of the city, borders the Atlantic Ocean. It has great lawns, small forest…
#include <iostream> #include <cstdlib> #include <cstdio> #include <algorithm> using namespace std; ; int a, b; int father[M]; //记录父节点 bool circle; //判断是否存在环 bool visit[M]; //用来记录顶点数 int edgenum, vnum; //分别表示边数,顶点数 void initial() {…
和上一道小希的迷宫差点儿相同,可是在HDU上提交一直WA,POJ过了 HDU的数据太强了吧,强的我感觉数据有问题 题意:输入若干对点,推断是否是一颗树,转化过来也就是是否存在环 点数-边数=1,还要推断每一个点的入度都<=1 POJ AC代码 #include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> #include <algorithm> con…
https://codeforces.com/contest/1131/problem/F #include<bits/stdc++.h> using namespace std; int n; vector<vector<int> > v; vector<int> par; int find(int d){ if(d==par[d]) return d; else return par[d]=find(par[d]); } void unite(int x…
杭电的图论题目列表.共计500题,努力刷吧 AC 64ms #include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> #include <algorithm> const int INF = 1e8; using namespace std; int father[100010]; bool vis[100010]; int findx(int r) {…
#include <iostream> #include <algorithm> using namespace std; + ; int fa[maxn]; int Find(int x){ if (x == fa[x]) return x; else return fa[x] = Find(fa[x]); } int main(){ int a, b; while (cin >> a){ //init ; i < maxn; i++) fa[i] = i; ;…
#include <cstdio> #include <cstring> #include <algorithm> #include <cstdlib> using namespace std; #define MAXN 1005 int n, m; int fa[MAXN]; //父节点 int mark[MAXN]; void init() { ; i <= n; i++) { fa[i] = i; mark[i] = ; } } int find…
#include <cstdio> #include <cstring> #include <algorithm> #include <cstdlib> using namespace std; #define MAXN 100005 int fa[MAXN]; //父节点 int vis[MAXN]; int Min = 0xffffff; ; void init() { ; i < MAXN; i++) { fa[i] = i; } } int f…
A Bug's Life Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 9204    Accepted Submission(s): 2961 Problem Description Background Professor Hopper is researching the sexual behavior of a rare sp…
转自:http://blog.csdn.net/shahdza/article/details/7779230 [HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基础并查集★1325&&poj1308 Is It A Tree? 基础并查集★1856 More is better 基础并查集★1102 Constructing Roads 基础最小生成树★1232 畅通工程 基础并查集★2120 Ice_cream's world I 基础并查集★212…
 题目连接:http://poj.org/problem?id=1182 基础并查集,需要维护与根节点关系,解析见代码: /* poj 1182 并查集 思路分析:让你分析这些话里面多少假的 只需要用一个并查集将已经给出的这些元素存起来 同时记录每个元素与他们根节点关系,如果根结点相同 但是关系不符合就是出现了矛盾. 关系有三种:同类 记为0 吃根节点 1 被根节点吃 2 这样也是为了与他给出的d关系一致 d-1就与我们规定的关系一致了 并查集的关键是路径压缩,在压缩路径的同时我们要更新与根节点…
题意: 一次地震震坏了所有网点 现在开始修复它们 有N个点 距离为d的网点可以进行通信 O p   代表p点已经修复 S p q 代表询问p q之间是否能够通信 思路: 基础并查集 每次修复一个点重新刷一边图就行了 /* *********************************************** Author :Sun Yuefeng Created Time :2016/11/1 15:40:35 File Name :food.cpp ******************…
http://poj.org/problem?id=1182 Description 动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A. 现有N个动物,以1-N编号.每个动物都是A,B,C中的一种,但是我们并不知道它到底是哪一种. 有人用两种说法对这N个动物所构成的食物链关系进行描述: 第一种说法是"1 X Y",表示X和Y是同类. 第二种说法是"2 X Y",表示X吃Y. 此人对N个动物,用上述两种说法,一句接一句地说出…
什么是并查集? 合并!查询!集合! 专业点说? 动态维护若干不重叠的和,支持合并查询的数据结构!(lyd老师说的) 数据结构特点:代表元.即为每个集合选择一个固定的元素,作为整个集合的代表,利用树形结构存储,每个节点都是一个元素,树根是集合的代表元素.(还是lyd老师说的) 两大基本操作 一.合并(merge()) 即把两个集合合并到一个的操作.通俗的说,即令其中一个树根为另一个树根的子节点. void merge(int x,int y) { fa[getf(x)]=getf(y); } 二.…
City Park Problem's Link: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=129725 Mean: 在网格中给你一些矩形,求最大连通块的面积. analyse: 由于题目保证了矩形不会相交,所以不用扫描线也可做. 先把所有的线段分为横向和纵向,然后排序,依次判断是否相邻,相邻就用并查集合并,最后再用并查集统计一下面积,取最大值即可. Time complexity: O(N) Source code…
先说说种类并查集吧. 种类并查集是并查集的一种.但是,种类并查集中的数据是分若干类的.具体属于哪一类,有多少类,都要视具体情况而定.当然属于哪一类,要再开一个数组来储存.所以,种类并查集一般有两个数组,一个存并查集内的父子关系,一个存各个节点所属的种类关系. 以这道题为例(题意在后面,如果没有读题,可以先看完题在来看这部分)—— 这道题很明显,将bug分成两类,一公一母.但是实际上我们并不关心它是公的还是母的,只关心它们之间是同性还是异性.所以,我们可以设与并查集的根节点同性的为0,反之为1.所…
裸敲并查集,很水一次AC #include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <cctype> #include <cmath> #include <algorithm> #include <numeric> #include <string> #pragma comment(lin…
裸并查集,但有二坑: 1.需要路径压缩,不写的话会TLE 2.根据题目大意,如果0组男孩合作的话,应该最大的子集元素数目为1.所以res初始化为1即可. #include <iostream> #include <cstring> #include <cstdio> #include <cstdlib> #include <algorithm> #include <numeric> #include <string> #i…
仍旧裸敲并查集.有这两点注意: 1.输入 0 0 时候要输出YES 2.留心数组的初始化 #include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <numeric> #include <algorithm> #include <cctype> #include <string> using name…
什么也不用说,并查集裸题,直接盲敲即可. #include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <cctype> #include <cmath> #include <algorithm> #include <numeric> using namespace std; ]; int getFath…