PolandBall and Forest
PolandBall lives in a forest with his family. There are some trees in the forest. Trees are undirected acyclic graphs with k vertices and k - 1 edges, where k is some integer. Note that one vertex is a valid tree.
There is exactly one relative living in each vertex of each tree, they have unique ids from 1 to n. For each Ball i we know the id of its most distant relative living on the same tree. If there are several such vertices, we only know the value of the one with smallest id among those.
How many trees are there in the forest?
The first line contains single integer n (1 ≤ n ≤ 104) — the number of Balls living in the forest.
The second line contains a sequence p1, p2, ..., pn of length n, where (1 ≤ pi ≤ n) holds and pi denotes the most distant from Ball i relative living on the same tree. If there are several most distant relatives living on the same tree, pi is the id of one with the smallest id.
It's guaranteed that the sequence p corresponds to some valid forest.
Hacking: To hack someone, you should provide a correct forest as a test. The sequence p will be calculated according to the forest and given to the solution you try to hack as input. Use the following format:
In the first line, output the integer n (1 ≤ n ≤ 104) — the number of Balls and the integer m (0 ≤ m < n) — the total number of edges in the forest. Then m lines should follow. The i-th of them should contain two integers ai and bi and represent an edge between vertices in which relatives ai and bi live. For example, the first sample is written as follows:
5 3
1 2
3 4
4 5
You should output the number of trees in the forest where PolandBall lives.
From the technical side, this problem is interactive. However, it should not affect you (except hacking) since there is no interaction.
5
2 1 5 3 3
2
1
1
1
In the first sample testcase, possible forest is: 1-2 3-4-5.
There are 2 trees overall.
In the second sample testcase, the only possible graph is one vertex and no edges. Therefore, there is only one tree.
并查集模版?
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <queue>
#include <map>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <numeric>
#include <cmath>
#include <unordered_set>
#include <unordered_map>
//#include <xfunctional>
#define ll long long
#define mod 998244353
using namespace std;
int dir[][] = { {,},{,-},{-,},{,} };
const long long inf = 0x7f7f7f7f7f7f7f7f;
const int INT = 0x3f3f3f3f;
int n;
vector<int> a;
int finds(int x)
{
return x == a[x] ? x : finds(a[x]);
}
int main()
{
int n;
cin >> n;
a.resize(n + );
for (int i = ; i <= n; i++)
{
a[i] = i;
}
for (int i = ; i <= n; i++)
{
int m, t1, t2;
cin >> m;
t1 = finds(i);
t2 = finds(m);
if (t2 != t1)
a[t2] = t1;
}
int ans = ;
for (int i = ; i <= n; i++)
{
if (i == a[i])
ans++;
}
cout << ans;
return ;
}
PolandBall and Forest的更多相关文章
- codeforces 755C. PolandBall and Forest
C. PolandBall and Forest time limit per test 1 second memory limit per test 256 megabytes input stan ...
- 【codeforces 755C】PolandBall and Forest
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- CodeForces 755C PolandBall and Forest (并查集)
题意:给定每一点离他最远的点,问是这个森林里有多少棵树. 析:并查集,最后统计不同根结点的数目即可. 代码如下: #pragma comment(linker, "/STACK:102400 ...
- Codeforces 755C:PolandBall and Forest(并查集)
http://codeforces.com/problemset/problem/755/C 题意:该图是类似于树,给出n个点,接下来p[i]表示在树上离 i 距离最远的 id 是p[i],如果距离相 ...
- CodeForces - 755C PolandBall and Forest (并查集)
题意:给定n个数,Ai的下标为1~n.对于每一个i,Ai与i在同一个树上,且是与i最远的点中id最小的点(这个条件变相的说明i与Ai连通).求森林中树的个数. 分析:若i与Ai连通,则在同一个树上,因 ...
- CF755C PolandBall and Forest 题解
Content 给定无向图的 \(n\) 个点的父亲节点,求无向图的联通块个数. 数据范围:\(1\leqslant n\leqslant 10^4\). Solution 并查集模板题. 我们将在当 ...
- 某5道CF水题
1.PolandBall and Hypothesis 题面在这里! 大意就是让你找一个m使得n*m+1是一个合数. 首先对于1和2可以特判,是1输出3,是2输出4. 然后对于其他所有的n,我们都可以 ...
- 基于netty轻量的高性能分布式RPC服务框架forest<下篇>
基于netty轻量的高性能分布式RPC服务框架forest<上篇> 文章已经简单介绍了forest的快速入门,本文旨在介绍forest用户指南. 基本介绍 Forest是一套基于java开 ...
- 基于netty轻量的高性能分布式RPC服务框架forest<上篇>
工作几年,用过不不少RPC框架,也算是读过一些RPC源码.之前也撸过几次RPC框架,但是不断的被自己否定,最近终于又撸了一个,希望能够不断迭代出自己喜欢的样子. 顺便也记录一下撸RPC的过程,一来作为 ...
随机推荐
- 删除Win10菜单中的幽灵菜单(ms-resource:AppName/Text )
新建一个 .bat文件,输入以下内容 @echo off taskkill /f /im explorer.exe taskkill /f /im shellexperiencehost.exe ti ...
- 剑指offer-面试题48-最长不含重复字符的子字符串-动态规划
/* 题目: 最长不含重复字符的子字符串. */ /* 思路: f(i) = f(i-1) + 1,(未出现过当前字符,distance > f(i-1) distance,当前字符和上一次出现 ...
- 【机器学习】使用CNN神经网络实现对图片分类识别及模型转换
仅做记录,后面慢慢整理 训练函数: from skimage import io, transform # skimage模块下的io transform(图像的形变与缩放)模块 import glo ...
- linux 多并发 连接限制修改
1. 修改 ulimit -a 查看 open files 表示单个用户能打开的最大句柄 如果开发的高并发当个进程打开的句柄需要很大. 修改/etc/security/limits.conf 里面有 ...
- ASP.NET Core SignalR 使用
SignalR: 实时 Web 功能使服务器端代码能够即时将内容推送到客户端(包括B/S,C/S,Andriod). SignalR最新版本为3.0(截止2020-02-28) SignalR ...
- Navigation Nightmare POJ - 1984 带权并查集
#include<iostream> #include<cmath> #include<algorithm> using namespace std; ; // 东 ...
- 数据库中的sql语句总结
初识SQL 1. 什么是SQL:结构化查询语言(Structured Query Language). 2. SQL的作用:客户端使用SQL来操作服务器. > 启动mysql.exe,连 ...
- Qt Gui 第七章
1.processEvents 该函数用来处理窗口的各种事件,重绘和event ; row < RowCount; ++row) { ; column < ColumnCount; ++c ...
- ArcMap 导入 CGCS2000 线段数据
1. 先确定数据的经纬度和X.Y列是否正确, 2. ArcToolBox ---> DataManagement Tools ---> XY to line 3. 选择数据,选择对应起始点 ...
- php 字符串常用函数
数组.字符串和数据库是我们函数里面最.最.最常用的三类函数. 当然PHP的字符串函数也有很多.我们最常使用的两个系列的字符串: 1.单字节字符串处理函数 2.多字节字符串处理函数 3.字符串编码转换函 ...