uva 12442 . Forwarding Emails

“... 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: [email protected]
    > 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;
}
时间: 2024-08-21 17:51:36

uva 12442 . Forwarding Emails的相关文章

BNU 20860——Forwarding Emails——————【强连通图缩点+记忆化搜索】

Forwarding Emails Time Limit: 1000ms Memory Limit: 131072KB This problem will be judged on UVA. Original ID: 1244264-bit integer IO format: %lld      Java class name: Main Prev Submit Status Statistics Discuss Next Type: None None Graph Theory 2-SAT

LightOJ1417 Forwarding Emails(强连通分量+缩点+记忆化搜索)

题目大概是,每个人收到信息后会把信息发给他认识的一个人如此下去,问一开始要把信息发送给谁这样看到信息的人数最多. 首先找出图中的SCC并记录每个SCC里面的点数,如果传到一个SCC,那么里面的人都可以看到信息. 然后SCC缩点后就形成DAG,直接记忆化搜索,d(u)搜索从u点出发开始传最多能传多少人. 最后就是找答案了. 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namesp

BNU Training 2015 07 27 题解

[比赛链接]:click here~~ uva 12435 C. Consistent Verdicts [题目大意]:给你二维平面一些人的坐标,每个人手上都有一把枪,求全部人同时开枪后所有人听到枪声的次数的可能数目. [解题思路]:O(n^2)暴力枚举+unique 函数去重相邻元素.居然只跑了3ms,~~ 代码: <span style="font-size:14px;">// C #ifndef _GLIBCXX_NO_ASSERT #include <cass

UVA 562 Dividing coins --01背包的变形

01背包的变形. 先算出硬币面值的总和,然后此题变成求背包容量为V=sum/2时,能装的最多的硬币,然后将剩余的面值和它相减取一个绝对值就是最小的差值. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; #define N 50007 int c[102],d

UVA 10341 Solve It

Problem F Solve It Input: standard input Output: standard output Time Limit: 1 second Memory Limit: 32 MB Solve the equation: p*e-x + q*sin(x) + r*cos(x) + s*tan(x) + t*x2 + u = 0 where 0 <= x <= 1. Input Input consists of multiple test cases and te

UVA 11014 - Make a Crystal(容斥原理)

UVA 11014 - Make a Crystal 题目链接 题意:给定一个NxNxN的正方体,求出最多能选几个整数点.使得随意两点PQ不会使PQO共线. 思路:利用容斥原理,设f(k)为点(x, y, z)三点都为k的倍数的点的个数(要扣掉一个原点O).那么全部点就是f(1),之后要去除掉共线的,就是扣掉f(2), f(3), f(5)..f(n).n为素数.由于这些素数中包括了合数的情况,而且这些点必定与f(1)除去这些点以外的点共线,所以扣掉.可是扣掉后会扣掉一些反复的.比方f(6)在f

[UVa] Palindromes(401)

UVA - 401 Palindromes Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description A regular palindrome is a string of numbers or letters that is the same forward as backward. For example, the string "ABCDED

uva 401.Palindromes

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=342 题目意思:给出一段字符串(大写字母+数字组成).判断是否为回文串 or 镜像串 or 回文镜像串 or 什么都不是.每个字母的镜像表格如下 Character Reverse Character Reverse Character Reverse A A M M Y Y B

[2016-02-19][UVA][129][Krypton Factor]

UVA - 129 Krypton Factor Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Submit Status Description You have been employed by the organisers of a Super Krypton Factor Contest in which contestants have very high mental and physica