题目链接: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. c++ 的makefile文件实例

    首先声明, 感谢九哥的帮助,因为从来没写过makefile, 所以一直是手动编译, 然后有一次写了三个文件, 需要编译, 而我只编译了一个文件, 所以一直出错, 九哥告诉我用makefile更方便, ...

  2. C语言中的循环语句练习

    注:练习题目均出自<明解C语言 入门篇> 一.do语句 1,求多个整数的和及平均值 #include<stdio.h> int main(void) { ; //和 ; //整 ...

  3. linux 日志查询

    tail -n 400 logname | grep "AAA" grep 简单使用 1.把要查询的行写到文本里面去: grep WXCP IC.NotifyIndexServer ...

  4. C# 在托盘显示图标

    //上面一行是主窗体InitializeComponent()方法中需要添加的引用 this.SizeChanged += new System.EventHandler(this.Form1_Siz ...

  5. Python资料收藏(杂乱版)

    http://blog.csdn.net/vagrxie/article/category/343814 http://blog.sina.com.cn/s/articlelist_280149524 ...

  6. 数据源(HikariCP)

    HikariCP 是一个高性能的 JDBC 连接池组件.下图是性能的比较测试结果: 自从看到了这张图,我就对于我之前一直在使用了 c3p0 产生了深深的怀疑,迫切的期望得到对应的数据来优化我的代码. ...

  7. Android带进度条的文件上传,使用AsyncTask异步任务

    最近项目中要做一个带进度条的上传文件的功能,学习了AsyncTask,使用起来比较方便,将几个方法实现就行,另外做了一个很简单的demo,希望能对大家有帮助,在程序中设好文件路径和服务器IP即可. A ...

  8. window 平台上面解决不能动态php_mysqli.dll

    今天在新服务器部署PHP+APACHE环境,启动的时候报错: PHP Startup: Unable to load dynamic library :php_mysqli.dll 解决办法: 把PH ...

  9. mysql中参数low_case_table_name的使用?不同参数值的设置有什么影响?

    需求描述: 今天一个同事问,在mysql中,默认的表名是大小写区分的吗,默认是什么设置, 如果要设置成大小写不区分的改怎么设置,是否需要进行重启.然后就进行了查询, 对于lower_case_tabl ...

  10. 既使用maven又使用lib下的Jar包

    maven 使用本地包 lib jar包 依赖一个lib目录 解决方法: # 把本地的lib加入maven编译时的依赖路径 From:http://blog.chinaunix.net/uid-231 ...