题目链接: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的更多相关文章

  1. 8VC Venture Cup 2017 - Elimination Round

    传送门:http://codeforces.com/contest/755 A题题意是给你一个数字n,让你找到一个数字m,使得n*m+1为合数,范围比较小,直接线性筛出1e6的质数,然后暴力枚举一下就 ...

  2. 解题报告8VC Venture Cup 2017 - Elimination Round

    题目链接:http://codeforces.com/contest/755 本蒟蒻做了半天只会做前两道题.. A. PolandBall and Hypothesis 题意:给出n,让你找出一个m, ...

  3. 8VC Venture Cup 2017 - Elimination Round - B

    题目链接:http://codeforces.com/contest/755/problem/B 题意:给定PolandBall 和EnemyBall 这2个人要说的单词,然后每一回合轮到的人要说一个 ...

  4. 8VC Venture Cup 2017 - Elimination Round - A

    题目链接:http://codeforces.com/contest/755/problem/A 题意:给定一个正整数N,问你是否存在一个数M使得N*M+1不是素数(M的范围在[1,1000]). 思 ...

  5. 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 ...

  6. 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 ...

  7. 8VC Venture Cup 2016 - Elimination Round

    在家补补题   模拟 A - Robot Sequence #include <bits/stdc++.h> char str[202]; void move(int &x, in ...

  8. 8VC Venture Cup 2016 - Elimination Round (C. Block Towers)

    题目链接:http://codeforces.com/contest/626/problem/C 题意就是给你n个分别拿着2的倍数积木的小朋友和m个分别拿着3的倍数积木的小朋友,每个小朋友拿着积木的数 ...

  9. 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 + ...

随机推荐

  1. JS中判断一个数组是否有相同数据的

    页面中有多个<select> $("select").each(function(){ str.push($(this).val());}); // join() 方法 ...

  2. vuex的mapState方法来获取vuex的state对象中属性

    有两种写法 1.首先在组件中引入vuex的mapState方法: 首先在组件中引入vuex的mapState方法: import { mapState } from 'vuex' 然后在compute ...

  3. Centos抓包方法

    1. 安装tcpdump工具 rpm -ql tcpdump #查看tcpdump是否安装 本机是安装的,yum安装: yum install tcpdump 2.  tcpdump抓包 根据协议和端 ...

  4. 命令——tr

    文本处理工具命令——tr 一帮助说明 TR() User Commands TR() NAME tr - translate or delete characters SYNOPSIS tr [OPT ...

  5. maven编译问题之 -The POM for XXX is invalid, transitive dependencies (if any) will not be available

    问题一: 把父工程tao-parent install 到maven本地仓后,接着install tao-common工程,然后报错 报错信息如下: [WARNING] The POM for com ...

  6. XMLUtil

    package Testlink; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; imp ...

  7. Dubbo 在跨语言和协议穿透性方向的探索:支持 HTTP/2 gRPC

    Dubbo 在跨语言和协议穿透性方向上的探索:支持 HTTP/2 gRPC 和 Protobuf 本文整理自刘军在 Dubbo 成都 meetup 上分享的<Dubbo 在多语言和协议穿透性方向 ...

  8. [design pattern](0) 概述

    一 引语 大家好,这是我第一次在网上写文章.从学校毕业一年多,感觉还有很多东西需要去学习.最近正在学习设计模式,希望可以在博客园把我学习的知识记录下来,能够和大家一起讨论设计模式相关的话题,也希望这个 ...

  9. WINDOWS2008server安全策略设置

    一.防止黑客或恶意程序暴力破解我的系统密码 答: 暴力破解Windows密码实质上是通过穷举算法来实现,尤其是密码过于简单的系统,暴力破解的方法还是比较实用的.有一点需要我们注意,这个问题的关键在于W ...

  10. (转)Kubernetes 配置Pod和容器(十七) 使用Secrets管理安全证书

    转:https://www.jianshu.com/p/530b3642c642 本章节展示了如何把密码秘钥等敏感数据安全的注入到Pod里面. 转换安全数据成base-64表示 假设你有两个秘密数据: ...