题目链接:

  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的更多相关文章

  1. 【27.48%】【codeforces 699D】 Fix a Tree

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  2. 【CodeForces 699D】Fix a Tree

    dfs找出联通块个数cnt,当形成环时,令指向已访问过节点的节点变成指向-1,即做一个标记.把它作为该联通图的根. 把所有联通的图变成一颗树,如果存在指向自己的点,那么它所在的联通块就是一个树(n-1 ...

  3. 【27.91%】【codeforces 734E】Anton and Tree

    time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  4. 【codeforces 791D】 Bear and Tree Jumps

    [题目链接]:http://codeforces.com/contest/791/problem/D [题意] 你可以从树上的节点一次最多走k条边. (称为跳一次); 树为无权树; 然后问你任意两点之 ...

  5. 【并查集】【模拟】Codeforces 698B & 699D Fix a Tree

    题目链接: http://codeforces.com/problemset/problem/698/B http://codeforces.com/problemset/problem/699/D ...

  6. 【19.27%】【codeforces 618D】Hamiltonian Spanning Tree

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  7. 【Codeforces 1086B】Minimum Diameter Tree

    [链接] 我是链接,点我呀:) [题意] 题意 [题解] 统计叶子节点个数m 把每条和叶子节点相邻的边权设置成s/cnt就可以了 这样答案就是2*s/m(直径最后肯定是从一个叶子节点开始,到另外一个叶 ...

  8. 【Codeforces 161D】Distance in Tree

    [链接] 我是链接,点我呀:) [题意] 问你一棵树上有多少条长度为k的路径 [题解] 树形dp 设 size[i]表示以节点i为根节点的子树的节点个数 dp[i][k]表示以i为根节点的子树里面距离 ...

  9. 【CodeForces 614A】Link/Cut Tree

    题 题意 给你一个区间,求里面有多少个数是k的次方. 分析 暴力,但是要注意这题范围会爆long long,当k=1e8: l=1:r=1e18时 k²=1e16,判断了是≤r,然后输出,再乘k就是1 ...

随机推荐

  1. 2018 ACM-ICPC World Finals B.Comma Sprinkler

    WF里面最简答一题,就是一个dfs就可以了,已经访问过的点可以不再访问 #include <algorithm> #include <cmath> #include <c ...

  2. 如何通过jQuery获取一个没有定高度的元素---------的自适应高度(offsetHeight的正确使用方法)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. miniui几个常用知识点汇总

    1.在表格中去除系统自带的序列号,请看代码: function allAndBrief(id) { if(id==1){ grid.set({ columns: [ { type: "ind ...

  4. Integer 和int 比较

    在jdk1.5的环境下,有如下4条语句: 1 2 3 4 Integer i01 = 59; int i02 = 59; Integer i03 =Integer.valueOf(59); Integ ...

  5. 对比Cassandra、 Mongodb、CouchDB、Redis、Riak、 Membase、Neo4j、HBase

    转自:http://www.cnblogs.com/alephsoul-alephsoul/archive/2013/04/26/3044630.html 导读:Kristóf Kovács 是一位软 ...

  6. PyCharm中HTML页面CSS class名称自动完成功能失效的问题

    如果这个HTML页面带有style元素的CSS定义,那class name自动完成功能就失效了 Pycharm Version:5.03

  7. 【转】UNREFERENCED_PARAMETER的作用

    UNREFERENCED_PARAMETER 的作用我们从 UNREFERENCED_PARAMETER 开始吧.这个宏在 winnt.h 中定义如下: #define UNREFERENCED_PA ...

  8. 并查集模板题(The Suspects )HZNU寒假集训

    The Suspects Time Limit: 1000MS Memory Limit: 20000KTotal Submissions: 36817 Accepted: 17860 Descrip ...

  9. ScalaPB(2): 在scala中用gRPC实现微服务

    gRPC是google开源提供的一个RPC软件框架,它的特点是极大简化了传统RPC的开发流程和代码量,使用户可以免除许多陷阱并聚焦于实际应用逻辑中.作为一种google的最新RPC解决方案,gRPC具 ...

  10. (二)SpringBoot基础篇- 静态资源的访问及Thymeleaf模板引擎的使用

    一.描述 在应用系统开发的过程中,不可避免的需要使用静态资源(浏览器看的懂,他可以有变量,例:HTML页面,css样式文件,文本,属性文件,图片等): 并且SpringBoot内置了Thymeleaf ...