先做拓扑排序,再bfs处理

#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<stack>
#include<queue>
#include<vector>
#include<cmath>
#include<utility>
using namespace std;
typedef long long LL;
const int N = , INF = 0x3F3F3F3F;
#define MS(a, num) memset(a, num, sizeof(a))
#define PB(A) push_back(A)
#define FOR(i, n) for(int i = 0; i < n; i++)
bool vis[N];
struct Node{
    int to,next;
}edge[N];
int head[N], tot, n,m,indeg[N];
int fa[N];
void init(){
    memset(head, -, sizeof(head));
    memset(indeg, , sizeof(indeg));
    tot = ;
}
void add(int u, int to){
    indeg[to]++;
    edge[tot].to=to;
    edge[tot].next=head[u];
    head[u]=tot++;
}
void Topsort(){
stack <int> st;
    for(int i = ;i<=n; i++) {
        if(indeg[i]==) {
            st.push(i);
        }
    }
    while(!st.empty()){
        int cur = st.top();
        st.pop();
        vis[cur] = ;
        for(int i = head[cur]; i != -; i = edge[i].next){
            int to= edge[i].to;
            indeg[to]--;
            if(!indeg[to]){
                st.push(to);
            }
        }
    }
} void bfs(int u){
    queue<int> q;
    q.push(u);
    vis[u]  =;
    while(!q.empty()){
            int u = q.front();
            q.pop();
           for(int i= head[u]; i != -; i = edge[i].next){
                int to = edge[i].to;
                if(!vis[to]){
                    vis[to] = ;
                    q.push(to);
                }
            }
    }
}
int main(){
    cin>>n;
    MS(vis, );
    init();
    for(int i = ;i <= n ;i++){
        int u;
        scanf("%d", &u);
        fa[i] = u;
        add(i, u);
    }
    Topsort();
    int v = ;
    for(int i = ;i <= n; i++){
        if(fa[i] == i){
            vis[i] = ;
            v = i;
            break;
        }
    }
    if(v == ){
        for(int i = ;i <= n; i++){
            if(!vis[i]){
                fa[i] = i;
                v = i;
                break;
            }
        }
    }     int cnt = ;
    for(int i =; i <= n; i++){
        if(!vis[i]){
            fa[i] = v;
            cnt++;
            bfs(i);
        }
    }
    cout<<cnt<<'\n';
    cout<<fa[];
    for(int i  = ;i <= n;i++){
        printf(" %d", fa[i]);
    }
    return ;

}

Codeforces Round #363 Fix a Tree(树 拓扑排序)的更多相关文章

  1. Codeforces Round 363 Div. 1 (A,B,C,D,E,F)

    Codeforces Round 363 Div. 1 题目链接:## 点击打开链接 A. Vacations (1s, 256MB) 题目大意:给定连续 \(n\) 天,每天为如下四种状态之一: 不 ...

  2. BZOJ_3012_[Usaco2012 Dec]First!_trie树+拓扑排序

    BZOJ_3012_[Usaco2012 Dec]First!_trie树+拓扑排序 题意: 给定n个总长不超过m的互不相同的字符串,现在你可以任意指定字符之间的大小关系.问有多少个串可能成为字典序最 ...

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

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

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

  6. Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) E. Tree Folding 拓扑排序

    E. Tree Folding 题目连接: http://codeforces.com/contest/765/problem/E Description Vanya wants to minimiz ...

  7. Codeforces Round #363

    http://codeforces.com/contest/699 ALaunch of Collider 题意:n个球,每个球向左或右,速度都为1米每秒,问第一次碰撞的时间,否则输出-1 贪心最短时 ...

  8. Codeforces Round #363 Div.2[111110]

    好久没做手生了,不然前四道都是能A的,当然,正常发挥也是菜. A:Launch of Collider 题意:20万个点排在一条直线上,其坐标均为偶数.从某一时刻开始向左或向右运动,速度为每秒1个单位 ...

  9. Codeforces gym101755F Tree Restoration(拓扑排序)

    题意: 一棵树,给出每个点的后代们,问你这棵树是否存在,存在就给出这棵树 n<=1000 思路: 对祖先->后代建立有向图,跑拓扑排序.跑的时候不断更新父亲并判断答案的存在性,同时注意一种 ...

随机推荐

  1. 1. dex和Jar反编译对比

    Java源码 public class Hello { public int foo(int a,int b) { return (a + b) * (a - b); } public static ...

  2. 使用 PHP 7 给 Web 应用加速

    PHP 20周年了!?? PHP 首发通告,1995年6月8日 发布于 COMP.INFOSYSTEMS.WWW.AUTHORING.CGI 主题:正式宣布:个人主页工具(Personal Home ...

  3. linux 终端报错 Out of memory: Kill process[PID] [process name] score问题分析

    从Out of memory来看是内存超出了,后面的 Kill process[PID] [process name] score好像和进程有关了,下面我们就一起来看看linux 终端报错 Out o ...

  4. KVC 与 KVO 理解

    KVC 与 KVO 是 Objective C 的关键概念,个人认为必须理解的东西,下面是实例讲解. Key-Value Coding (KVC) KVC,即是指 NSKeyValueCoding,一 ...

  5. centos7时间同步和时区设置

    centos7时间同步和时区设置 安装ntp服务的软件包 sudo yum install ntp 将ntp服务设置为缺省启动 systemctl enable ntpd 修改启动参数,增加-g -x ...

  6. ios NSURLSession completeHandler默认调用quque

    注意 , [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSU ...

  7. java web 学习 --第八天(Java三级考试)

    第七天的学习内容:http://www.cnblogs.com/tobecrazy/p/3464231.html EL表达式 EL : Expression Language 使用EL表达式可以减少& ...

  8. ACM/ICPC 之 模拟 (HNUOJ 13391-换瓶模拟)

    题意:汽水瓶有三个部分cap+plastic bottle+ label(瓶盖-瓶身-瓶底),给出数据:n为原瓶数,x,y,z为这三个部分可以用相应的数字换取新瓶子,求最大总瓶数. 模拟(暴力) // ...

  9. 10. javacript高级程序设计-DOM

    1. DOM DOM(文档对象模型)是针对HTML和XML文档的一个API(应用程序接口) 1.1 节点层次 DOM可以将任何HTML和XML文档描绘成一个由多层节点构成的结构.节点分为几种不同的类型 ...

  10. .net Framework Class Library(FCL)

    from:http://msdn.microsoft.com/en-us/library/ms229335.aspx 我们平时在VS.net里引用的那些类库就是从这里来的 The .NET Frame ...