【codeforces 698B】 Fix a Tree
题目链接:
http://codeforces.com/problemset/problem/698/B
题解:
还是比较简单的。因为每个节点只有一个父亲,可以直接建反图,保证出现的环中只有一条路径。
然后发现,有多少个环,就需要改多少条边。然后设出现连向自己的节点为x,这些也要改边,对答案的贡献应为:$max(x-1,0)$。对于最后的根节点,有自环选一个,没自环在其他环上任选一个点就行。
#include<cstdio>
inline int min(int a,int b){return a<b?a:b;}
inline int read(){
int s=;char ch=getchar();
while(ch<''||ch>'') ch=getchar();
while(ch>=''&&ch<='') s=s*+(ch^),ch=getchar();
return s;
}
const int N=;
int n;
int p[N];
int dfn[N],low[N],dfx,stk[N],top,scc;
bool instk[N];
int size[N];
int first[N];
inline void tarjin(int x){
dfn[x]=low[x]=++dfx;
stk[++top]=x;instk[x]=true;
if(!dfn[p[x]]){
tarjin(p[x]);
low[x]=min(low[x],low[p[x]]);
}else if(instk[p[x]]) low[x]=min(low[x],dfn[p[x]]);
if(low[x]==dfn[x]){
int t;scc++;
do{
t=stk[top--];
size[scc]++;
if(!first[scc])
first[scc]=t;
instk[t]=false;
}while(t!=x);
}
}
int main(){
n=read();
for(int i=;i<=n;i++){
p[i]=read();
}
int ans=;
int to=;
for(int i=;i<=n;i++)
if(p[i]==i){ ans--;break;}
for(int i=;i<=n;i++){
if(!dfn[i])
tarjin(i);
if(p[i]==i&&!to)
to=i;
}
if(!to){
to=p[first[]]=first[];
}
for(int i=;i<=scc;i++){
if(size[i]>||(first[i]==p[first[i]]&&size[i]==)){
p[first[i]]=to;
ans++;
}
}
printf("%d\n",ans);
for(int i=;i<=n;i++)
printf("%d%c",p[i],i<n?' ':'\n');
}
【codeforces 698B】 Fix a Tree的更多相关文章
- 【27.48%】【codeforces 699D】 Fix a Tree
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【CodeForces 699D】Fix a Tree
dfs找出联通块个数cnt,当形成环时,令指向已访问过节点的节点变成指向-1,即做一个标记.把它作为该联通图的根. 把所有联通的图变成一颗树,如果存在指向自己的点,那么它所在的联通块就是一个树(n-1 ...
- 【27.91%】【codeforces 734E】Anton and Tree
time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 791D】 Bear and Tree Jumps
[题目链接]:http://codeforces.com/contest/791/problem/D [题意] 你可以从树上的节点一次最多走k条边. (称为跳一次); 树为无权树; 然后问你任意两点之 ...
- 【并查集】【模拟】Codeforces 698B & 699D Fix a Tree
题目链接: http://codeforces.com/problemset/problem/698/B http://codeforces.com/problemset/problem/699/D ...
- 【19.27%】【codeforces 618D】Hamiltonian Spanning Tree
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【Codeforces 1086B】Minimum Diameter Tree
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 统计叶子节点个数m 把每条和叶子节点相邻的边权设置成s/cnt就可以了 这样答案就是2*s/m(直径最后肯定是从一个叶子节点开始,到另外一个叶 ...
- 【Codeforces 161D】Distance in Tree
[链接] 我是链接,点我呀:) [题意] 问你一棵树上有多少条长度为k的路径 [题解] 树形dp 设 size[i]表示以节点i为根节点的子树的节点个数 dp[i][k]表示以i为根节点的子树里面距离 ...
- 【CodeForces 614A】Link/Cut Tree
题 题意 给你一个区间,求里面有多少个数是k的次方. 分析 暴力,但是要注意这题范围会爆long long,当k=1e8: l=1:r=1e18时 k²=1e16,判断了是≤r,然后输出,再乘k就是1 ...
随机推荐
- svn 不能添加.a文件
1.打开终端输入 open ~/.subversion/ 2.双击打开config文件 3.修改如下两行 # global-ignores = *.o *.lo *.la *.al .libs ...
- Python并发编程-RabbitMQ消息队列
RabbitMQ队列 RabbitMQ是一个在AMQP基础上完整的,可复用的企业消息系统.他遵循Mozilla Public License开源协议. MQ全称为Message Queue, 消息队列 ...
- Pyharm中关于“warning: Debugger speedups using cython not found”问题的解决
在终端中输入 Python "/Applications/PyCharm CE.app/Contents/helpers/pydev/setup_cython.py" build_ ...
- IT桔子沙龙之本地生活服务O2O探路者笔记整理
这个沙龙活动侧重于创业融资与投资角度 主持人:真格基金投资分析师顾女士;E-mail:grace@zhenfund.com;新浪微博:@顾三小姐 赞助商七牛云的服务宣传:静态资源托管:上传下载全网加速 ...
- Ubuntu硬盘空间清理
1.删除多余的安装下载文件: sudo aptitude autoclean sudo aptitude clean 2.删除多余的内核版本: sudo apt-get autoremove 4.去/ ...
- 分享一个开源的网盘下载工具BaiduPCS-Go
大家在使用网盘的时候,一定忍受不了限速下载的速度.今天给大家分享一个开源的网盘下载项目BaiduPCS-Go.Go语言编写,仿 Linux shell 文件处理命令的百度网盘命令行客户端.多平台支持, ...
- Bootstrap在线引用css和js
百度在线调用 <script src="http://libs.baidu.com/bootstrap/3.0.3/js/bootstrap.min.js"></ ...
- python都能做什么
一.python: Python具有丰富和强大的库.它常被昵称为胶水语言,能够把用其他语言制作的各种模块(尤其是C/C++)很轻松地联结在一起.常见的一种应用情形是,使用Python快速生成程序的原型 ...
- 重温《STL源码剖析》笔记 第五章
源码之前,了无秘密 ——侯杰 序列式容器 关联式容器 array(build in) RB-tree vector set heap map priority-queue multiset li ...
- VS2010+OpenCV3.4.1+zbar 64位
1. OpenCV3.4.1和zbar文件夹放到指定的路径下,我把它们放在了"D:\二维码\环境"中. zbar:链接:https://pan.baidu.com/s/11eCDV ...