标签:
“... so forward this to ten other people, to prove that you believe the emperor has new clothes.”
Aren’t those sorts of emails annoying?
Martians get those sorts of emails too, but they have an innovative way of dealing with them.
Instead of just forwarding them willy-nilly, or not at all, they each pick one other person they know
to email those things to every time - exactly one, no less, no more (and never themselves). Now, the
Martian clan chieftain wants to get an 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 that see it?
Input
The first line of input will 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 two integers: u v (1 ≤ u, v ≤ N , u ? = v)
meaning that Martian u forwards email to Martian v.
Output
For each case, print the case number and an 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
题意是说发短信,每个人只会给一个人发,问从哪个人开始发,能传到的人最多
思路是每个人开始做一遍dfs...
毫无意外的TLE了
一个容易想到的剪枝是,如果在第i次之前的路径上的点,在之后以它作为起点遍历一定不优.
我们可以用一个数组vis标记上(注意不要和为了dfs的标记数组vis2混淆,vis2标记的主要作用是判断是否成环)
sad,看来还是要提高自己的搜索姿势啊....
/************************************************************************* > File Name: code/2015summer/sea#2/B.cpp > Author: 111qqz > Email: rkz2013@126.com > Created Time: 2015年07月28日 星期二 14时59分16秒 ************************************************************************/ #include<iostream> #include<iomanip> #include<cstdio> #include<algorithm> #include<cmath> #include<cstring> #include<string> #include<map> #include<set> #include<queue> #include<vector> #include<stack> #define y0 abc111qqz #define y1 hust111qqz #define yn hez111qqz #define j1 cute111qqz #define tm crazy111qqz #define lr dying111qqz using namespace std; #define REP(i, n) for (int i=0;i<int(n);++i) typedef long long LL; typedef unsigned long long ULL; const int N=5E4+7; int a[N]; bool vis[N],vis2[N]; int u,v,n; int dfs(int x) { int res=0; vis2[x]=true; int tmp = a[x]; if (!vis2[tmp]) { res = dfs(tmp)+1; //当前这个认能传的长度=他传的人能传的长度+1 } vis[x] = true; vis2[x] = false; return res; } int main() { int T; cin>>T; int cas = 0; while (T--) { memset(vis,false,sizeof(vis)); cas++; scanf("%d",&n); for ( int i = 0 ; i < n; i++ ) { scanf("%d %d",&u,&v); a[u]=v; } int mx = - 1; int ans; for ( int i = 1 ; i <= n ; i++) { // memset(vis2,false,sizeof(vis2)); if (vis[i]) continue; //如果在上一次的dfs中经过了i,那么从i开始传播一定是之前标记i的那次开始传播的子链,一定不优. int tmp = dfs(i); // cout<<dfs(i,1)<<endl; if (tmp>mx) { mx = tmp; ans = i; } } printf("Case %d: %d\n",cas,ans); } return 0; }
标签:
原文地址:http://www.cnblogs.com/111qqz/p/4683075.html