并查集,构造。

先看一下图的特殊性,按照这种输入方式,一个点的入度最多只有$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. Curator Recipes(Cache&Counter)

    Cache 路径缓存(Path Cache) 监视一个ZNode,当子节点增加.更新.删除改变状态时,路径缓存会在本地保存当前子节点及其数据和状态. public PathChildrenCache( ...

  2. 微信企业号 JS-SDK:上传图片

    微信的JS-SDK提供了微信客户端相关的功能,如:拍照.选图.语音.位置等手机系统的能力,同时可以直接使用微信分享.扫一扫等微信特有的能力,为微信用户提供更优质的网页体验.这里将会介绍如何通过调用JS ...

  3. visual studio 2010 无法连接到ASP.NET Development Server

    在点击运行项目时,生成成功.但是页面没有弹出来,弹出个提示框,无法连接到 ASP.NET Development Server. 网上我看到说关闭掉防火墙,可是再关掉防火墙后还是不行.但是其他的项目又 ...

  4. Windows 8 卡在正在检查更新

    原文地址:https://answers.microsoft.com/en-us/windows/forum/windows_8-update/windows-update-not-updating- ...

  5. 常用的meta标签总结

    <!-- 关键字,搜所引擎 SEO --><meta http-equiv="keywords" content="关键字1,关键字2,..." ...

  6. 想入门webpack,这篇就够了

    申明:本文转载自简书 文/zhangwang(简书作者)原文链接:http://www.jianshu.com/p/42e11515c10f#著作权归作者所有,转载请联系作者获得授权,并标注" ...

  7. JavaEE XML XSL转换(XSLT)

    XSL转换(XSLT) @author ixenos 定义: XSL转换机制可以指定将XML文档转换为其他格式的规则,例如,txt纯文本.XHTML或其他任何XML格式. 用途: XSLT通常用来将某 ...

  8. 走迷宫 (nyoj 306)

    学习自此博客题解 二分搜索+深搜.二分枚举最小差距值(路径上的最大值与最小值的差距),枚举的最小值为abs(a[1][1]-a[n][n]),最大值为题目给出的120.搜索时代入这个最小差距值,若存在 ...

  9. php dday1... web服务器的搭建 数据库的安装....

  10. 复习java7 集合的底层实现理解

    1.ArrayList 特点:查询快,增删慢,可以重复,无序 实现:底层是用数组实现的,在添加数据的时候,当数组已经满了,会扩容,默认增加1.5倍的原数组长度,并将旧的数组数据拷贝到新的数组上. 2. ...