先做拓扑排序,再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. HTK搭建语音拨号系统实验材料下载

    选自:http://maotong.blog.hexun.com/6267266_d.html 压缩包包括全部的配置文件,脚本文件,必备的模型文件和实验手册. 全部实验材料的下载链接: 1 http: ...

  2. silverlight 获取服务器上图片出现异常 “AG_E_NETWORK_ERROR”

    前言 之前项目一直是发布在IIS上面使用HTTP访问,现在要求改为HTTPS,通过在IIS生成自签名后,打开HTTPS通道,可以将原来的程序已HTTPS的方式发布出来. 可参见 http://blog ...

  3. Graph Valid Tree

    Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...

  4. ERROR Cannot determine the location of the VS Common Tools Folder

    参考:ERROR Cannot determine the location of the VS Common Tools Folder   http://blog.csdn.net/m3728975 ...

  5. 引用外部css文件

    <link type="text/css" rel="stylesheet" href="http://files.cnblogs.com/91 ...

  6. VIM基础

    http://www.cnblogs.com/wawlian/archive/2012/05/22/2512801.html http://www.cnblogs.com/wawlian/archiv ...

  7. 数据结构——二叉查找树、AVL树

    二叉查找树:由于二叉查找树建树的过程即为插入的过程,所以其中序遍历一定为升序排列! 插入:直接插入,插入后一定为根节点 查找:直接查找 删除:叶子节点直接删除,有一个孩子的节点删除后将孩子节点接入到父 ...

  8. metro压缩和解压文件

    在1.zip中增加一张新图片StorageFile jpg = await KnownFolders.PicturesLibrary.GetFileAsync("1.jpg"); ...

  9. Mathematics:Find a multiple(POJ 2356)

    找组合 题目大意:给你N个自然数,请你求出若干个数的组合的和为N的整数倍的数 经典鸽巢原理题目,鸽巢原理的意思是,有N个物品,放在N-1个集合中,则一定存在一个集合有2个元素或以上. 这一题是说有找出 ...

  10. FileUpload控件使用初步

    FileUpload控件使用初步   FileUpload控件使用初步: 1.实现文件上传 protected void btnSubmit_click(object sender, EventArg ...