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

相关推荐

深入解析UEFI PEI阶段:PeiCore的三次调用与内存管理机制

本文深入解析UEFI启动流程中的PEI阶段,重点探讨PeiCore的三次调用与内存管理机制。详细介绍了PeiCore在临时内存中的初始化、堆栈切换至永久内存的过程,以及最终交接给DXE阶段的关键步骤,为开发者提供调试技巧与实战案例,助力解决UEFI启动问题。

weixin_28452161的博客 654

图论——cf 699 D

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

我怎么这么菜的呢 962

ndnSIM学习笔记(一)

在接下来的一段时间要用ndnSIM来做自己的仿真网络,需要系统的学习相关内容,以下为内容记录。1.安装      1.1核心依赖(必需的)             • python &gt;= 2.6             • libsqlite3             • libcrypto++             • pkg-config             • Boost lib...

gaonjzzz的博客 2988

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

IBTrACS v04r01 数据提取实战:Pandas 筛选 JTWC/CMA/JMA 3机构最佳路径数据

本文详细介绍了如何使用 Pandas 高效提取和处理 IBTrACS v04r01 数据集中的最佳路径数据,特别针对 JTWC、CMA 和 JMA 三家权威机构的数据进行对比分析。通过构建可复用的数据提取函数,解决多机构数据质量差异问题,并提供高级分析与可视化应用示例,助力热带气旋研究。

clijovtbq401783153的博客 521

【图像超分】论文精读:RepSR: Training Efficient VGG-style Super-Resolution Networks with Structural Re-Paramete

摘要: 本文提出RepSR,一种基于结构重新参数化和批量归一化(BN)的高效VGG式超分辨率网络。针对SR任务中BN易引发伪影的问题,作者通过分析发现训练-测试阶段的不一致性是主因,并提出先使用小批量统计训练、后期切换为总体统计的优化策略。RepSR块设计包含干净的残差路径和带改进BN的扩展-压缩卷积路径,有效平衡了训练非线性与推理效率。实验表明,RepSR在不同模型规模下优于现有重参数化方法,且实际推理速度更快。该工作为SR提供了性能与效率兼优的解决方案,相关代码未开源。 关键词: 超分辨率、结构重参数化

主要更新底层视觉(去噪、超分等)相关的科研内容,形式为【论文精读】+【论文复现】 318

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

(秋招复习)一些SLAM基础

秋招复习--SLAM基础知识篇

weixin_45432823的博客 770

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

图解B+树的插入过程

B+ 树在现代数据库中很常见,如果我们了解它,在工作中可能对性能优化会有更好的帮助! 最近我一直在思考 B+ 树的高度是由什么决定的。知道我了解了 B+ 树的插入过程,才有一种恍然大悟的感觉! 网上的一些资料杂乱无章,不同的数据库可能还有对 B+ 树有不同的实现。但是万变不离其宗,B+ 树的定义,大致如下所示: 总结一下,B+ 树有下面 5 个重要的特点。 B+ 树包含 2 种类型的结点: 内部结点(也称索引结点)和叶子结点。 根结点本身即可以是内部结点,也可以是叶子结点。 根结点的关键字个数最

愿君出走半生,归来时仍是少年 2137

数据预处理

数据预处理 数据预处理(数据清洗)是数据挖掘过程中的重要一步,它主要包括数据清洗,数据集成,数据变换和数据规约。 在我们进行数据挖掘的过程中,大量的原始数据中存在着大量有问题的数据(脏数据)比如有缺失值的,不一致的,有异常的,大量重复的数据等。这些脏数据会严重影响到数据挖掘建模过程的执行效率,也可能会使挖掘结果产生偏差。通过数据预处理(数据清洗)可以提高数据的质量,更好的适应特定的挖掘技术及工具。...

CJW的博客 9934

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 625

huggingface下载提示huggingface_hub.errors.EntryNotFoundError:404 Client Error: (Request 1D: Root...如何解决?

🏆 本文收录于《全栈Bug调优(实战版)》专栏,致力于分享我在项目实战过程中遇到的各类Bug及其原因,并提供切实有效的解决方案。无论你是初学者还是经验丰富的开发者,本文将为你指引出一条更高效的Bug修复之路,助你早日登顶,迈向财富自由的梦想🚀!同时,欢迎大家关注、收藏、订阅本专栏,更多精彩内容正在持续更新中。让我们一起进步,Up!Up!Up!    备注: 部分问题/难题源自互联网,经过精心筛选和整理,结合数位十多年大厂实战经验资深大佬经验总结所得,数条可行方案供所需之人参考。

**My Coding Family** 1240

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

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

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

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

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

不知所云的博客 324

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

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

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

Draven__的博客 442

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

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

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

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

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

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

余额充值