8VC Venture Cup 2017 - Elimination Round C. PolandBall and Forest【并查集】

本文介绍了一种通过给定点集和每个点对应的最远点来确定森林中树的数量的方法。通过对点与其最远点进行合并操作,最终统计独立根节点的数量以确定树的数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C. PolandBall and Forest
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

PolandBall lives in a forest with his family. There are some trees in the forest. Trees are undirected acyclic graphs withk vertices and k - 1 edges, wherek 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 from1 to n. For each Balli 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?

Input

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 lengthn, where (1 ≤ pi ≤ n) holds andpi denotes the most distant from Balli 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 acorrect 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 integerm (0 ≤ m < n) — the total number of edges in the forest. Thenm lines should follow. The i-th of them should contain two integers ai andbi and represent an edge between vertices in which relativesai andbi live. For example, the first sample is written as follows:

5 3
1 2
3 4
4 5
Output

You should output the number of trees in the forest where PolandBall lives.

Interaction

From the technical side, this problem is interactive. However, it should not affect you (except hacking) since there is no interaction.

Examples
Input
5
2 1 5 3 3
Output
2
Input
1
1
Output
1
Note

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.


题目大意:

一共有N个点,接下来N个数,第i个数表示距离点i最远的点的编号,如果有相同距离最远的点,那么这个编号将是最小编号。

问这个图有几棵树。


思路:


很显然,如果对于一个点,其在树上有距离这个点最远的点,那么他两个一定属于一棵树,而非一个孤立点,那么我们将这个点和最远点进行合并,那么我们找到f【i】==i的点就是一棵树的根。

问题就是要统计根的个数啦。


Ac代码:

#include<stdio.h>
#include<string.h>
using namespace std;
int f[100550];
int a[100550];
int find(int a)
{
    int r=a;
    while(f[r]!=r)
    r=f[r];
    int i=a;
    int j;
    while(i!=r)
    {
        j=f[i];
        f[i]=r;
        i=j;
    }
    return r;
}
int merge(int a,int b)
{
    int A,B;
    A=find(a);
    B=find(b);
    if(A!=B)
    {
        f[B]=A;
    }
}
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        for(int i=1;i<=n;i++)f[i]=i;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
        }
        int output=0;
        for(int i=1;i<=n;i++)
        {
            merge(i,a[i]);
        }
        for(int i=1;i<=n;i++)
        {
            if(f[i]==i)output++;
        }
        printf("%d\n",output);
        fflush(stdout);
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值