【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 ...
随机推荐
- javascript学习笔记(四) Number 数字类型
数字格式化方法toFixed().toExponential().toPrecision(),三个方法都四舍五入 toFixed() 方法指定小数位个数 toExponential() 方法 用科学 ...
- C4 垃圾回收
使用C4垃圾回收器可以有效提升对低延迟有要求的企业级Java应用程序的伸缩性. 到目前为止,stop-the-world式的垃圾回收视为影响Java应用程序伸缩性的一大障碍,而伸缩性又是现代企业级Ja ...
- myBatis之入门示例
1. myBatis目录结构: --src ---entity [POJO类] ---mappers [映射类] ----*Mapper.java [方法接口,相当于Dao] ----*Mapper. ...
- Spring Boot开发MongoDB应用实践
本文继续上一篇定时任务中提到的邮件服务,简单讲解Spring Boot中如何使用MongoDB进行应用开发. 上文中提到的这个简易邮件系统大致设计思路如下: 1.发送邮件支持同步和异步发送两种 2.邮 ...
- Nginx接收的host值会影响alias的规则匹配
一般内网接收的HTTP请求都是内网唯一的网关传过来的,nginx的alias匹配会直接使用网关穿过的host值,而不是从URL解析出来的,从而导致的问题是,容器的alias相关Server_name规 ...
- 使用Glide以及OkHttp集成
1.glide的使用: 添加依赖: compile 'com.github.bumptech.glide:glide:3.7.0' 调用代码: ImageView imageView = (Image ...
- Bootstrap在线引用css和js
百度在线调用 <script src="http://libs.baidu.com/bootstrap/3.0.3/js/bootstrap.min.js"></ ...
- 为什么在JDBC要使用Class.forName();这句话
为什么在调用JDBC的时候,我们总要写这句话:Class.forName("驱动类");解释:在JDBC编程中一般有以下几个步骤:1>加载驱动,也就是Class.forNam ...
- 常用域名记录解释:A记录、MX记录、CNAME记录、TXT记录、AAAA记录、NS记录
A记录 A记录是用来创建到IP地址的记录. A记录设置技巧 1.如果想创建不带www的记录,即ezloo.com,在主机记录中填写@或者留空,不同的注册商可能不一样. 2.创建多个域名到同一个IP,比 ...
- Sublime中文编码问题
1. 改配置 Preferences->Settings 三个全部加上 "default_encoding": "UTF-8" 2. 代码编写 2.1 ...