洛谷P4089The Bovine Shuffle S 详解

# [USACO17DEC]The Bovine Shuffle S

## 题面翻译

背景

FJ 坚信快乐的牛可以产出更多的牛奶,因此 FJ 在牛棚里安装了一个巨大的迪斯科球并且打算让奶牛们学会跳舞。

FJ在许多出名的奶牛舞中选择了一种叫做 `Bovine Shuffle` 的舞蹈。这种舞蹈由 FJ 的 $N$ 头奶牛组成。$N$ 头奶牛以一种顺序排成一行,接着表演数次 `shuffle`。每次的 `shuffle` 会将奶牛重新排列。FJ 为了让奶牛们更加快乐,让奶牛们更容易找到重新排列后的位置,他标记了 $N$ 头奶牛的位置。在最开始,所有奶牛排成一排,第一头奶牛会在位置 $1$ 上,第二只在 $2$ 上,以此类推。

我们用 $N$ 个正整数 $a_1,a_2\dots a_n$ 来描述每次的 `shuffle`。$a_i$ 说明了在位置 $i$ 上的奶牛在经过这回合的 `shuffle` 之后,会跑到位置 $a_i$ 上。令 FJ 倍感非洲的是,即使 $i$ 与$j$ 不同,$a_i$ 也可能会等于 $a_j$ !所以可能在一次 `shuffle` 后,有多头奶牛会跑到同一位置上,在这之后这群奶牛也会一同行动。

作为一名资深的养牛大户&坑牛专家的 FJ 猛然发现,无论经过多少次的 `shuffle`,一直都有 $k$ 个位置上有奶牛。FJ现在要你在 $1$ 秒内帮他得出 $k$ 的值,否则就赏你 $10^{18} \bmod10$ 大板!

输入格式

第一行包含一个整数,$N$ 

第二行包含 $N$ 个整数,描述题目中的$a_1,a_2\dots a_n$ 

输出格式

一个整数,代表 $k$

## 题目描述

Convinced that happy cows generate more milk, Farmer John has installed a giant disco ball in his barn and plans to teach his cows to dance!

Looking up popular cow dances, Farmer John decides to teach his cows the "Bovine Shuffle". The Bovine Shuffle consists of his $N$ cows ($1 \leq N \leq 100,000$) lining up in a row in some order, then performing successive "shuffles", each of which potentially re-orders the cows. To make it easier for his cows to locate themselves, Farmer John marks the locations for his line of cows with positions $1 \ldots N$, so the first cow in the lineup will be in position 1, the next in position 2, and so on, up to position $N$.


A shuffle is described with $N$ numbers, $a_1 \ldots a_N$, where a cow in position $i$ moves to position $a_i$ during the shuffle (and so, each $a_i$ is in the range $1 \ldots N$). Every cow moves to its new location during the shuffle. Unfortunately, all the $a_i$'s are not necessarily distinct, so multiple cows might try to move to the same position during a shuffle, after which they will move together for all remaining shuffles.


Farmer John notices that some positions in his lineup contain cows in them no matter how many shuffles take place. Please help him count the number of such positions.

## 输入格式

The first line of input contains $N$, the number of cows. The next line contains the $N$ integers $a_1 \ldots a_N$.

## 输出格式

Please output the number of positions that will always contain cows, no matter how many shuffles take place.

## 样例 #1

### 样例输入 #1

```
4
3 2 1 3
```

### 样例输出 #1

```
3
```

翻译:

农夫坚信快乐的牛可以产出更多的牛奶,因此FJ在牛棚里安装了一个巨大的迪斯科球并且打算让奶牛们学会跳舞。

农夫在许多出名的奶牛舞中选择了一种叫做"Bovine Shuffle"的舞蹈。这种舞蹈由FJ的NN 头奶牛组成。NN头奶牛以一种顺序排成一行,接着表演kk次"shuffle"。每次的"shuffle"会将奶牛重新排列。FJ为了让奶牛们更容易找到重新排列后的位置,他标记了NN 头奶牛的位置。在最开始,所有奶牛排成一排,第一头奶牛会在位置11上,第二只在22上,以此类推。

我们用NN 个正整数a_1,a_2,\cdots,a_na1​,a2​,⋯,an​ 来描述每次的"shuffle"。a_iai​ 说明了在位置ii 上的奶牛在经过这回合的"shuffle"之后,会跑到位置a_iai​ 上。对于不同的ii与jj,a_iai​不会等于a_jaj​. 所以在一次"shuffle"后,所有奶牛都会占据不同位置。

FJ的奶牛都有一个77位数ID, 给出奶牛的初始顺序,求出kk次"shuffle"后每个位置上奶牛的ID.

原理:数组、有向图。

一般地,用数组a储存奶牛的地址,b来储存奶牛下一步的变换地址。

附代码:

#include<iostream>

using namespace std;

int a[105],b[105],m[105];

int main()
{
	int n,k;
	cin>>n
	>>k;
	for(int i=1;i<=n;i++)
	{
		cin>>b[i];
	}
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
	}

	for(int i=0;i<k;i++)
	{

		for(int j=1;j<=n;j++)
		{
			m[b[j]]=a[j];
		}
	for(int j=1;j<=n;j++)
		{
			cout<<m[j]<<" ";
		}
		for(int j=1;j<=n;j++)
		{
			a[j]=m[j];
		}
					
		cout<<endl;
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值