先做拓扑排序,再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. Python之函数之路

    1 集合 集合是一个无序的,不重复的数据组合,它的主要作用如下: 去重,把一个列表变成集合,就自动去重了 关系测试,测试两组数据之前的交集.差集.并集等关系 创建集合 a = {3, 5, 9, 9, ...

  2. django之form表单验证

    django中的Form一般有两种功能: 输入html 验证用户输入 #!/usr/bin/env python # -*- coding:utf- -*- import re from django ...

  3. openerp child_of操作符深度解析

    child_of  此操作符,从代码来看,等价于: [('x','child_of',id)] ==>  x.prarent_left >=id.parent_left && ...

  4. ndk学习20: jni之OnLoad动态注册函数

    一.原理 当在系统中调用System.loadLibrary函数时,该函数会找到对应的动态库, 然后首先试图找到"JNI_OnLoad"函数,如果该函数存在,则调用它 JNI_On ...

  5. MongoDB-Getting Started with the C# Driver

    简介:本文仅提供快速入门级别的使用C# Driver操作MongoDB,高手跳过 Downloading the C# Driver 猛击下载 添加相关的dll引用 MongoDB.Bson.dll ...

  6. 给一个正在运行的Docker容器动态添加Volume

    给一个正在运行的Docker容器动态添加Volume本文转自:http://dockone.io/article/149 [编者的话]之前有人问我Docker容器启动之后还能否再挂载卷,考虑到mnt命 ...

  7. [Linux]非外网环境下配置lnmp心得

    1.安装gcc编译器 基本所有编译安装包都需要gcc编译器,而且编译安装gcc也需要一个编译器,这是一个悖论.所以,这里只能寻求rpm包安装.但是rpm包需要匹配特定的操作系统内核,所以没有一个通用的 ...

  8. 3.kvm的基本管理

    1.查看KVM虚拟机配置文件 #KVM虚拟机默认配置文件位置 [root@kvm qemu]# pwd /etc/libvirt/qemu [root@kvm qemu]# ll total 12 # ...

  9. codeforces 515C. Drazil and Factorial 解题报告

    题目链接:http://codeforces.com/problemset/problem/515/C 题目意思:给出含有 n 个只有阿拉伯数字的字符串a(可能会有前导0),设定函数F(a) = 每个 ...

  10. asp.net 短信群发

    protected void Btn_Save_Click(object sender, EventArgs e) { string Contents = this.Txt_SmsContents.T ...