8VC Venture Cup 2017 - Elimination Round - C
题目链接:http://codeforces.com/contest/755/problem/C
题意:PolandBall 生活在一个森林模型的环境中,定义森林由若干树组成,定义树为K个点,K-1条无向边的图。现在给定每个家庭在对应的树上离他最远的另一个家庭的编号。问这个森林有多少棵树组成。
思路:由于只给出了每个点在树上离他最远的点的坐标而不知道整体结构,但是可以知道该点和给定离他最远的点一定是在同一颗树上,所以维护一个并查集,最后有多少个连通分量就是有多少棵树了。
import java.io.PrintWriter;
import java.util.*; public class Main {
public static final int MAXN=10000+10;
public static int fa[]=new int [MAXN];
public static int ans;
public static int find(int x){
return x==fa[x] ? (fa[x]) : (fa[x]=find(fa[x]));
}
public static void Union(int x,int y){
int rootx=find(x),rooty=find(y);
if(rootx==rooty)
{
return;
}
fa[rooty]=rootx;
ans--;
}
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int n=cin.nextInt(); ans=n;
for(int i=1;i<=n;i++){
fa[i]=i;
}
for(int i=1;i<=n;i++){
Union(i,cin.nextInt());
}
out.println(ans);
cin.close();
out.flush();
}
}
8VC Venture Cup 2017 - Elimination Round - C的更多相关文章
- 8VC Venture Cup 2017 - Elimination Round
传送门:http://codeforces.com/contest/755 A题题意是给你一个数字n,让你找到一个数字m,使得n*m+1为合数,范围比较小,直接线性筛出1e6的质数,然后暴力枚举一下就 ...
- 解题报告8VC Venture Cup 2017 - Elimination Round
题目链接:http://codeforces.com/contest/755 本蒟蒻做了半天只会做前两道题.. A. PolandBall and Hypothesis 题意:给出n,让你找出一个m, ...
- 8VC Venture Cup 2017 - Elimination Round - B
题目链接:http://codeforces.com/contest/755/problem/B 题意:给定PolandBall 和EnemyBall 这2个人要说的单词,然后每一回合轮到的人要说一个 ...
- 8VC Venture Cup 2017 - Elimination Round - A
题目链接:http://codeforces.com/contest/755/problem/A 题意:给定一个正整数N,问你是否存在一个数M使得N*M+1不是素数(M的范围在[1,1000]). 思 ...
- Codeforces Round #393 (Div. 2) (8VC Venture Cup 2017 - Final Round Div. 2 Edition)A 水 B 二分 C并查集
A. Petr and a calendar time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- 8VC Venture Cup 2016 - Elimination Round D. Jerry's Protest 暴力
D. Jerry's Protest 题目连接: http://www.codeforces.com/contest/626/problem/D Description Andrew and Jerr ...
- 8VC Venture Cup 2016 - Elimination Round
在家补补题 模拟 A - Robot Sequence #include <bits/stdc++.h> char str[202]; void move(int &x, in ...
- 8VC Venture Cup 2016 - Elimination Round (C. Block Towers)
题目链接:http://codeforces.com/contest/626/problem/C 题意就是给你n个分别拿着2的倍数积木的小朋友和m个分别拿着3的倍数积木的小朋友,每个小朋友拿着积木的数 ...
- codeforces 8VC Venture Cup 2016 - Elimination Round C. Lieges of Legendre
C. Lieges of Legendre 题意:给n,m表示有n个为2的倍数,m个为3的倍数:问这n+m个数不重复时的最大值 最小为多少? 数据:(0 ≤ n, m ≤ 1 000 000, n + ...
随机推荐
- Oracle 11g 详细安装步骤
一.Oracle 下载 注意Oracle分成两个文件,下载完后,将两个文件解压到同一目录下即可. 路径名称中,最好不要出现中文,也不要出现空格等不规则字符. 官方下地址: http://www.ora ...
- Git关联JIRA的issue
指导文章 http://www.51testing.com/html/30/n-3724930.html http://{$host_url}/help/user/project/integratio ...
- js执行上下文与执行上下文栈
一.什么是执行上下文 简单说就是代码运行时的执行环境,必须是在函数调用的时候才会产生,如果不调用就不会产生这个执行上下文.在这个环境中,所有变量会被事先提出来(变量提升),有的直接赋值,有的为默认值 ...
- php substr()函数 语法
php substr()函数 语法 作用:截取字符串 语法:substr(string,start,length)大理石平台 参数: 参数 描述 string 必需.规定要返回其中一部分的字符串. s ...
- JS自定义随机键盘
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 【前端技术】一篇文章搞掂:uni-app
语法 //列表遍历,遍历数组,第一个参数为数组中元素,可以给第二个参数作为索引值 <view v-for="(item, itemIndex) in card" :key=& ...
- SHADER 用 step替代 if-else
今天聊起这个问题,百度发现了这个优化方式: https://blog.csdn.net/liu_if_else/article/details/77455639
- 常用的Android关键词定位方法
1字符串,特征字 根据程序运行中出现的特征字词进行搜索,从而获取定位到程序的相关位置之中.以前用 得比较多,不过现在一般难以找到想要的关键词.有时候需要对特征字进行拆分来进行搜索.才 能获得一点提示. ...
- C# DataTable删除行Delete与Remove的问题
DataTable删除行使用Delete后,只是该行被标记为deleted,但是还存在,用Rows.Count来获取行数时,还是删除之前的行数,需要使用datatable.AcceptChanges( ...
- python学习笔记:unittest单元测试
单元测试:开发自测时写的代码 unittest基本原理: ♦整个平台的搭建使用的是python的unittest测试框架,这里简单介绍下unittest模块的简单应用. ♦unittest是pytho ...