UVA 12442. Forwarding Emails

Source: Internet
Author: User

"... so forward-to-ten other people, to prove-believe the emperor has new clothes."
Aren ' t those sorts of emails annoying?
Martians get those sorts of emails too, but they has an innovative-to-be of dealing with them.
Instead of just forwarding them willy-nilly, or not @ all, they each pick one other person they know
To e-mail those things to every time-exactly one, no less, no more (and never themselves). Now, the
Martian clan chieftain wants to get a email to start going around, but he stubbornly only wants to
Send one email. Being The Chieftain, he managed to find out who forwards emails to whom, and he
Wants to Know:which Martian should he send it to maximize the number of Martians?
Input
The first line of input would contain T (≤20) denoting the number of cases.
Each case starts with a line containing an integer N (2≤n≤50000) denoting the number of
Martians in the community. Each of the next N lines contains, Integers:u V (1≤u, v≤n, u? = V)
meaning that Martian u forwards e-mail to Martian v.
Output
For each case, print the case number and a integer m, where M is the Martian that the chieftain
Should send the initial email to. If there is more than one correct answer, output the smallest number.
Sample Input
Sample Output
3
3
1
2
3
4
1
2
4
3
5
1
2
5
3
4
2
3
1
2
1
3
2
2
1
3
4
5
Sample Output
Case 1:1
Case 2:4
Case 3:3

Test instructions is said to send text messages, everyone will only send a person, ask from which people start hair, can reach the most people

The idea is that everyone starts to do the DFS again ...

No surprise, Tle.

An easy-to-think pruning is that if the point on the path before the first time, then it must be suboptimal to traverse it as a starting point.

We can use an array vis tag (note not to be confused with Vis2 for the DFS tag array, the main function of the VIS2 tag is to determine whether to loop)

Sad, it seems to improve their search posture ah ....

/************************************************************************* > File name:code/2015summer/sea#2 /b.cpp > Author:111qqz > Email: [email protected] > Created time:2015 July 28 Tuesday 14:59 16 seconds ********** **************************************************************/#include<iostream>#include<iomanip>#include<cstdio>#include<algorithm>#include<cmath>#include<cstring>#include<string>#include<map>#include<Set>#include<queue>#include<vector>#include<stack>#defineY0 ABC111QQZ#defineY1 HUST111QQZ#defineYn hez111qqz#defineJ1 CUTE111QQZ#defineTM CRAZY111QQZ#defineLR DYING111QQZusing namespacestd;#defineREP (i, n) for (int i=0;i<int (n); ++i)typedefLong Longll;typedef unsignedLong LongULL;Const intn=5e4+7;intA[n];BOOLVis[n],vis2[n];intU,v,n;intDfsintx) {    intres=0; VIS2[X]=true; intTMP =A[x]; if(!Vis2[tmp]) {Res= DFS (TMP) +1;//The length of the current pass = the length of the person he preached can be passed +1} Vis[x]=true; VIS2[X]=false; returnRes; }intMain () {intT; CIN>>T; intCAS =0;  while(t--) {memset (Vis,false,sizeof(VIS)); CAS++; scanf ("%d",&N);  for(inti =0;  I < n; i++) {scanf ("%d%d",&u,&v); A[u]=v; }      intMX =-1; intans;  for(inti =1; I <= N; i++)      {    //memset (vis2,false,sizeof (VIS2));        if(Vis[i])Continue;//If I was passed in the last Dfs, then the propagation from I must have been a sub-chain of the start propagation of the previous Mark I, certainly not excellent.        intTMP =DFS (i);//Cout<<dfs (i,1) <<endl;        if(tmp>mx) {mx=tmp; Ans=i; }} printf ("Case %d:%d\n", Cas,ans); }      return 0;}

UVA 12442. Forwarding Emails

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.