Problem - D - Codeforces Fix a Tree
Problem - D - Codeforces Fix a Tree
看完第一名的代码,顿然醒悟。。。
我可以把所有单独的点全部当成线,那么只有线和环。
如果全是线的话,直接线的条数-1,便是操作数。
如果有环和线,环被打开的同时,接入到线上。那就是线和环的总数-1.
如果只有环的话,把所有的环打开,互相接入,共需n次操作。
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 2e5+;
int cur[maxn];
int pre[maxn];
int Find(int x) {return pre[x] == x ? x : Find(pre[x]);}
int main()
{
int n;
scanf("%d",&n);
int thread = ;
int ans = ;
for(int i = ; i<=n; i++) pre[i] = i;
for(int i = ; i<=n; i++)
{
scanf("%d",&cur[i]);
if(cur[i]==i)
{
thread = i;
ans++;
}
else
{
int fx = Find(i);
int fy = Find(cur[i]);
if(fx == fy)
{
cur[i] = i;
ans++;
}
else
{
pre[fx] = fy;
}
}
}
if(thread==) //全是环
{
for(int i = ; i<=n; i++)
{
if(cur[i]==i)
{
thread = i;
break;
}
}
ans++;
}
printf("%d\n",ans-);
for(int i = ; i<=n; i++)
{
if(cur[i]==i) cur[i] = thread;
}
for(int i = ; i<n; i++) printf("%d ",cur[i]);
printf("%d\n",cur[n]);
return ;
}
Problem - D - Codeforces Fix a Tree的更多相关文章
- Codeforces Fix a Tree
Fix a Tree time limit per test2 seconds A tree is an undirected connected graph without cycles. Let' ...
- 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 ...
- Fix a Tree
Fix a Tree A tree is an undirected connected graph without cycles. Let's consider a rooted undirecte ...
- Codeforces 699D Fix a Tree 并查集
原题:http://codeforces.com/contest/699/problem/D 题目中所描述的从属关系,可以看作是一个一个块,可以用并查集来维护这个森林.这些从属关系中会有两种环,第一种 ...
- 【并查集】【模拟】Codeforces 698B & 699D Fix a Tree
题目链接: http://codeforces.com/problemset/problem/698/B http://codeforces.com/problemset/problem/699/D ...
- 【codeforces 698B】 Fix a Tree
题目链接: http://codeforces.com/problemset/problem/698/B 题解: 还是比较简单的.因为每个节点只有一个父亲,可以直接建反图,保证出现的环中只有一条路径. ...
- Codeforces Round #363 (Div. 1) B. Fix a Tree 树的拆环
题目链接:http://codeforces.com/problemset/problem/698/B题意:告诉你n个节点当前的父节点,修改最少的点的父节点使之变成一棵有根树.思路:拆环.题解:htt ...
随机推荐
- NoSQL数据库种类
NoSQL数据库的四大分类 键值(Key-Value)存储数据库 这一类数据库主要会使用到一个哈希表,这个表中有一个特定的键和一个指针指向特定的数据.Key/value模型对于IT系统来说的优 ...
- 谈CSS模块化【封装-继承-多态】
第一次听到“CSS模块化”这个词是在WebReBuild的第四届“重构人生”年会上,当时我还想,“哈,CSS也有模块化,我没听错吧?”事实上,我没听错,你也没看错,早就有CSS模块化这个概念了.之所以 ...
- EF中用Newtonsoft.Json引发的循环引用问题
描述: 1.双向关系表a->b b->aList 2.在查询a引用b以后 3.用Newtonsoft.Json 去tojsonstring 4.一个只有6条数据的json串 出现了一屏幕字 ...
- mongodb分片
在系统早期,数据量还小的时候不会引起太大的问题,但是随着数据量持续增多,后续迟早会出现一台机器硬件瓶颈问题的.而mongodb主打的就是海量数据架构,他不能解决海量数据怎么行!不行!“分片”就用这个来 ...
- Java 中的四种引用及垃圾回收策略
Java 中有四种引用:强引用.软引用.弱引用.虚引用: 其主要区别在于垃圾回收时是否进行回收: 1.强引用 使用最普遍的引用.如果一个对象具有强引用,那就 类似于必不可少的生活用品,垃圾回收器绝不会 ...
- WPF中ListBox控件选择多个数据项
XAML: <Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft ...
- JavaBean技术的一些讲解
JavaBean: 由于原先的jsp的html代码和css代码以及java代码都是写在一起的,所以看起来就比较冗余,同时 也体现不了javaBean中面向对象的思想的{当然可以同过jstl标签库以及m ...
- slf4j(simple logging facade for java)
http://www.tuicool.com/articles/IfeUfq slf4j(simple logging facade for java)是Java的简单的日志门面,它 不是具体的日 ...
- display:table表格合并单元格
直接上代码: <%@ page language="java" contentType="text/html; charset=UTF-8" pageEn ...
- js入门实例
<!DOCTYPE html> <html> <body> <h1>My First Web Page</h1> <p>My F ...