CodeForces - 755C PolandBall and Forest (并查集)
题意:给定n个数,Ai的下标为1~n。对于每一个i,Ai与i在同一个树上,且是与i最远的点中id最小的点(这个条件变相的说明i与Ai连通)。求森林中树的个数。
分析:若i与Ai连通,则在同一个树上,因此连通块的个数就是树的个数。并查集即可。
#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
typedef long long ll;
typedef unsigned long long llu;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const ll LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {, , -, , -, -, , };
const int dc[] = {-, , , , -, , -, };
const int MOD = 1e9 + ;
const double pi = acos(-1.0);
const double eps = 1e-;
const int MAXN = + ;
const int MAXT = + ;
using namespace std;
int fa[MAXN];
int a[MAXN];
set<int> s;
int Find(int v){
return fa[v] = (fa[v] == v) ? v : Find(fa[v]);
}
int main(){
for(int i = ; i < MAXN; ++i){
fa[i] = i;
}
int n;
scanf("%d", &n);
for(int i = ; i <= n; ++i){
scanf("%d", &a[i]);
int tx = Find(i);
int ty = Find(a[i]);
if(tx < ty) fa[ty] = tx;
else fa[tx] = ty;
}
for(int i = ; i <= n; ++i){
if(Find(i) == i) s.insert(i);
if(Find(a[i]) == a[i]) s.insert(a[i]);
}
printf("%d\n", s.size());
return ;
}
CodeForces - 755C PolandBall and Forest (并查集)的更多相关文章
- CodeForces 755C PolandBall and Forest (并查集)
题意:给定每一点离他最远的点,问是这个森林里有多少棵树. 析:并查集,最后统计不同根结点的数目即可. 代码如下: #pragma comment(linker, "/STACK:102400 ...
- codeforces 755C. PolandBall and Forest
C. PolandBall and Forest time limit per test 1 second memory limit per test 256 megabytes input stan ...
- Codeforces 699D Fix a Tree 并查集
原题:http://codeforces.com/contest/699/problem/D 题目中所描述的从属关系,可以看作是一个一个块,可以用并查集来维护这个森林.这些从属关系中会有两种环,第一种 ...
- Codeforces 731C:Socks(并查集)
http://codeforces.com/problemset/problem/731/C 题意:有n只袜子,m天,k个颜色,每个袜子有一个颜色,再给出m天,每天有两只袜子,每只袜子可能不同颜色,问 ...
- codeforces 400D Dima and Bacteria 并查集+floyd
题目链接:http://codeforces.com/problemset/problem/400/D 题目大意: 给定n个集合,m步操作,k个种类的细菌, 第二行给出k个数表示连续的xi个数属于i集 ...
- Codeforces 1027F Session in BSU - 并查集
题目传送门 传送门I 传送门II 传送门III 题目大意 有$n$门科目有考试,第$i$门科目有两场考试,时间分别在$a_i, b_i\ \ (a_i < b_i)$,要求每门科目至少参加 ...
- CodeForces - 455C Civilization (dfs+并查集)
http://codeforces.com/problemset/problem/455/C 题意 n个结点的森林,初始有m条边,现在有两种操作,1.查询x所在联通块的最长路径并输出:2.将结点x和y ...
- Codeforces 859E Desk Disorder:并查集【两个属性二选一】
题目链接:http://codeforces.com/problemset/problem/859/E 题意: 有n个人,2n个座位. 给出这n个人初始的座位,和他们想坐的座位. 每个人要么坐在原来的 ...
- Codeforces 651E Table Compression【并查集】
题目链接: http://codeforces.com/problemset/problem/650/C 题意: 给定n*m的矩阵,要求用最小的数表示每个元素,其中各行各列的大小关系保持不变. 分析: ...
随机推荐
- Android学习笔记17:单项选择RadioButton和多项选择CheckBox的使用
请参见 http://www.android100.org/html/201406/05/19495.html
- C++中文件的读取操作,如何读取多行数据,如何一个一个的读取数据
练习8.1:编写函数.接受一个istream&参数,返回值类型也是istream&.此函数必须从给定流中读取数据,直至遇到文件结束标识时停止. #include <iostrea ...
- IDEA导入 Eclipse项目
选址 import 导入项目,选择create..., 下一步...下一步 导入项目后: 点击config 点击OK 然后添加artifacts 即可运行项目. IDEA 添加artifacts 方法 ...
- URL构成及各个协议默认端口
url的构成:一般来说,http请求都会和URL地址有关,对于url来说一般由下面5个部分构成 .协议:通常就是第一个冒号之前的内容常见协议:http,https(http+ssl),ftp,ssh, ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 按钮:制作一个大按钮
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- 为何以及如何学Linux系统?
在当今的社会中,linux用处实在是太过广泛了.现在用在服务器和嵌入式上的Linux发行版本数不胜数,桌面上linux只占1%的比例,但这不代表linux比windows和mac 做得差,实际上桌面系 ...
- 新闻网大数据实时分析可视化系统项目——13、Cloudera HUE大数据可视化分析
1.Hue 概述及版本下载 1)概述 Hue是一个开源的Apache Hadoop UI系统,最早是由Cloudera Desktop演化而来,由Cloudera贡献给开源社区,它是基于Python ...
- 十五、web中处理乱码问题总结
一.jsp变成之道---中文乱码 jsp在转换为Servlet的过程经过三次编码转化: 转自 http://www.cnblogs.com/chenssy/p/4235191.html 二.java ...
- linux环境下安装solr
1.上传并解压solr文件 2.将solr解压缩包的dist/solr-4.10.3.war包部署到tomcat下.并改名为solr.war 3.解压war包(启动tomcat后会自动解压war包) ...
- struts的错误回显