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. 阅读详情

相关推荐

东方财富API隐藏技巧:这样获取分时均价线数据更稳定(避坑指南)

本文深入解析东方财富API获取股票分时数据,特别是分时均价线的实战技巧与避坑指南。针对高频调用中常见的请求失败、数据错位及IP限制等问题,提供了从接口参数深层解析、稳健请求与错误处理机制,到系统优化与数据校验的全流程解决方案,帮助开发者构建稳定可靠的数据采集系统。

rice5的博客 2573

图论——cf 699 D

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

我怎么这么菜的呢 962

Zemax序列模式下激光二极管(LD)光源建模与像散校正实战

本文详细讲解了在Zemax序列模式下精确建立激光二极管(LD)光源模型并校正其像散的核心方法。通过将椭圆光束的发散角转换为渐晕系数,并巧妙利用理想柱面镜模拟像散,实现了对LD光源特性的高精度模拟。该方法特别适用于激光耦合、激光雷达等需要对像散光束进行精密控制的光学系统设计。

weixin_28455801的博客 455

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...

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

什么是PON(无源光纤网络)、PON的发展及演进

全球知名媒体Omdia在光纤和铜缆接入设备预测报告中对2022年2月的预测显示,PON设备端口收入预计在2020年至2027年期间以12.3%的年复合增长率增长,到2027年将达到163亿美元,远高于2020年的82亿美元。 此前,Omdia有份报告称,在3月份举行的美国OFC展会中,众多 CSP 正在顺利部署 XGS PON(10G 对称),其他 CSP 专注于基于 GPON 的低速 FTTH 服务。另外关于25G PON 和 50G PON 之间的争论仍在继续,而Omdia 认为 25G PON 和 5

4949

25、伊藤随机积分与引理的深入解析

本文深入解析了伊藤随机积分与伊藤引理的基本概念、数学定义及其在随机微积分中的应用。内容涵盖概率解法求解方程、伊藤积分的定义与性质、伊藤引理的推导与实例应用,并介绍了其在金融、物理等领域的拓展应用。同时,文章提供了数值计算方法与示例代码,帮助读者加深理解并进行实践验证。

gpu4optimizer的博客 372

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 626

大模型——Coze工作流实战:一键生成像素风格视频

本文介绍了如何利用Coze平台工作流生成像素风格心理学短视频的方法。通过创建工作流、设置变量节点、调用大模型生成文案、数据格式化处理、合成音频、生成字幕等步骤,最终实现从输入主题到生成完整视频的自动化流程。文章详细展示了每个节点的具体配置方法,包括提示词编写和数据处理脚本,为读者提供了完整的像素风格视频制作解决方案。

834

并查集判环+思维 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

不知所云的博客 325

31、摩擦学中的材料选择

本博文深入探讨了摩擦学应用中材料选择的重要性,分析了材料的机械、物理和化学属性对摩擦学性能的影响。同时介绍了材料匹配原则、处理方法及表面涂层技术,以优化耐磨性和抗腐蚀性。通过实际案例和评估方法,为读者提供了系统化的材料选择流程和决策依据。

juice的博客 257

YOLO11改进|YOLO11中引入注意力和卷积融合模块CAFMAttention

YOLO11改进|YOLO11中引入注意力和卷积融合模块CAFMAttention

A1983Z的博客 1249

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的博客 456

Python 爬虫实战:豆瓣 / IMDB 电影数据抓取(评分 + 剧情 + 演员阵容结构化处理)

在数据为王的时代,电影数据是进行市场分析、推荐系统构建和学术研究的重要资源。豆瓣和 IMDB 作为国内外最具影响力的电影评分和资讯网站,包含了海量高质量的电影数据。本文将带领你从零开始,构建一个健壮的 Python 爬虫,用于抓取豆瓣和 IMDB 的电影信息,并将其中的非结构化数据(如剧情简介、演员列表)进行清洗和结构化处理,最终存储到便于分析的结构化文件(如 CSV)中。

yansideyucsdn的博客 969

【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' 1628

visio安装包

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

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

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的博客 169

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

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

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

Draven__的博客 444

WINCC7.3破解文件

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

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的博客 459

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的博客 1518

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的博客 1009

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的博客 977

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的博客 955

hdu 4355 Party All the Time 初识三分法

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

ctrss的博客 915

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的博客 913

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的博客 744

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的博客 692
上一篇: 初识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、付费专栏及课程。

余额充值
⌂ 换网址