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的更多相关文章

  1. Codeforces Fix a Tree

    Fix a Tree time limit per test2 seconds A tree is an undirected connected graph without cycles. Let' ...

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

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

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

  5. Fix a Tree

    Fix a Tree A tree is an undirected connected graph without cycles. Let's consider a rooted undirecte ...

  6. Codeforces 699D Fix a Tree 并查集

    原题:http://codeforces.com/contest/699/problem/D 题目中所描述的从属关系,可以看作是一个一个块,可以用并查集来维护这个森林.这些从属关系中会有两种环,第一种 ...

  7. 【并查集】【模拟】Codeforces 698B & 699D Fix a Tree

    题目链接: http://codeforces.com/problemset/problem/698/B http://codeforces.com/problemset/problem/699/D ...

  8. 【codeforces 698B】 Fix a Tree

    题目链接: http://codeforces.com/problemset/problem/698/B 题解: 还是比较简单的.因为每个节点只有一个父亲,可以直接建反图,保证出现的环中只有一条路径. ...

  9. Codeforces Round #363 (Div. 1) B. Fix a Tree 树的拆环

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

随机推荐

  1. hdu_5742_It's All In The Mind

    题目链接:hdu_5742_It's All In The Mind 题意: 有一个部分的数列,让你找一个满足他给的三个条件的数列,使前两个数的和除这个数列的sum最大 题解: xjb贪心一下就行了. ...

  2. find the closest sum to a target value

    problem: given an array of integers including positive and negative, a target value. find 2 numbers ...

  3. svn rollback: 恢复到上一版本

    18:48:32svn的文件版本是168,我想用167的版本覆盖掉168的版本如何搞? 18:52:47先把本地的那个文件用rm命令删掉,然后,使用svn up -r 167 文件路径,UP下来的文件 ...

  4. jenkins与rebotframework搭配

    一.下载Jenkins 下载地址:http://mirrors.jenkins-ci.org/ 贫道比较推荐下载war包的,进入上面的地址,页面里有war的链接,各种类型各种版本的release,大家 ...

  5. style控制打印分页

    [转载地址:http://www.cnblogs.com/JustinYoung/articles/710734.html]page-break-before和page-break-after CSS ...

  6. SQL的自增列重置的方法

    SQL的自增列挺好用,只是开发过程中一旦删除数据,标识列就不连续了 写起来 也很郁闷,所以查阅了一下标识列重置的方法 发现可以分为三种: --- 删除原表数据,并重置自增列 truncate tabl ...

  7. php 四种基础算法 ---- 选择排序法

    2. 选择排序法: 选择排序法思路: 每次选择一个相应的元素,然后将其放到指定的位置 代码: function select_sort($arr) {//实现思路 双重循环完成,外层控制轮数,当前的最 ...

  8. xp安装maven

    1.下载apache-maven-2.0.8 2.设置xp环境变量 MAVEN_HOME D:\apache-maven-2.0.8 在path里面假如  %MAVEN_HOME%\bin 然后打开c ...

  9. UIWebView & javascript

    http://blog.163.com/m_note/blog/static/208197045201293015844274/ UIWebView是IOS SDK中渲染网面的控件,在显示网页的时候, ...

  10. Jdk1.7环境变量的配置

    在"系统属性——高级——环境变量——系统变量"中新建如下变量: 变量名:JAVA_HOME 变量值:C:\Program Files\Java\jdk1.7.0_03  (即jdk ...