Codeforces Round #363 (Div. 1) B. Fix a Tree 树的拆环
题目链接: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 树的拆环的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- Codeforces Round 363 Div. 1 (A,B,C,D,E,F)
Codeforces Round 363 Div. 1 题目链接:## 点击打开链接 A. Vacations (1s, 256MB) 题目大意:给定连续 \(n\) 天,每天为如下四种状态之一: 不 ...
- Codeforces Round #363 Div.2[111110]
好久没做手生了,不然前四道都是能A的,当然,正常发挥也是菜. A:Launch of Collider 题意:20万个点排在一条直线上,其坐标均为偶数.从某一时刻开始向左或向右运动,速度为每秒1个单位 ...
- Codeforces Round #603 (Div. 2) E. Editor(线段树)
链接: https://codeforces.com/contest/1263/problem/E 题意: The development of a text editor is a hard pro ...
- 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/ ...
- Codeforces Round #363 (Div. 2)
A题 http://codeforces.com/problemset/problem/699/A 非常的水,两个相向而行,且间距最小的点,搜一遍就是答案了. #include <cstdio& ...
随机推荐
- ubuntu lapack安装与使用
https://blog.csdn.net/zouyu1746430162/article/details/53374693 https://blog.csdn.net/mlnotes/article ...
- SAP财务供应链与金库管理的联系与区别
SAP Treasure Module & Cash Fund , Risk Management 本文简要阐述一下什么是财务供应链管理(FSCM),什么是金库管理(Treasury Ma ...
- (笔记)Ubuntu下安装arm-linux-gcc-4.4.3.tar.gz (交叉编译环境)
参考了前人的成果,结合自己实践,arm-linux-gcc-4.4.3.tar.gz的下载地址为:http://ishare.iask.sina.com.cn/f/13836544.html?from ...
- MyBatis环境配置及入门
Mybatis 开发环境搭建,选择: MyEclipse8.5 版本,mysql 5.5, jdk 1.8, mybatis3.2.3.jar 包.这些软件工具均可以到各自的官方网站上下载. 整个过程 ...
- Spring JDBC对象批量操作
以下示例将演示如何使用spring jdbc中的对象进行批量更新.我们将在单次批次操作中更新student表中的记录. student表的结果如下 - CREATE TABLE student( id ...
- (转)live555学习笔记-UsageEnvironment和TaskScheduler
2011-12-6阅读1264 评论1 一直想学习流媒体服务器的设计,这几天有点时间,看了一下live555的源代码.live555是一个开源的跨平台流媒体服务器,使用编程语言是C++.将现阶段学习笔 ...
- 浅析Android Camera开发中的三个尺寸和三种变形 (贡献一个自适配Picturesize和Previewsize的工具类)
转至 (http://blog.csdn.net/yanzi1225627/article/details/17652643) 经常听人问Camera开发中,各种变形问题,今天有空就在此梳理总结下. ...
- VMware安装与VMware下安装CentOS系统
1.下载安装VMware,我安装的是VMware 12.VMware从11开始不再支持32位系统,32位系统请安装VMware10. VMware官方功能特性介绍http://www.vmware.c ...
- ubuntu下gedit和vim输入中文和中文显示
安装和配置VIM,参考 http://jingyan.baidu.com/album/046a7b3efd165bf9c27fa915.html?picindex=4 在home/你的用户名 这个 ...
- JSTL XML标签库 使用
推荐博客:http://blog.sina.com.cn/s/blog_4f925fc30101820u.html XML标签库 JSTL提供了操作xml文件的标签库,使用xml标签库可以省去使用Do ...