zoukankan      html  css  js  c++  java
  • 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 limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    A tree is an undirected connected graph without cycles.

    Let's consider a rooted undirected tree with n vertices, numbered 1 through n. There are many ways to represent such a tree. One way is to create an array with n integers p1, p2, ..., pn, where pi denotes a parent of vertex i (here, for convenience a root is considered its own parent).

    For this rooted tree the array p is [2, 3, 3, 2].

    Given a sequence p1, p2, ..., pn, one is able to restore a tree:

    1. There must be exactly one index r that pr = r. A vertex r is a root of the tree.
    2. For all other n - 1 vertices i, there is an edge between vertex i and vertex pi.

    A sequence p1, p2, ..., pn is called valid if the described procedure generates some (any) rooted tree. For example, for n = 3 sequences (1,2,2)(2,3,1) and (2,1,3) are not valid.

    You are given a sequence a1, a2, ..., an, not necessarily valid. Your task is to change the minimum number of elements, in order to get a valid sequence. Print the minimum number of changes and an example of a valid sequence after that number of changes. If there are many valid sequences achievable in the minimum number of changes, print any of them.

    Input

    The first line of the input contains an integer n (2 ≤ n ≤ 200 000) — the number of vertices in the tree.

    The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ n).

    Output

    In the first line print the minimum number of elements to change, in order to get a valid sequence.

    In the second line, print any valid sequence possible to get from (a1, a2, ..., an) in the minimum number of changes. If there are many such sequences, any of them will be accepted.

    Examples
    input
    4
    2 3 3 4
    
    output
    1
    2 3 4 4 
    
    input
    5
    3 2 2 5 3
    
    output
    0
    3 2 2 5 3 
    
    input
    8
    2 3 5 4 1 6 6 7
    
    output
    2
    2 3 7 8 1 6 6 7


    题意:

    有n个节点,给出每个点的父节点。问至少修改多少个节点的父节点,使得这些节点刚好构成一棵树?


    题解:

    1.经过分析,假设在初始状态有k个集合,那么对于每一个集合,它要么是一棵根节点的父节点为自己的树,要么是一棵根节点的父节点为其子孙的树(虽然不是树,但姑且这么叫,便于理解)。

    2.首先用并查集使得每个集合“现出原形”,得到集合后,需要选一个根节点,作为最终树的根节点,父节点为自己的根节点优先被选择,然后再把其他根节点的 父节点改为最终树的根节点。如果选另一种,就需要把它的父节点改为自己,真正作为根节点(这个集合就真真正正是一棵树了)后,再把其他根节点的父节点改为最终树的根节点,这样就多了一次修改。


    代码如下:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cstdlib>
     5 #include <string>
     6 #include <vector>
     7 #include <map>
     8 #include <set>
     9 #include <queue>
    10 #include <stack>
    11 #include <sstream>
    12 #include <algorithm>
    13 using namespace std;
    14 #define ms(a, b)  memset((a), (b), sizeof(a))
    15 typedef long long LL;
    16 const double eps = 1e-6;
    17 const int INF = 2e9;
    18 const LL LNF = 9e18;
    19 const int mod = 1e9+7;
    20 const int maxn = 200000+10;
    21 
    22 int fa[maxn], a[maxn], b[maxn];
    23 int n;
    24 
    25 int find(int x) { return fa[x]==x? fa[x]=x:find(fa[x]); }
    26 
    27 void init()
    28 {
    29     scanf("%d",&n);
    30     for(int i = 1; i<=n; i++)
    31         scanf("%d",&a[i]), b[i] = a[i], fa[i] = i;
    32 }
    33 
    34 void solve()
    35 {
    36     for(int i = 1; i<=n; i++)
    37     {
    38         int u = a[i], v = i;
    39         int m1 = find(u);
    40         int m2 = find(v);
    41 
    42         if(m1!=m2)
    43             fa[m2] = m1;
    44     }
    45 
    46     int rt = 0;
    47     for(int i = 1; i<=n; i++)
    48     if(fa[i]==i && a[i]==i)
    49     {
    50         rt = i;
    51         break;
    52     }
    53 
    54     for(int i = 1; i<=n; i++)
    55     {
    56         if(fa[i]==i)
    57         {
    58             if(!rt)
    59                 rt = i, a[i] = i;
    60             else
    61                 a[i] = rt;
    62         }
    63     }
    64 
    65     int ans = 0;
    66     for(int i = 1; i<=n; i++)
    67         if(a[i]!=b[i])
    68             ans++;
    69 
    70     printf("%d
    ",ans);
    71     for(int i = 1; i<=n; i++)
    72         printf("%d ",a[i]);
    73 }
    74 
    75 int main()
    76 {
    77 //    int T;
    78 //    scanf("%d",&T);
    79 //    while(T--)
    80     {
    81         init();
    82         solve();
    83     }
    84     return 0;
    85 }
    View Code



  • 相关阅读:
    低级格式化
    winhex恢复误GHOST系统造成的数据丢失
    共享进程空间内容(修的进程空间代码/HOOK)
    WinDbg 入门教程
    遍历删除当前目录下所有文件
    VC/MFC非模态对话框实例
    经典SQL语句
    C++实现系统服务暂停、停止、启动
    汇编中的管道操作方法
    Dll2lib探究
  • 原文地址:https://www.cnblogs.com/DOLFAMINGO/p/7538696.html
Copyright © 2011-2022 走看看