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. cmstop传递什么控制器和方法---就实例化该控制器

    object顶级类class object 第一级抽象类controllerabstract class controller extends object 第二级抽象类controller_abst ...

  2. openwrt默认不开启wifi

    Openwrt默认不开启wifi,要开启的话, 修改这个文件: openwrt/trunk/package/kernel/mac80211/files/lib/wifi/mac80211.sh. 滚到 ...

  3. LeetCode OJ 292.Nim Gam19. Remove Nth Node From End of List

    Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...

  4. CentOS 7 BIND 搭建

    域名查找顺序 设置 /etc/host.conf 1. bind 安装 $ yum install bind bind-utilsnslookup (name server lookup) 在bind ...

  5. 哈佛大学构建动态网站--第七讲ajax

    Ajax ajax举例: DOM的结构 通过js来修改html页面. Ajax的含义: return false的用途 跨浏览器的ajax 为什么不直接从yahoo获得数据呢? XMLHttpRequ ...

  6. c printf()详解[转载]

    ref : http://www.cnblogs.com/yuaqua/archive/2011/10/21/2219856.html #include <cstdio> #include ...

  7. mysql 不能插入中文

    三.#vim /etc/mysql/my.cnf .(5.5以前系统)在[client]下面加入 default-character-set=utf8 在[mysqld]下面加入default-cha ...

  8. Food on the Plane

    Food on the Plane time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  9. jquer 带左右按钮滚动图片 点击显示大图

    <style> ul{ list-style:none; padding:0px; margin:0px;} li{ list-style:none; padding:0px; margi ...

  10. android cts 命令的说明

    Host help showthis message 帮助文档 exit exitcts command line 退出CTS ls 全部用l替代,--plan直接用p替代,也即 l p .其他类似 ...