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. CentOS + EPEL YUM源地址

    [bizosv] name=bizsov-centos-$releasever - centos baseurl=http://yikat:yikat@download.bizsov.com/ gpg ...

  2. MySQL数据库分区修改【原创】

    之前有个表分区添加时s201607添加成s201617,所以在查询7月份数据时报错 错误的 alter table statistics_ticket add partition (partition ...

  3. DataGirdView 编辑项时的验证

    dgvConfig.DataSource = CreateTable(); dgvConfig.Columns["编号"].ReadOnly = true; //只读 dgvCon ...

  4. shell中 if else以及大于、小于、等于逻辑表达式介绍

    比如比较字符串.判断文件是否存在及是否可读等,通常用"[]"来表示条件测试. 注意:这里的空格很重要.要确保方括号的空格.笔者就曾因为空格缺少或位置不对,而浪费好多宝贵的时间. i ...

  5. Django Template模板

    Django Template 你可能已经注意到我们在例子视图中返回文本的方式有点特别. 也就是说,HTML被直接硬编码在 Python 代码之中. 下面我们来调用html views def ind ...

  6. linux命令:find详解

    Linux中find命令常见用法示例: find path -option [-print] [ -exec -ok command ] {} \; #-print 将查找到的文件输出到标准输出#-e ...

  7. Tree Restoring

    Tree Restoring Time limit : 2sec / Memory limit : 256MB Score : 700 points Problem Statement Aoki lo ...

  8. linux ubuntu平台下安装Scrapy

    1.安装Python sudo apt-get install python2.7 python2.7-dev 2.安装pip 下载get-pip.py 选中该文件所在路径,执行下面的命令 sudo ...

  9. php下载文件的一种方式

    <?php ob_start(); // $file_name="cookie.jpg"; $file_name="abc.jpg"; //用以解决中文不 ...

  10. math ceil以及math floor,math:round

    ◎Math.ceil()执行向上舍入,即它总是将数值向上舍入为最接近的整数:◎Math.floor()执行向下舍入,即它总是将数值向下舍入为最接近的整数:◎Math.round()执行标准舍入,即它总 ...