题目链接: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. e615. Finding the Next Focusable Component

    public Component findNextFocus() { // Find focus owner Component c = KeyboardFocusManager.getCurrent ...

  2. JAVA平台在手机上广泛应用

    JAVA平台由于在手机上广泛应用,使得扩展名为jar的游戏成为目前手机游戏市场上最大的家族,直接传入手机直接安装即可. 众所周知,JAVA是一种跨平台的程序设计语言.由于其高可移植性.简单.可靠.安全 ...

  3. C#提高-------------------Module的使用

    如果没有看<C#反射(一)>.建议先看<C#反射(一)>再看这一篇.上一篇文章发表,有人评论我所写的东西比较基础.其实我也知道我也只不过是在写最基础的语法而已,之所以写它是因为 ...

  4. Python——errno

    该模块实现标准的 errno 系统符号,每一个对应于一个整数,名称和描述借鉴了 linux/include/errno.h.  errno.errorcode 包含从 errno 到底层系统中错误名称 ...

  5. Win10尝鲜体验——初识传说中不一样的Windows 分类: 资源分享 2015-07-24 18:27 13人阅读 评论(0) 收藏

    这几天,网上传来一个消息,虽然不知是好是坏,Win10可以下载安装了! 出于好奇,下载尝鲜,几个截图,留作纪念~ 中文,还是要好好支持的,毕竟中国有如此多的用户 可选的安装版本 许可条款也刚刚出炉,估 ...

  6. CURL 常用参数

    在Linux中curl是一个利用URL规则在命令行下工作的文件传输工具,可以说是一款很强大的http命令行工具. 1.查看响应头信息: -I :显示http response的头信息. [root@l ...

  7. PHP和Apache结合 Apache默认虚拟主机

  8. UNIX环境编程学习笔记(25)——信号处理进阶学习之 sigaction 函数

    lienhua342014-11-05 sigaction 函数跟 signal 函数一样,用于设置信号处理函数.此函数是用于取代 UNIX 早期版本使用的 signal 函数.UNIX 早期版本的 ...

  9. windows 环境内网超快同步 DFS

    记录下: 在WINDOWS环境下,内网同步使用DFS可以超快实现文件同步,效果非常OK 纯粹记录下!

  10. linux中通过awk进行文本的对齐格式化处理?awk printf左对齐?

    需求描述: 今天在对一个从excel文件中粘出来的内容进行整理的时候,发现格式很乱,就想用awk工具格式化一下,在此记录一下. 操作过程: 1.从excel中复制出来的内容 job_name    j ...