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

题意:给你一堆点阿,线阿,环阿,让你改最小的边(指向)变成树。

思路:有环拆环指向根节点,无环直接指向根节点。除非全是环否则根节点绝对不为环中的点(否则多变     一个点),用并查集查环,没有构成环添加,构成环变指向。

问题:夹克聚聚说他的dfs盖了139ms的时间戳,弱弱写的超时了...

代码:

#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <algorithm>

using namespace std;

int father[200000 + 5];
int a[200000 + 5];
bool vis[200000 + 5];
int root = -1 ,sum;
int ffather(int x)
{
    return father[x] == x?x:father[x]=ffather(father[x]);

}

void un(int x, int y)  //合并:0-0-0-0 和 1-1-1-1,找到最边上的0,1,变成0-0-0-0==1-1-1-1
{
    x = ffather(x);
    y = ffather(y);
    if(x == y)
        return;
    else
        father[x] = y;
}
int main()
{

    int n;
    cin>>n;
    for(int i = 1; i <= n; i++)
        cin>>a[i];
    for(int i = 1; i <= n; i++)
    {
        father[i] = i;            //初始化
    }
    for(int i = 1; i <= n; i++)
    {
         if(a[i] == i)
        {
            root = a[i];          //找根节点
            break;
        }
    }
  
    for(int i = 1; i <= n; i++)
    {
        if(ffather(a[i]) == ffather(i))          //若加入i和a[i]则成环
        {
            if(root == -1)                       //给的数据全是环
            {
                root = i;
                a[i] = i;
                sum++;
            }
            else if(a[i] != root)
            {
                a[i] = root;
                sum++;
            }
        }
        else
            un(i,a[i]);
    }
    for(int i = 1; i <= n; i++)
    {
        if(i == a[i] && i != root)
        {
            a[i] = root;
            sum++;
        }
    }

    cout<<sum<<endl<<a[1];
    for(int i = 2; i <= n; i++)
        cout<<" "<<a[i];

}


AMDGPU SVM VRAM Migration 实现详解 AMD正在基于DRM SVM框架重构SVM实现,该框架即将广泛应用。本文分析了AMD开发者提交的POC验证版本的技术方案。该架构分为三层:AMDGPU SVM映射层、DRM Pagemap通用框架层和AMDGPU迁移后端硬件驱动层。系统涉及三个地址空间转换:VRAM分配器空间、HPA/PFN空间和GPU PTE地址空间。关键数据结构包括设备级迁移基础设施amdgpu_pagemap、迁移临时BO包装器amdgpu_svm_bo等。该方案通过地址空间转换实现VRAM迁移功能,目前仍处于POC阶段,设计可能变更 阅读详情

相关推荐

matlab内读取文件目录时斜杠和反斜杠的使用

@matlab内读取文件目录时斜杠和反斜杠的使用TOC matlab内读取文件目录时,斜杠和反斜杠的使用 最近被斜杠和反斜杠弄晕了,于是总结了一下,在此和大家分享一下。 代码 clear; clc; close all; I1= imread('..\zs1.jpg'); %和代码文件夹同目录的图片 (纯反斜杠) I2= imread('../zs2/zs2.j...

weixin_44456694的博客 6575

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

题目链接:http://codeforces.com/contest/699/problem/D D. Fix a Tree time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard o

A man can be destroyed, but not defeated. 327

Jetson-nano 编译实时内核并烧录

前言 之前一篇文章介绍了正常的烧录过程,使用的是默认内核,实际工作中需要对内核打补丁升级为实时内核,并对其重编译。 一、准备内容 官网大法好 官方内核下载链接 官方交叉编译器下载链接 官方文档 ubuntu主机一台 根据官网选择下载对应的文件, 1、L4TDriver Package (BSP) source jetson内核文件。 2、官方交叉编译器 因为我们不是在jetson上直接编译,而jetson是arm平台,所以要进行交叉编译。 二、使用步骤 1.解压 内核 tar -xjf public_s

ldl371122的博客 3288

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 witho

nare123的博客 303

应急响应靶场练习Linux-web-2

本次应急响应靶机采用的是知攻善防实验室的Linux-2应急响应靶机靶机下载地址为:相关账户密码: root/Inch@957821.(第一个为大写的i,记住要带最后的点.)1.该攻击者的IP为192.168.20.12.攻击方式应该是通过/index.php?user-app-register上传了一个webshell,然后使用蚁剑管理webshell之后再次上传version2.php进行提权.3.攻击者修改了phpmyadmin数据库的管理员密码。

tddkett的博客 2806

Innovus ECO技巧:用setEcoMode+ecoAddRepeater实现批量buffer替换的5个关键步骤

本文详细介绍了在Cadence Innovus工具中,利用setEcoMode与ecoAddRepeater命令实现高效、批量Buffer/Inverter替换的工程化策略。通过启用批处理模式、构建可维护的替换规则、掌握高级定位技巧以及规避布线陷阱等5个关键步骤,帮助芯片设计工程师在ECO阶段大幅提升修改效率与质量,确保设计稳定签核。

d6e7f8的博客 1122

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

题目链接题目描述 D. Fix a Tree time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output A tree is an undirected connected graph without cycles.Let’s consi

fouzhe的博客 481

超级全的PCB LAYOUT高速信号走线指南——简化应用版

在PCB Layout中,几乎涵盖了所有的高速信号走线指南。包括了TF/SD、HDMI、LVDS、DVP、eDP、MIPI、USB2.0、USB3.0、MAC、BT.1120、PCIE高速信号等等。此版本为简化版。

bhxc809751502的博客 8552

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 witho

bin_gege的博客 738

【论文硬核拆解】Ctx2Skill:让大模型自己跟自己玩,从上下文里「进化」出可复用技能

论文选择自然语言技能可解释性:技能是人类可读的 Markdown 文件,可以审查、编辑、调试可迁移性:即插即用,从一个模型到另一个模型无需参数访问:对闭源模型友好这比 SKILL0/SkillRL 等参数内化方法更实用,尤其是在商业 API 场景。

步子哥的博客 157

【WRF-Chem第二期】WRF-Chem有关 namelist 详解

【WRF-Chem第二期】WRF-Chem有关 namelist 详解

WW、forever的博客 2157

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

cf#363 B. Fix a Tree

陌上尘的博客 273

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

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

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

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

WINCC7.3破解文件

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

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 witho

Seraphimon的博客 499

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

一般做cf,我很少会看d题,前边的都写不完,哪有心思看这个。现在刷刷以前cf的后边的题目。 我思路:先用并查集查询出独立的连通块。然后每个连通块,求点双,这样就能找到改变次数。然后发现题目还要求输出所有节点父节点,我想把上边搞完后再重新建树就行了。后来发现,我的能力实现不了。 参考题解:http://blog.csdn.net/u010734277/article/details/5198043

梦幻的蔷薇色 302

Codeforces Round #363 (Div. 2) D. Fix a Tree(greedy+dsu)

题目链接 大意:给你一个序列,让你改动最少的元素使得可以构成一个有根树。 a1,a2.....ana_1,a_2.....a_na1​,a2​.....an​表示iii节点的父亲节点,有且只有一个节点i=aii=a_ii=ai​. 输出改动次数和改动后的序列。 思路:如果开始构不成树的话,必然存在环,同时也可能是有多个联通块,我们要做的就是把环去掉,把所有的联通块连在一起。 我们由于要保证改动次数...

pubgoso的博客 207

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

这道题给我们一些联通的节点,其中有环,让我们算出最少改变多少个节点的father来构建一棵树 主要是判断圆环,并且将圆环断开并且指向其他树的根(如果不止一个根节点的话),这样才是最小的改变。

hi_just_do_it的博客 341

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

题目链接:点击打开链接 题目大意:给出一个可能有环的图的父节点序列,要求修改最少的边使其变成一棵树,输出改变次数和改变后的父节点序列。 解题思路:定义一个root,表示变换后的树根。在输入的同时,如果一个点的父节点是其本身,那么把root更新为该点。然后向图中逐条加边,如果出现环 1.若此时root已经存在,那么直接把i连向root 一个栗子: 变换后 2.若此时root不

Noooooorth的专栏 546

【解题报告】Codeforces Round #363 (Div. 2)

简略的解题报告。

TRiddle的博客 1133

Codeforces Round #363 (Div. 2) Problem D - Fix a Tree(并查集 + 构造)

分析: 这题得先分析,每一个连通分量的性质: - 这个连通分量要么有一个根节点,要么有一个环,且最多只有一个环。我把这些根节点转入一个容器,把这些形成环的节点(在并查集合并连通分量的时候判断)装入另一个容器。 优先选择已经成为根节点的作为根,否则选择环节点。然后把容器里的其他所有节点连接到根。 心得: 观察性质,简化代码。#include <iostream> #include <vect

JIBANCANYANG 366

Fix a Tree【Codeforces 699 D】【dfs + 树的性质】

Codeforces Round #363 (Div. 2) D 题意:有N个点,每个点i都有一个父节点p[i],如果“i == p[i]”则是说明i结点是根结点,现在我们给出这样的1~N的p[i],这可能是不合法的,问,我们应该最少改变多少个使它变成一棵合法的树。 思路:有两种情况,一种是多棵树,另一种是一个环,也就是这个块是没有树,他们构成了一个环,如果是一个环的话,拆解环上任意一...

既然弱小,就只顾变强就是了 279
上一篇: Codeforces#363 (Div. 2) C.Vacations
下一篇: Codeforces#364(Div.2)B.Cell Not Under Attack
July_ing
博客等级 码龄11年 9粉丝 85原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值