nyoj 1022 合纵连横 经典并查集
思路:关键在于并查集的删点操作。 给每个诸侯国一个另外的编号,比如box[i]表示诸侯国i现在处于第box[i]个联盟,可以随时改变它的联盟编号,并且让box[i] = k, 实现删除操作。以前联盟中的那个点是一个虚点,只是帮助子节点找到根结点。查找时通过联盟编号查询。
AC代码
#include <cstdio>
#include <cmath>
#include <cctype>
#include <algorithm>
#include <cstring>
#include <utility>
#include <string>
#include <iostream>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
#pragma comment(linker, "/STACK:1024000000,1024000000")
#define eps 1e-10
#define inf 0x3f3f3f3f
#define PI pair<int, int>
typedef long long LL;
const int maxn = 100000 + 5;
int p[maxn<<1], box[maxn], vis[maxn<<1];
int find(int x) {
return p[x] == x ? x : p[x] = find(p[x]);
}
void unionset(int x, int y) {
int px = find(box[x]), py = find(box[y]);
if(px != py) p[px] = py;
}
int cur;
void del(int x) { //删点
box[x] = cur++;
p[box[x]] = box[x];
}
int main() {
int n, m, kase = 1;
while(scanf("%d%d", &n, &m) == 2) {
cur = n;
for(int i = 0; i < n; ++i) {
p[i] = i;
box[i] = i;
}
int x, y;
char ch;
for(int i = 0; i < m; ++i) {
getchar();
scanf("%c", &ch);
if(ch == 'U') {
scanf("%d%d", &x, &y);
unionset(x, y);
}
else {
scanf("%d", &x);
del(x);
}
}
memset(vis, 0, sizeof(vis));
for(int i = 0; i < n; ++i) {
vis[find(box[i])] = 1;
}
int ans = 0;
for(int i = 0; i < cur; ++i) {
if(vis[i]) ++ans;
}
printf("Case #%d: %d\n", kase++, ans);
}
return 0;
}
如有不当之处欢迎指出!
nyoj 1022 合纵连横 经典并查集的更多相关文章
- NYOJ 1022 合纵连横 (并查集)
题目链接 描述 乱世天下,诸侯割据.每个诸侯王都有一片自己的领土.但是不是所有的诸侯王都是安分守己的,实力强大的诸侯国会设法吞并那些实力弱的,让自己的领土面积不断扩大.而实力弱的诸侯王为了不让自己的领 ...
- nyoj 1022 合纵连横【并查集节点的删除】
合纵连横 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 乱世天下,诸侯割据.每个诸侯王都有一片自己的领土.但是不是所有的诸侯王都是安分守己的,实力强大的诸侯国会设法 ...
- nyoj 1022:合纵连横(并查集删点)
题目链接 参考链接 只附代码好了 #include<bits/stdc++.h> using namespace std; ; int a[N],b[N],vis[N]; int n,m, ...
- HDU1232 畅通工程---(经典并查集应用)
http://acm.hdu.edu.cn/showproblem.php?pid=1232 畅通工程 Time Limit: 4000/2000 MS (Java/Others) Memory ...
- nyoj 1022 合纵连横 (并查集<节点删除>)
合纵连横 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 乱世天下,诸侯割据.每个诸侯王都有一片自己的领土.但是不是所有的诸侯王都是安分守己的,实力强大的诸侯国会设法 ...
- HDU 5652 India and China Origins(经典并查集)
特别经典的一个题,还有一种方法就是二分+bfs 题意:空间内n*m个点,每个点是0或者1,0代表此点可以走,1代表不能走.接着经过q年,每年一个坐标表示此点不能走.问哪年开始图上不能出现最上边不能到达 ...
- POJ 1182 食物链(经典并查集) (多组输入有时乱加也会错!)
多组输入有时乱加也会错! 这次用多组输入竟然,不用竟然对了,所以以后做题目,若是答案错误,先看加上或者删掉多组输入,看对不对 食物链 Time Limit: 1000MS Memory Lim ...
- POJ1182--食物链(经典并查集)并查集看不出来系列2
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 65906 Accepted: 19437 Description ...
- B-食物链-经典并查集
动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A. 现有N个动物,以1-N编号.每个动物都是A,B,C中的一种,但是我们并不知道它到底是哪一种. 有人用两种 ...
随机推荐
- Arrays.sort解析
Arrays.sort()解读 在学习了排序算法之后, 再来看看 Java 源码中的, Arrays.sort() 方法对于排序的实现. 都是对基本数据类型的排序实现, 下面来看看这段代码: Arra ...
- jdk源码->集合->LinkedList
类的属性 public class LinkedList<E> extends AbstractSequentialList<E> implements List<E&g ...
- Android 使用EventBus发送消息接收消息
基本使用 自定义一个类 public class LoginEvent { private String code;//是否成功 public LoginEvent(String code) { th ...
- android之间传递list
Intent intent = new Intent(getActivity(), Activity_Character.class); intent.putExtra("mlTrait&q ...
- SQL语句-INSERT语句
Insert语句 Insert语句三种写法: mysql> desc students; +-------+-------------+------+-----+---------+------ ...
- How to get started with GIT and work with GIT Remote Repo
https://www.ntu.edu.sg/home/ehchua/programming/howto/Git_HowTo.html#zz-7. 1. Introduction GIT is a ...
- 用记事本编写java中的HelloWorld
一.安装并配置jdk(图片来自百度经验) 安装JDK 选择安装目录 安装过程中会出现两次 安装提示 .第一次是安装 jdk ,第二次是安装 jre .建议两个都安装在同一个java文件夹中的不同文件夹 ...
- transfer-webpack-plugin最简使用示例
转移文件的插件 加载插件 $ npm install transfer-webpack-plugin --save-dev API new CopyWebpackPlugin(patterns: Ar ...
- template()方法
template(id, data)方法: id:必传,渲染模板的id. data:可选,一个Object对象. return:传data—>渲染完成html代码:不传data—>一个渲染 ...
- aop难点解析。
静态织入和动态织入的区别? 需求示例:假设有一个包,一个包当中有一个方法,我们想在这个方法的前后,加上环绕. 那么怎么加呢? 把知道的都说一遍. 1.建立JsonService 2.建立JSONASP ...