CF 699D Fix a Tree 并查集

cf 670D2 Magic Powder - 2 二分裸题 D2. Magic Powder - 2 time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard outputWaking up in the morning, Apollinaria decided to bake cookies. To bake o 阅读详情
D. Fix a Tree
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(because p4 = 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.

In the second sample, the given sequence is already valid.



题意:给定一个图,第i个点指向a[i],现让你改变最少的点的指向,使所有的点构成一棵生成树

思路:如果要改变最少的话,那么要修改的点只有孤立点和构成环的点,用并查集来找出这些点,并确定一个根节点(可以是孤立点,也可以是构成环的点)使得所有要修改的点直接指向这个根节点就行了


代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int fa[200005];
int find(int x)
{
	if(x==fa[x])return x;
	  else return fa[x]=find(fa[x]);
}

int main()
{
	
	int i,j,k,m,n;
	int a[200005];
	scanf("%d",&n);
	for(int i=1;i<=n;i++)scanf("%d",&a[i]);
	for(int i=1;i<=n;i++)fa[i]=i;
	int flag=0;
	int cnt=0;
	int gen=-1;
	for(int i=1;i<=n;i++)
	 if(a[i]==i)
	 {cnt++;
      gen=i;
	 }
	for(int i=1;i<=n;i++)
	{
		int xx=find(i);
		int yy=find(a[i]);
		if(a[i]==i)continue;
		if(xx==yy)
		 {
		 	a[i]=i;
		 	cnt++;
		 }
		 else 
		 fa[xx]=yy;
	}
	if(gen==-1)
	for(int i=1;i<=n;i++)
	if(a[i]==i)
	{
		gen=i;
		cnt++;
		break;
	}
	printf("%d\n",--cnt);
	for(int i=1;i<=n;i++)
	{
		if(a[i]==i)a[i]=gen;
		printf("%d ",a[i]);
	}
	return 0;
}

暑假集训也好久了,刷的题到现在都没写过博客,刷的题也不多,空闲时间似乎全无所事事掉了……还是太懒了……感觉最近陷入了“看了题解也不会做,瞎做一通也过不了样例,过得了样例也a不了题,a的了题还下次是不会做”的怪圈,前途渺茫啊。

【信息科学与工程学】计算机科学与技术——第七十七篇 系统架构设计02 编号类型模型配方企业资本运作的方法/规则/条件/依据及所有的步骤(​风险处理/异常处理/危机应对关联知识法律法规和裁决依据​​由发起人(通常是知名投资人、投行或产业资本)先设立一个只有现金、无实际业务的特殊目的收购公司(SPAC),通过IPO募集资金并在交易所上市。然后在规定时间内(通常24个月)寻找一家非上市公司进行合并(De-SPAC),使目标公司迅速实现上市,规避传统IPO的复杂流程和不确定性。1.:在开曼或特拉华注册空壳公司,注入少量种子资金。2. 阅读详情

相关推荐

OPC DCOM详细配置方法 - 不关防火墙不换登陆用户

1. 前言   OPC DCOM的设置是实现跨网络OPC通信的基础,现有的一些OPC DCOM配置教程虽然讲解了如何配置DCOM,但没有讲清楚为什么要这么做,而且各个教程涉及的配置方法也不尽相同。   为了彻底搞清楚OPC DCOM的配置过程,我重新做了两台机器的系统,一台是Windows 7 32位,一台是Windows 10 64位,并且重零开始完整的测试了正常运行OPC DCOM所需的最少的...

steventian72的博客 3万+

图论——cf 699 D

http://codeforces.com/contest/699/problem/D 我靠思路我竟然自己想出来了; 终于有点用处了5555555555555555; 就是说给你一个a[] 第i点和a[i]连接双向边; 求变成一颗树的最小改动代价; 树的跟连自己;我们发现读入就是很多个联通块,最终我们要把它变成一棵树; 考虑每个联通块; 如果联通块有n个点,必然只有n调边; 那么一

我怎么这么菜的呢 961

量化实战:如何用Barra多因子模型给你的投资组合做一次‘风险体检’?

本文详细介绍了如何利用Barra多因子模型对投资组合进行全面的风险体检,帮助投资者识别行业和风格因子的风险暴露。通过实战案例和Python代码示例,展示了数据采集、风险归因和组合优化的完整流程,提升投资组合的风险调整收益。

weixin_27215337的博客 323

并查集判环+思维 Codeforces Round #363 (Div. 2) D题 Fix a Tree

Fix a Tree 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, whe

不知所云的博客 323

【雷达仿真 | FMCW TDMA-MIMO毫米波雷达信号处理仿真(可修改为DDMA-MIMO)】

1、FMCW叫做调频连续波,调频连续波有步进调频连续波(SFMCW)、线性调频连续波(LFMCW),以及其他非线性调频连续波。LFMCW有锯齿波和三角波等等;锯齿波有恒定调制周期的锯齿波和非恒定周期的锯齿波。Introduction to mmwave sensing:FMCW radars.pdf和The fundamentals of millimeter wave sensors。2、MIMO是多输入多输出(Multiple input Multiple output),也就是多发多收。

【调皮连续波】雷达技术领域知识博主 1万+

C语言——利用矩阵LU分解法求逆、行列式

本章介绍了LU分解法,以及如何利用LU分解法求逆、行列式,针对每个公式、原理、代码进行了详细介绍,希望可以给大家带来帮助。

王元笙 1万+

Equidistant Vertices-树型dp-Codeforces Round #734 (Div. 3)

Description A tree is an undirected connected graph without cycles. You are given a tree of n vertices. Find the number of ways to choose exactly k vertices in this tree (i. e. a k-element subset of vertices) so that all pairwise distances between the sele

PushyTao的博客 455

一种四通道高精度IEPE加速度传感器控制器设计稿

一种四通道高精度IEPE加速度传感器控制器设计稿,颇具参考价值。

boyondmcu的博客 3850

And And And【2019西安邀请赛】【点分治】

题目链接 A tree is a connected graph without cycles. You are given a rooted tree withnnnodes, labeled from1 to n1ton. The tree is rooted at node11. The parent of theii-th node isf_{a_i}fai​​. The e...

既然弱小,就只顾变强就是了 410

高项49个过程,输入、工具技术、输出的思维导图

为了制定项目章程的管理方法主要是采用了数据收集技术,例如通过头脑风暴,通过召集主题专家和团队成员收集信息、解决方案,并在短时间内获得大量创意,并对大量创意进行分析和排序,选出最优化最适合本项目方案,并对项目成员的不同差异观点,进行了引导和对冲突的管理,最终有助于项目干系人的目标、成功标准的内容达成一致意见,形成完善的项目章程。在本过程中得到项目章程,在制定项目章程中需要包括信息有:项目名称、项目目标、项目总里程碑进度表、项目经理、主要干系人及财务预算信息。本过程在仅开展一次,或仅在项目的预定义点开展。

Legend_Jiang的博客 6179

AutoSAR实验视频讲解:相关资料下载

AutoSAR实验视频讲解:闪烁一颗LED实验【相关资料下载】 总目录 为了方便大家自己的实验顺利进行,这里我们准备了一些实验中可能用到的资料供大家使用 介绍 这里主要是三个文件: MyApp_Core.0是我们的用户代码,在工程集成那集将会讲解 McalConfig.arxml就是我们的Mcal导出文件,用于导入到DaVinci中生成代码 CAN.dbc文件是我们的通信矩阵文件 以上几个文件都是简单的配置,不具有实际项目的实用性,大家仅供参考 // 顺便吐槽一句:百度网盘的吃相真的难看 下载连接

雪云飞星的博客 6668

Codeforces Round #363 (Div. 2) D. Fix a Tree (并查集)

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 cre

WZJRJ28 623

2024年09月CCF-GESP编程能力等级认证Scratch图形化编程二级真题解析

小杨购物 【题目描述】 小杨有n元钱用于购物。商品A的单价是a元,商品B的单价是b元。 小杨想购买相同数量的商品A和商品B。 默认小猫角色和白色背景,编写程序请你帮小杨计算出他最多能够购买多少个商品A和商品B。 【输入描述】 新建变量“n”用于存储小杨购物的金额。 新建变量“a”用于存储商品A的价格。 新建变量“b”用于存储商品B的价格。 1≤n,a,b≤100000 如下图所示: 图片 【输出描述】 新建变量“result”用于存储小杨最多购买商品A和商品B的个数。 如下图所示:

人生就是一个不断学习的过程 494

CF#699 D. Fix a Tree (并查集)

题目点我点我点我 D. Fix a Tree time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output A tree is an undi

blanKey的博客 455

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

【CF 698B】Fix a Tree(dfs)

【CF 699D】Fix a Tree(dfs) D. Fix a Tree time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output A tree is a

ACMer' 1626

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

Codeforces Round #363 (Div. 2)D. Fix a Tree(并查集)

D. Fix a Tree 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 cycle...

weixin_30477293的博客 166

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

D - Fix a Tree (并查集+拆环)

题意: 已知有向树的定义,除了根节点外每个节点都有且仅有一条边都指向它的父亲节点,而根节点有且仅有一条边指向自己。 现在输入一张图,每个节点都有一条边指向某个点,要求修改尽量少的边,将这张图变成一颗树。\ 分析; 最终形成的树中一定只有根节点指向自己,并且除了根节点的自环外没有其他的环。 对于图中形成的一个环,我们只需要拆开一个点就能将这个环修改成一...

Draven__的博客 442

hdu 1370 中国剩余定理

首先介绍一下中国剩余定理,在寒假回来的飞机上曾经看过证明……但是看着看着就睡着了……先把方法写上,证明以后再补吧……(虽然也只会最简单的三个数的,还互素……) 中国剩余定理介绍了这样一个问题,有这样一个数x,x%a=a1,x%b=b1,x%c=c1,求这个数x。(a,b,c互素) 首先求出三个数的逆元a2,b2,c2,如,a的逆元a2就是b1*c1*a2%a==1; 于是这个

ctrss的博客 1515

poj2407欧拉函数模板

第一次接触欧拉函数,phi(x)为小于等于x且与x互质的数的个数……先介绍一些基本定理 1.定义在所有正整数上的函数成为算数函数。 2.一个算术函数f,如果对于两个互素整数m,n有f(nm)=f(n)*f(m)则称f为积性函数。如果对于任意两个正整数mn有f(nm)=f(n)*f(m)则称f为完全积性函数。 3.如果f是一个积性函数,则对于正整数n=p1^a1*p2^a2……(pi为素数),

ctrss的博客 1008

dinic网络流前向星写法

终于照着模板打了一遍前向星…… 虽然还不是太懂…… 直接上代码吧……依然是poj1273的题 #include #include #include #include #define M 400 int INF=10000001; using namespace std; struct node { int u,v,cap,next; }; node edge[M*M]; int

ctrss的博客 976

zoj 1196 Fast Food 变种区间dp

好吧,其实我觉得这个题在一开始想的时候与区间dp还是有点差别的……虽然写出来跟区间dp确实差不多 Fast Food Time Limit: 2 Seconds      Memory Limit: 65536 KB The fastfood chain McBurger owns several restaurants along a highway. Recently,

ctrss的博客 953

hdu 4355 Party All the Time 初识三分法

以前听到三分法的时候总是一脸懵逼,心想三分了怎么继续确定区间,三分的两个点又怎么确定?想破头也想不出来。 今天偷偷看了西工大某美女姐姐(好吧,虽然没见过,就这么叫吧)的博客,发现三分竟然是有特定条件的…… 先来说说三分法,据前人的研究,三分法适用于凹凸函数求它的凹凸点。与二分法不同的是三分法将mid点与r点的中间点也并入讨论,设其为mmid点。 显然,mmid>mid. 以凹函数为例子,求

ctrss的博客 914

cf676c Vasya and String 尺取法裸题

C. Vasya and String time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output High school student Vasya got a string o

ctrss的博客 910

poj1681Painter's Problem 增广矩阵消元法错误水过版

Painter's Problem Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5519   Accepted: 2660 Description There is a square wall which is made of n*n small square

ctrss的博客 742

poj 1284欧拉函数求素数元根个数

Primitive Roots Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3601   Accepted: 2101 Description We say that integer x, 0 i mod p) | 1 <= i <= p-1 } is eq

ctrss的博客 688
上一篇: 初识python 交叉排序
下一篇: HDU 5521 Meeting 化简边的最短路
ctrss
博客等级 码龄11年 1粉丝 35原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值