[BestCoder Round #7] hdu 4985 Little Pony and Permutation (find loop section ),

Source: Internet
Author: User

[BestCoder Round #7] hdu 4985 Little Pony and Permutation (find loop section ),

Little Pony and Permutation


Problem Description
As a unicorn, the ability of using magic is the distinguishing feature among other kind of pony. being familiar with composition and decomposition is the fundamental course for a young unicorn. twilight Sparkle is interested in the decomposition of permutations. A permutation of a set S = {1, 2 ,..., n} is a bijection from S to itself. in the great magician -- Cauchy's two-line notation, one lists the elements of set s in the first row, and then for each element, writes its image under the permutation below it in the second row. for instance, a permutation of set {1, 2, 3, 4, 5} σ can be written:


Here σ (1) = 2, σ (2) = 5, σ (3) = 4, σ (4) = 3, and σ (5) = 1.
Twilight Sparkle is going to decompose the permutation into some disjoint cycles. For instance, the above permutation can be rewritten:


Help Twilight Sparkle find Lexicographic smallest solution. (Only considering numbers ). 
InputInput contains multiple test cases (less than 10 ). for each test case, the first line contains one number n (1 <= n <= 10 ^ 5 ). the second line contains n numbers which the I-th of them (start from 1) is σ (I ).
OutputFor each case, output the corresponding result.
Sample Input
52 5 4 3 131 2 3
 
Sample Output
(1 2 5)(3 4)(1)(2)(3)
 
SourceBestCoder Round #7


Solution:

Find the loop section and use the while loop. Pay attention to the output format.

Code:

#include <iostream>#include <stdio.h>#include <string.h>using namespace std;const int maxn=100010;int num[maxn];bool vis[maxn];int n;void getCircle(){    memset(vis,0,sizeof(vis));    int t;    bool ok=0;    for(int i=1;i<=n;i++)    {        int temp=i;        if(!vis[temp])            printf("(");        while(!vis[temp])        {            t=temp;            vis[temp]=true;            temp=num[temp];            if(!vis[temp])                printf("%d ",t);            else            {                printf("%d",t);                ok=1;            }        }        if(ok)        {             printf(")");             ok=0;        }    }    printf("\n");}int main(){    while(scanf("%d",&n)!=EOF)    {        for(int i=1;i<=n;i++)            scanf("%d",&num[i]);        getCircle();    }    return 0;}



What is round?

Round
[Raund]
N.
Circle, circle, tour, round singing, [body]
Adj.
Round, spherical, plump, approximate, full, complete, straight, obese
Prep.
Around, nearby, bypassing, around...
Vt.
Round, round, complete, round
Vi.
Turn round, gain weight, walk around, turn, progress
Adv.
Around, cyclically, around, roundabout, and in the opposite direction, one by one

Round
Round 1
AHD: [round]
D. J. [raund]
K. K. [ra & nd]
Adj. Abbr. rd., rnd. (adjective) stands for rd., rnd.
Being such that every part of the surface or the circumference is equidistant from the center:
Circle: the distance between any part of the surface or circumference and the center is equal:
A round ball.
Ball
Moving in or forming a circle.
Of or forming circles.
Shaped like a cylinder; cylindrical.
Cylindrical; cylindrical
Rather rounded in shape:
Approximately circular:
The child's round face.
Child's face
Full in physique; plump:
Plump; plump:
A round figure.
Plump shape
Linguistics Formed or articulated with the lips in a rounded shape:
[Linguistics] of the lips: The lips in the shape of a circle or the lips with a vowel:
A round vowel.
Round lip vowel
Full in tone; sonorous.
Sound rounded; brilliant
Whole or complete; full:
Complete, complete; full:
A round dozen.
A dozen
Mathematics Expressed or designated as a whole number or integer; not fractional.
[Mathematics] of integers: Expressed by integers rather than scores.
Not exact; approximate:
Not exact; roughly:
A round estimate.
Rough Estimation
Large; considerable:
Large; considerable:
A round sum of money.
A considerable amount of money
Brought to satisfactory conclusion or completion; finished.
Successful: the outcome is satisfactory or satisfactory; perfect
Outspoken; blunt:
Frank; outspoken:
A round scolding.
Direct criticism
Done with full force; unrestrained:
Harsh and crude: violent; unrestricted:
Gave me a round thrashing.
Beat me up
N. (TERM)
Something, such as a circle, disk, globe, or ring, that is round.
A sphere: a circle, disc, ball, or ring.
A circle formed of various things.
Circle: A circle surrounded by various things
Movement around a circle or about an axis.
Rotation: circle or circle axis rotation
A rung or... the remaining full text>

Round usage and meaning (English)

Round
US: [ra hybrid nd] English: [ra hybrid nd]
N. rotation; inspection; Series; Circle
Adv. Surround; nearby; everywhere; Four Sides
Prep. Surround; in... Always
V. Bypass; turn over; turn into a circle; turn... Rounding
Adj. Circular; spherical; obese; ARC

1 circular; annular; spherical shaped like a circle or a ball
A round plate circle plate
These glasses suit people with round faces.

This pair of glasses is suitable for people with round faces.

The fruit are small and round.

This fruit is small and round.

Rugby isn' t played with a round ball.

The rugby match does not use a ball.

The discovery that the world is round

The discovery that the earth is round

The child was watching it all with big round eyes (=
Showing interest ).

The child looked at it with wide and round eyes.

A T-shirt with a round neck

Round neck t-shirt

2. Arc having a curved shape

The round green hills of Donegal

Those round green hills of donegor

Round brackets (= in writing)

Parentheses

She had a small mouth and round pink cheeks.

Her mouth was small and her face was red.

3 [obn] integer; the ending number is a of 0 (or 5)
<Dhb> round </dhb> figure or amount is one that is given as a whole
Number, usually one ending in 0 or 5

1 rotation; surround; circle moving in a circle

Everybody joins hands and dances round.

Everyone started to dance in a circle.

How do you make the wheels go round?

How did you turn the wheel?

The children were spinning round and round.

The children turned around.

The thought kept going round and round in her head.

This idea has been lingering in her mind.

2 perimeter; surrounding; around a whole circle measuring or marking the edge or outside
Something

A young tree measuring only 18 inches round

Small trees with a circumference of only 18 inch

They 've built a high fence all round to keep intruders
Out.

They erected a high wall around them to prevent outsiders from entering.

3.
All sides of sb/something

A large crowd had gathered round to watch.
... The remaining full text>

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.