Rokua P2661 Information Transfer ==coedevs4511 information transfer NOIP2015 Day1 T2

Source: Internet
Author: User
Tags rounds

P2661 Information Transfer Topic description

There are n classmates (numbered 1 to N) playing a message-passing game. In the game each person has a fixed message to pass the object, wherein, number I of the classmate's information Transfer object is numbered TI classmate.

At the beginning of the game, everyone only knew their birthdays. In each round, everyone will tell their own information about their current birthday message (note: Someone may get information from several people, but each person will only tell one person, that is, their message). The game is over when someone learns their birthday from someone else's mouth. How many rounds can the game take?

Input output Format input format:

Enter a total of 2 rows.

Line 1th contains 1 positive integers n for n people.

Line 2nd contains n spaces separated by a positive integer t1,t2,......, tn where the i integer TI shows number i

The students of the information Transfer object is numbered Ti classmate, Ti≤n and Ti≠i

Data guarantee the game will end.

Output format:

Output a total of 1 lines, containing 1 integers, which indicates how many rounds the game can carry out altogether.

Input and Output Sample input example # #:
52 4 2) 3 1
Sample # # of output:
3
Description

Example 1 explanation

The process of the game. After the 3rd round of games, player number 4th will hear that player number 2nd tells him

Birthday, so the answer is 3. Of course, after the 3rd round of games, player number 2nd and player 3rd will be able to get a message from themselves

Sources know that their birthdays are also eligible for the end of the game.

For 30% of data, n≤200;

For 60% of data, n≤2500;

For 100% of data, n≤200000.

Puzzle: Tarjan's board, ans: The number of the first sum[i]>1

#include <cstdio>#include<cstring>#include<iostream>#include<algorithm>#include<vector>#include<stack>using namespacestd;#defineN 200010Vector<int>Grap[n];stack<int>s;intLow[n];intDfn[n];intMark[n];intId[n];intPD;intSD;intSum[n];voidTarjan (intv) {Low[v]=dfn[v]=++PD;     S.push (v); MARK[V]=1;  for(intI=0; I<grap[v].size (); i++){        intw=Grap[v][i]; if(!Dfn[w])            {Tarjan (w); LOW[V]=min (low[v],low[w]); }        Else if(Mark[w]) {Low[v]=min (low[v],dfn[w]); }        }    intu; if(low[v]==Dfn[v]) {SD++;  Do{u=S.top ();            S.pop (); Id[u]=SD; SUM[SD]++; Mark[u]=0; } while(u!=v); }}intMain () {intn,m,a,b; scanf ("%d",&N);  for(intI=1; i<=n;i++) {scanf ("%d",&a);    Grap[i].push_back (a); }     for(intI=1; i<=n;i++){        if(!Dfn[i]) Tarjan (i); } sort (Sum+1, sum+sd+1);  for(intI=1; i<=sd;i++){        if(sum[i]>1) {printf ("%d\n", Sum[i]); return 0; }    }    return 0;}

Rokua P2661 Information Transfer ==coedevs4511 information transfer NOIP2015 Day1 T2

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.