cf #363 d
2 seconds
256 megabytes
standard input
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:
- There must be exactly one index r that pr = r. A vertex r is a root of the tree.
- 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.
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).
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.
4
2 3 3 4
1
2 3 4 4
5
3 2 2 5 3
0
3 2 2 5 3
8
2 3 5 4 1 6 6 7
2
2 3 7 8 1 6 6 7
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的更多相关文章
- cf #363 c
C. Vacations time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
- cf #363 b
B. One Bomb time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...
- cf #363 a
A. Launch of C time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- 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 ...
- cf之路,1,Codeforces Round #345 (Div. 2)
cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅..... ...
- cf Round 613
A.Peter and Snow Blower(计算几何) 给定一个点和一个多边形,求出这个多边形绕这个点旋转一圈后形成的面积.保证这个点不在多边形内. 画个图能明白 这个图形是一个圆环,那么就是这个 ...
- ARC下OC对象和CF对象之间的桥接(bridge)
在开发iOS应用程序时我们有时会用到Core Foundation对象简称CF,例如Core Graphics.Core Text,并且我们可能需要将CF对象和OC对象进行互相转化,我们知道,ARC环 ...
- [Recommendation System] 推荐系统之协同过滤(CF)算法详解和实现
1 集体智慧和协同过滤 1.1 什么是集体智慧(社会计算)? 集体智慧 (Collective Intelligence) 并不是 Web2.0 时代特有的,只是在 Web2.0 时代,大家在 Web ...
- 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 ...
随机推荐
- 3.3常用类(java学习笔记)Runtime与Process
一.Runtime 我们来看下文档中对Runtime的说明: 每一个java程序都有一个属于Runtime类的实例,它允许程序连接到程序运行环境. 当前runtime可以用getRuntime()方法 ...
- iOS开发——MJExtension复杂数组用法
最近在看MJExtension的Demo,发现了一个plist文件直接转数组模型的方法.以前研究过但是浅尝辄止没有解决,这几天有时间,好好看了看,找到了解决办法,与大家分享. 如果大家的项目中有这种嵌 ...
- Chrome好用的扩展插件
LastPassChrome上最好用的密码管理软件,支持密码自动填充.生成密码
- Kingdee Apusic 中间件有关资料
Kingdee Apusic 中间件有关资料: 1.官方网站:http://www.apusic.com 2.资料目录:http://www.apusic.com/dist 3.Apusic 8 资料 ...
- 采用Apache作为WebLogic Server集群的负载均衡器
强烈建议不要使用WebLogic ClusterServlet作为Proxy进行生产环境的负载均衡, 那个是用来进行集群的功能测试的,Oracle的产品文挡也写得比较清楚. 如果采用软件的负载均衡,可 ...
- LVM分区管理和扩展
一.LVM简介 LVM是 Logical Volume Manager(逻辑卷管理)的简写.LVM将一个或多个硬盘的分区在逻辑上集合,相当于一个大硬盘来使用,当硬盘的空间不够使用的时候,可以继续将其它 ...
- windows server 2012将计算机图标添加到桌面(图文教程)(转)
windows server 2012系统安装完以后桌面默认只有回收站一个图标,如何将window常用的图标(计算机.控制面板.网络.用户文件)的图标添加到桌面呢,下面为作者本人亲测.操作简单至极. ...
- 基于CentOS与VmwareStation10搭建Oracle11G RAC 64集群环境:3.安装Oracle RAC-3.3.安装前检查
3.3. 安装前检查 1.检查节点连通性 或:./runcluvfy.sh stage -pre crsinst -n linuxrac1,linuxrac2 -fixup -verbose [gri ...
- django admin富文本编辑kindeditor
最近在做django项目,需要在后台管理系统加入富文本编辑 其实加入富文本编辑很简单,就是导入几个编辑器的js脚本到admin页面内,下面说说怎么做 第一步,下载想要的富文本编辑器如kindedito ...
- ibatis传入list对象
在使用ibatis的时候经常需要传入list对象,sql语句如下. <select id="GET-PERSONS" parameterClass="java.ut ...