time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

A tree is an undirected connected graph without cycles.

Let's consider a rooted undirected tree with n vertices, numbered 1 through n. There are many ways to represent such a tree. One way is to create an array with n integers p1, p2, ..., pn, where pi denotes a parent of vertex i (here, for convenience a root is considered its own parent).

For this rooted tree the array p is [2, 3, 3, 2].

Given a sequence p1, p2, ..., pn, one is able to restore a tree:

  1. There must be exactly one index r that pr = r. A vertex r is a root of the tree.
  2. For all other n - 1 vertices i, there is an edge between vertex i and vertex pi.

A sequence p1, p2, ..., pn is called valid if the described procedure generates some (any) rooted tree. For example, for n = 3 sequences(1,2,2), (2,3,1) and (2,1,3) are not valid.

You are given a sequence a1, a2, ..., an, not necessarily valid. Your task is to change the minimum number of elements, in order to get a valid sequence. Print the minimum number of changes and an example of a valid sequence after that number of changes. If there are many valid sequences achievable in the minimum number of changes, print any of them.

Input

The first line of the input contains an integer n (2 ≤ n ≤ 200 000) — the number of vertices in the tree.

The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ n).

Output

In the first line print the minimum number of elements to change, in order to get a valid sequence.

In the second line, print any valid sequence possible to get from (a1, a2, ..., an) in the minimum number of changes. If there are many such sequences, any of them will be accepted.

Examples
input
4
2 3 3 4
output
1
2 3 4 4
input
5
3 2 2 5 3
output
0
3 2 2 5 3
input
8
2 3 5 4 1 6 6 7
output
2
2 3 7 8 1 6 6 7
Note

In the first sample, it's enough to change one element. In the provided output, a sequence represents a tree rooted in a vertex 4 (becausep4 = 4), which you can see on the left drawing below. One of other correct solutions would be a sequence 2 3 3 2, representing a tree rooted in vertex 3 (right drawing below). On both drawings, roots are painted red.

破圈法

#include<iostream>
#include<stdio.h>
#define maxx 200005
using namespace std;
int root=;
int ci=;
int ans=;
int a[maxx];
int vis[maxx];
void find(int x){
vis[x]=ci;
while(!vis[a[x]]){
x=a[x];
vis[x]=ci;
}
if(vis[a[x]]==ci){
if(root==){
root=x;
}
if(a[x]!=root){
a[x]=root;
ans++;
}
}
ci++;
}
int main(){
int n;
scanf("%d",&n);
for(int i=;i<=n;i++) vis[i]=;
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
if(i==a[i]) root=i; }
for(int i=;i<=n;i++){
if(!vis[i]){
find(i);
}
}
printf("%d\n",ans);
for(int i=;i<n;i++){
printf("%d ",a[i]);
}
printf("%d\n",a[n]);
return ;
}

cf #363 d的更多相关文章

  1. cf #363 c

    C. Vacations time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  2. cf #363 b

    B. One Bomb time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...

  3. cf #363 a

    A. Launch of C time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

  4. ORA-00494: enqueue [CF] held for too long (more than 900 seconds) by 'inst 1, osid 5166'

    凌晨收到同事电话,反馈应用程序访问Oracle数据库时报错,当时现场现象确认: 1. 应用程序访问不了数据库,使用SQL Developer测试发现访问不了数据库.报ORA-12570 TNS:pac ...

  5. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

  6. cf Round 613

    A.Peter and Snow Blower(计算几何) 给定一个点和一个多边形,求出这个多边形绕这个点旋转一圈后形成的面积.保证这个点不在多边形内. 画个图能明白 这个图形是一个圆环,那么就是这个 ...

  7. ARC下OC对象和CF对象之间的桥接(bridge)

    在开发iOS应用程序时我们有时会用到Core Foundation对象简称CF,例如Core Graphics.Core Text,并且我们可能需要将CF对象和OC对象进行互相转化,我们知道,ARC环 ...

  8. [Recommendation System] 推荐系统之协同过滤(CF)算法详解和实现

    1 集体智慧和协同过滤 1.1 什么是集体智慧(社会计算)? 集体智慧 (Collective Intelligence) 并不是 Web2.0 时代特有的,只是在 Web2.0 时代,大家在 Web ...

  9. CF memsql Start[c]UP 2.0 A

    CF memsql Start[c]UP 2.0 A A. Golden System time limit per test 1 second memory limit per test 256 m ...

随机推荐

  1. Asp.Net MVC part1

    路由简介在Global中注册了路由数据包括:默认Controller,默认Action,请求地址匹配路由规则 约定大于配置为了尽量少的配置,于是将常用的配置作为默认约定,如果不同则进行少量配置主要从存 ...

  2. 用fastjson反序列化 带泛型的类

    ServiceResult<User> serviceResult = JSON.parseObject(obtainResult, new TypeReference<Servic ...

  3. 在WPF中合并两个ObservableCollection

    WPF中的ObservableCollection是一个非常常用的集合对象,我们可以通过将它绑定到ListBox之类的集合控件上时,当集合发生变更时,会同步更新到界面上.但是,有的时候我们需要合并两个 ...

  4. SQL Server Wait Types Library

    https://www.sqlskills.com/blogs/paul/announcing-the-comprehensive-sql-server-wait-types-and-latch-cl ...

  5. 【SQL】查询数据库中某个字段有重复值出现的信息

    select name,mobile from [GeneShop].[dbo].[xx_member] where mobile in ( SELECT mobile FROM [GeneShop] ...

  6. Key-Value Observing (键值监測)

    Key-Value Observing (键值监測) 简单介绍 KVO是一套当目标对象的属性值改变时观察者对象能够接受到通知的机制.必须先理解KVC才干更好的理解KVO,前者是后者的实现基础. 这种通 ...

  7. 第五章:关于ESearch的应用

    ElasticSearch是一个开源的分布式搜索引擎,具备高可靠性,支持非常多的企业级搜索用例. 我在这里做一下简单介绍和整理: 首先.ES搜索引擎给予java开放使用之前必须安装java环境,使用j ...

  8. Idea闪退问题-内存不能给太大

    Idea闪退问题-内存不能给太大 学习了:https://blog.csdn.net/qq_17776287/article/details/77529455 学习了:https://blog.csd ...

  9. EasyBoot使用方法

    1 修改背景图片直接替换掉EasyBoot\disk1\ezboot目录下面的BACK.BMP文件即可.但是限于DOS功能限制,只能使用640×480像素,256位色的BMP图片.   2 鼠标左键单 ...

  10. 程序员之---C语言细节24(段错误、类型提升、sizeof &#39;A&#39;)

    主要内容:段错误.类型提升.sizeof  'A' #include <stdio.h> int main() { union test{ char a[10]; int b; }u; i ...