P2661 information transmission and p2661 Information Transmission

Source: Internet
Author: User
Tags rounds

P2661 information transmission and p2661 Information Transmission
Description

N students (numbered 1 to n) are playing an information transfer game. In the game, each person has a fixed Information Transmission object. Specifically, the information transmission object for students numbered I is Ti.

At the beginning of the game, everyone only knows their birthdays. In each round, everyone will also inform their respective information about their current birthday information (NOTE: Some people may obtain information from several people, but each person will only tell one person the information, that is, the object of his own information ). When someone knows their birthday from someone else's mouth, the game ends. How many rounds can this game play?

Input/Output Format

Input Format:

 

Enter two rows in total.

The first row contains one positive integer. n indicates n people.

The fifth line contains n positive integers separated by spaces: T1, T2 ,......, In Tn, the I-th integer Ti indicates I

The student's information is transmitted to students numbered Ti, Ti ≤ n and Ti = I

Data ensures that the game is over.

 

Output Format:

 

The output contains one row and an integer, indicating the total number of rounds the game can perform.

 

Input and Output sample input sample #1:
52 4 2 3 1
Output sample #1:
3
Description

Example 1

The game process. After 3rd rounds of games, players on the 4th will hear the 2nd tell him

My birthday, so the answer is 3. Of course, after 3rd rounds of games, players on the 2nd and 3rd can get their messages

The source knows that his birthday is also in line with the conditions for the end of the game.

For 30% of data, n ≤ 200;

For 60% of data, n ≤ 2500;

For 100% of data, n ≤ 200000.

 

 

Many people use bare DFS to score 70 or 80 points.

Then, the feeling of self-destruction never breaks 80 points (from the discussion board)

Why does it time out?

Because a lot of useless searches are performed

However, the answer has been fixed during the search.

So we can add a tot variable similar to the card.

Exit when it is greater than a number (I obtain 4384380, which cannot be greater than 10 ^ 8 ).

Output the current optimal solution.

Bare DFS is not only easy to understand, but also has short code

Create a linked list and search for it.

Make sure to judge the useless ring.

 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 using namespace std; 6 const int MAXN=200001; 7 void read(int & n) 8 { 9     char c='+';int x=0;10     while(c<'0'||c>'9')11     c=getchar();12     while(c>='0'&&c<='9')13     {14         x=x*10+(c-48);15         c=getchar();16     }17     n=x;18 }19 int n;20 int ans=0x7ffffff;21 int a[MAXN];22 int tot=0;23 int main()24 {25     read(n);26     for(int i=1;i<=n;i++)27     read(a[i]);28     for(int i=1;i<=n;i++)29     {30         int bg=i;31         int now=i;32         int num=0;33         while(a[now]!=bg)34         {35             tot++;36             num++;37             now=a[now];38             if(num>n+1||num>ans)39             {40                 num=0x7ffffff;41                 break;42             }43         }44         if(tot>4384380)45         break;46         ans=min(num+1,ans);47     }48     printf("%d",ans);49     return 0;50 }

 

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.