Codeforces Round #363 (Div. 2) D. Fix a Tree

Source: Internet
Author: User

D. Fix a Tree time limit per test 2 seconds memory limit per test megabytes input standard input output standard OUTPU T

A Tree is an undirected connected graph without cycles.

Let's consider a rooted undirected tree with n vertices, numbered 1 through N. There is many ways to represent such a tree. One-is-to-create an array with n integers p1, p2, ..., pn, where Pi denotes a parent of vertex I (here, for Convenienc e a root is considered its own parent). The rooted tree the array p is [2, 3, 3, 2].

Given a sequence p1, p2, ..., PN, one is able to restore a tree:there must being exactly one index r that PR = R. A vertex R is a root of the tree. For all other n-1 vertices I, there are 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) is 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 is many valid sequences achievable in the minimum number of changes, print any of them. Input

The first line of the input contains a integer n (2≤n≤200)-the number of vertices in the tree.

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

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 is many such sequences, any of them would 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
Note

In the first sample, it's enough to change one element. In the provided output, a sequence represents a tree rooted in a vertex 4 (because P4 = 4), which you can see on the left D Rawing below. One of other correct solutions would is a sequence 2 3 3 2, representing a tree rooted in Vertex 3 (right drawing below). On both drawings, roots is painted red.

In the second sample, the given sequence is already valid.

Idea: The topic is asking you to get a valid array from the given array by the rules of the topic (I and vis[i] to connect with the ANS changes, so that the graph represented by the array is a tree. It is possible to record all the rings and the root of each subtree through and through the final unified connection to the root node, the ring only need to be one side of the two points at any point of the last tree root to connect him to the last tree formed root node can be.
The code is as follows:

#include <algorithm> #include <cstdio> #include <cstring> #include <cstdlib> #include < iostream> #include <vector> #include <queue> #include <stack> #include <iomanip> #include

<string> #include <climits> #include <cmath> #define MAX 110000 #define LL Long long using namespace std;
int vis[200005];
int ben[200005];

Vector<int> VX, VY;
    int findx (int x) {int r = x;
    while (r! = Ben[r]) {r = Ben[r];
    } int i = x;
        while (i! = r) {int j = ben[i];
        Ben[i] = r;
    i = j;
} return R;
    } int main () {int n;
        while (scanf ("%d", &n)! = EOF) {for (int i = 1; I <= n; i + +) ben[i] = i;
        Vx.clear ();
        Vy.clear ();
            for (int i = 1; I <= n; i + +) {scanf ("%d", &vis[i]);
            if (vis[i] = = i) {vy.push_back (i);//Save root node}else {int FX = FINDX (Vis[i]);
                int fy = findx (i);
                if (FX = = FY) {vx.push_back (i);//Save Ring} else
                {BEN[FX] = FY;
        }}} int ans = 0;
        int gen = 0;
        if (Vy.size ()) Gen = vy[0];
        else Gen = vx[0];  if (Vx.size ())//processing ring {for (int i = 0; i < vx.size (); i + +) {vis[Vx[i]
               ] = Gen;
           ans++;
            }} if (Vy.size () > 1)//processing subtree root node {for (int i = 1; i < vy.size (); i + +)
                {vis[Vy[i]] = gen;
            ans++;
        }} printf ("%d\n", ans);
        for (int i = 1; i < n; i + +) printf ("%d", vis[i]);
    printf ("%d\n", Vis[n]);
} return 0;
 }



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.