题目链接:http://codeforces.com/problemset/problem/698/B
题意:告诉你n个节点当前的父节点,修改最少的点的父节点使之变成一棵有根树。
思路:拆环。
题解:http://codeforces.com/blog/entry/46148
代码:

#include <cstdio>
const int maxn = ;
int f[maxn], vis[maxn], n, s, cnt, idx;
int Find(int x)
{
vis[x] = ++ idx;
while (!vis[ f[x] ])
{
x = f[x];
vis[x] = idx;
}
if (vis[ f[x] ] == idx)
{
if (s == )
s = x;
if (f[x] != s)
{
f[x] = s;
cnt ++;
}
}
}
int main()
{
scanf("%d", &n);
for (int i = ; i <= n; i ++)
scanf("%d" , f + i);
for (int i = ; i <= n; i ++)
if (i == f[i])
s = i;
for (int i = ; i <= n; i ++)
if (!vis[i])
Find(i);
printf("%d\n", cnt);
for (int i = ; i <= n; i ++)
printf("%d ", f[i]);
return ;
}

Codeforces Round #363 (Div. 1) B. Fix a Tree 树的拆环的更多相关文章

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

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

    D. Fix a Tree time limit per test 2 seconds memory limit per test 256 megabytes     A tree is an und ...

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

  4. Codeforces Round #379 (Div. 2) E. Anton and Tree 树的直径

    E. Anton and Tree time limit per test 3 seconds memory limit per test 256 megabytes input standard i ...

  5. Codeforces Round 363 Div. 1 (A,B,C,D,E,F)

    Codeforces Round 363 Div. 1 题目链接:## 点击打开链接 A. Vacations (1s, 256MB) 题目大意:给定连续 \(n\) 天,每天为如下四种状态之一: 不 ...

  6. Codeforces Round #363 Div.2[111110]

    好久没做手生了,不然前四道都是能A的,当然,正常发挥也是菜. A:Launch of Collider 题意:20万个点排在一条直线上,其坐标均为偶数.从某一时刻开始向左或向右运动,速度为每秒1个单位 ...

  7. Codeforces Round #603 (Div. 2) E. Editor(线段树)

    链接: https://codeforces.com/contest/1263/problem/E 题意: The development of a text editor is a hard pro ...

  8. Codeforces Round #319 (Div. 1) B. Invariance of Tree 构造

    B. Invariance of Tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/576/ ...

  9. Codeforces Round #363 (Div. 2)

    A题 http://codeforces.com/problemset/problem/699/A 非常的水,两个相向而行,且间距最小的点,搜一遍就是答案了. #include <cstdio& ...

随机推荐

  1. SAP 以工序为基准进行发料 机加工行业 Goods Issue to Routing

    SAP 以工序为基准进行发料   这个流程是在业务有关需求,业务需要按照工序发料,一个工单有多个工序,而料是要发到每个工序上,而且没到工序之间在物理上是有距离的,所以仓管员在打印配发单之后希望了解到哪 ...

  2. html5视频播放解决方案

    关键词:html5  nativeapp webapp mp4 H.264 html5没学习之前总觉的很神秘.近期通过学习和研究html5有点成果,特总结分享给大家.众所周知应用开发分两种:一是原生的 ...

  3. Java如何显示不同格式的日期?

    在Java中,如何以不同的格式来显示日期? 此示例使用DateFormatSymbols().DateFormatSymbols类的getWeekdays()方法来显示时间的格式. package c ...

  4. 使用Maven模板创建项目

    在本教程中,我们将向你展示如何使用mvn archetype:generate从现有的Maven模板列表中生成项目.在Maven 3.3.3,有超过1000+个模板,Maven 团队已经过滤掉一些无用 ...

  5. 嵌入式开发值zynq---zynq中tlv320aic23b spi的驱动移植

    http://blog.csdn.net/djason43/article/details/52876742 http://blog.csdn.net/lxmky/article/details/68 ...

  6. TCDB 数据库简介

    TCDB是对膜转运蛋白(Membrane Transport Protein)进行分类的一个数据库,它制定了一套转运蛋白分类系统(Transporter Classification), 简称TC S ...

  7. Java反射 Introspector

    一.解释 Introspector  内省,自我检查. 位于java中的java.beans包中,其原文说明文为: The Introspector class provides a standard ...

  8. 原版的WEB认证客户端,提供源代码,让用户自行编译

    今天在翻找文件,偶尔发现这个文件,就把源代码发给大家吧. 有需要的,可以尽管改,程序在D7下编译 下载地址:下载地址1

  9. PHP数组排序函数array_multisort()函数详解(二)

    array_multisort()这个函数可以对多个PHP数组进行排序,排序结果是所有的数组都按第一个数组的顺序进行排列 例如array_multisort($a,$b),$a,$b是两个数组,如果排 ...

  10. Spock集成入门

    本文基于SpringBoot 在pom.xml添加Spock依赖 <!-- test --> <dependency> <groupId>org.codehaus. ...