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 ...
随机推荐
- cocos2d-x引擎优化 修改记录
3.13 一些无聊的bug etc1 在android 平台不能正常显示,包括 cc.Button,setGray,clipeNode 3.13.1优化记录 7月 2.公式计算改为长整型, ...
- Java高级架构师(一)第17节:X-gen生成所需的DAO部分模板
- Linux下Shell脚本替换换行符(转)
说明:过滤换行符确实有点吭,不是那么好弄.处理文本时需要将换行符替换为空格,若使用sed命令会比较麻烦,而使用tr命令非常方便. 输入文本(country.txt) China America Fra ...
- 为ASP.NET WEB API生成人性化说明文档
一.为什么要生成说明文档 我们大家都知道,自己写的API要供他人调用,就需要用文字的方式将调用方法和注意事项等写成一个文档以更好的展示我们设计时的想法和思路,便于调用者更加高效的使用我们的API. 当 ...
- t-sql 笔记(1)
Toad-for-SQL-Server-Freeware 1.查询哪些数据库对象使用了某个表 SELECT b.[name], a.[definition] FROM sys.all_sql_modu ...
- leetcode练习之No.7------ 翻转整数reverse_integer
原文地址:http://www.niu12.com/article/48 git地址:git@github.com:ZQCard/leetcode.git 给定一个 32 位有符号整数,将整数中的数字 ...
- MVC Movie App
ylbtech- ASP.NET MVC:MVC Movie App 功能描述:MVC Movie App 2,TechnologyAndTheEnvironment(技术与环境) 操作系统: win ...
- osgMulitiplerendertargets sample 中fbo使用【HTC VIVE开发中应用】
osgmultiplerendertargets.cpp ...................................... // now create the camera to do t ...
- SVN服务的部署及使用
环境说明 系统版本 CentOS 7.2 x86_64 SVN是Subversion的简称,是一个开放源代码的版本控制系统,相较于RCS.CVS,它采用了分支管理系统,它的设计目标就是 ...
- SQL查询刚開始学习的人指南读书笔记(一)关系数据库和SQL介绍
PART I:Relational Databases and SQL Chapter2, Ensuring Your Database Structure IsSound. 数据库设计准则 什么是关 ...