并查集,构造。

先看一下图的特殊性,按照这种输入方式,一个点的入度最多只有$1$,因此,问题不会特别复杂,画画图就能知道了。

如果给出的序列中已经存在$a[i]=i$,那么随便取一个$a[i]=i$的$i$作为$root$,剩下的每一条边$a[i] \to i$,可以用并查集来处理,如果发现某条边$a[i] \to i$加入前$a[i]$与$i$已经在同一集合中,说明再加$a[i] \to i$会导致成环,因此将$i$的$father$改成$root$即可,并将$i$与$root$合并。

如果给出的序列中不存在$a[i]=i$,和上面的处理方法类似,唯一不同的是:找到第一条不能加入的边$a[i] \to i$,将$i$的$father$改为$i$,并且$root$设置为$i$,之后出现不能加入的边和上面处理方法一样。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-;
void File()
{
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
}
template <class T>
inline void read(T &x)
{
char c = getchar(); x = ;while(!isdigit(c)) c = getchar();
while(isdigit(c)) { x = x * + c - ''; c = getchar(); }
} const int maxn=;
int f[maxn],a[maxn],n; int Find(int x)
{
if(x!=f[x]) f[x]=Find(f[x]);
return f[x];
} int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%d",&a[i]);
int root=-; for(int i=;i<=n;i++) if(a[i]==i) root=i;
int num=; for(int i=;i<=n;i++) f[i]=i; for(int i=; i<=n; i++)
{
if(i==root) continue;
int fx=Find(i),fy=Find(a[i]);
if(fx!=fy) f[fx]=fy;
else
{
if(root==-) a[i]=i, root=i, num++;
else
{
a[i]=root, num++;
fx=Find(root); fy=Find(i);
f[fx]=fy;
}
}
}
printf("%d\n",num);
for(int i=; i<=n; i++) printf("%d ",a[i]);
printf("\n");
return ;
}

CodeForces 698B Fix a Tree的更多相关文章

  1. CodeForces 698B Fix a Tree (并查集应用)

    当时也是想到了并查集,但是有几个地方没有想清楚,所以就不知道怎么写了,比如说如何确定最优的问题.赛后看了一下别人的思路,才知道自己确实经验不足,思维也没跟上. 其实没有那么复杂,这个题目我们的操作只有 ...

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

  3. Codeforces 699D Fix a Tree 并查集

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

  4. Problem - D - Codeforces Fix a Tree

    Problem - D - Codeforces  Fix a Tree 看完第一名的代码,顿然醒悟... 我可以把所有单独的点全部当成线,那么只有线和环. 如果全是线的话,直接线的条数-1,便是操作 ...

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

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

  7. Codeforces Fix a Tree

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

  8. Codeforces 461B Appleman and Tree(木dp)

    题目链接:Codeforces 461B Appleman and Tree 题目大意:一棵树,以0节点为根节点,给定每一个节点的父亲节点,以及每一个点的颜色(0表示白色,1表示黑色),切断这棵树的k ...

  9. Fix a Tree

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

随机推荐

  1. js动画(二)

    嗯,今天好冷,特别冷,我的手指,都冻的打不了字了.今天一件特别的傻的事就是,在 for(var i;i<obj.length;i++){} 找了半天没有注意到 var i 没有赋值.够150 了 ...

  2. labview 调用 matlab script的神坑! Error 1050 occurred at LabVIEW

    显示变量没有被定义,原因是clear 关键字的问题,去掉即可!!! 未找到 文件路径,定位: 文件路径中不能有中文路径

  3. 【Java每日一题】20170112

    20170111问题解析请点击今日问题下方的"[Java每日一题]20170112"查看(问题解析在公众号首发,公众号ID:weknow619) package Jan2017; ...

  4. 写一个MyList

    首先定义接口 using System; using System.Collections.Generic; using System.Linq; using System.Text; using S ...

  5. minSdkVersion与targetSdkVersion

    targetSdkVersion是Android提供向前兼容的主要依据,在应用的targetSdkVersion没有更新之前,系统不会应用最新的行为变化 比如设置了app的targetSdkVersi ...

  6. Ubuntu中Qt5.7.0的安装及opencv2.4.13配置

    去官网下载qt-opensource-linux-x64-5.7.0.run,到"下载"目录 Ctrl+Alt+T打开终端 cd /home/jv/下载sudo mv qt-ope ...

  7. vim实用技巧总结

    1. 单个文件内实用mark来帮助跳转以及一些宏操作.(注意:mark不能用于多文件之间的跳转)mch 设置宏,保存到ch标记(不是ch寄存器)'ch 跳转到标记的行首`ch 跳转到标记的字符处另:为 ...

  8. Cracking the Coding Interview 第二章

    2.2 链表中倒数第k个结点 输入一个链表,输出该链表中倒数第k个结点. 思路:快慢指针(error: 判断是否有可行解,否则返回null, while, if 后加空格) /* public cla ...

  9. openSuse快捷键

    1.printscreen全屏截图 2.ctrl+printscreen窗口截图 3.shift+printscreen选择截图 4.Ctrl+Alt+up arrow.Ctrl+Alt+down a ...

  10. Anton and Tree

    Anton and Tree 题目链接:http://codeforces.com/contest/734/problem/E DFS/BFS 每一次操作都可以使连通的结点变色,所以可以将连通的点缩成 ...