“... 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;
}

转载于:https://www.cnblogs.com/111qqz/p/4683075.html

uva 12442 . Forwarding Emails相关推荐

  1. html怎么用空格占位符,HTML空格占位符

    给深度学习入门者的Python快速教程 - 番外篇之Python-OpenCV 这次博客园的排版彻底残了..高清版请移步: https://zhuanlan.zhihu.com/p/24425116 ...

  2. 我如何构建heimdall开源个人电子邮件监护人

    I love receiving spam mail! - No one ever. 我喜欢收到垃圾邮件! - 没有人. I use Gmail, which does a fantastic job ...

  3. apple store 慢_建议改善与Apple Store(以及一般的Cloud Services)的客户互动

    apple store 慢 备用标题:"良好的欺诈检测是什么样的"(Alternative Title: "What good fraud detection looks ...

  4. [搜索]UVa 129 困难的串

    题意:将一个包含两个相邻的重复子串的子串,称为"容易的串",其他为"困难的串". 输入正整数n和l,输出由前l个字符组成的,字典序第n小的困难的串. 输入样例: ...

  5. uva 401.Palindromes

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  6. Uva 3767 Dynamic len(set(a[L:R])) 树套树

    Dynamic len(set(a[L:R])) Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 https://uva.onlinejudge.org/in ...

  7. UVA 11752 超级幂

    UVA 11752 超级幂 Z - The Super Powers Time Limit:1000MS     Memory Limit:0KB     64bit IO Format:%lld & ...

  8. UVa 11174 - Stand in a Line

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  9. UVa 10112 - Myacm Triangles

    UVa第一卷最后一题. 求内部不含点并且面积最大的三角形. 暴力. 代码如下: 1 #include<iostream> 2 #include<cstdio> 3 #inclu ...

最新文章

  1. iOS 跑马灯之 TXScrollLabelView
  2. 模板文件默认标准版首页模板.html未找到!,html页模板
  3. 线程的状态:分离(detached)和joinable(可结合的)
  4. 项目管理十大知识领域之项目进度管理
  5. raft论文 中文 pdf_八篇论文总结BERT+KG:预训练模型与知识图谱相结合的研究进展...
  6. Spring MVC:使用SimpleUrlHandlerMapping的一个简单例子
  7. 关于sklearn中“决策树是否可以转化为json并进行绘制”的调研
  8. 传递参数的2种情况的理解。
  9. 如何与深度学习服务器优雅的交互?(长期更新)
  10. 内联命名空间(inline namespace)
  11. QML工作笔记-为PushButton戴上皮肤
  12. Android后台服务---无交互时的Service
  13. 一维FDTD等离子体的Matlab,修正过的一维FDTD等离子体MATLAB代码(公式修正)
  14. Java数据结构和算法(四)--链表
  15. python获取url参数_python测试开发django(4)--获取url参数和name的作用
  16. TOMCAT SSL 配置
  17. 北风网66jquery
  18. 超简单的倒计时代码编写
  19. 计算机应用技术题,计算机应用技术复习题.doc
  20. 动手创建docker镜像

热门文章

  1. angularjs 服务
  2. 卷积,DFT,FFT,图像FFT,FIR 和 IIR 的物理意义
  3. 华为鸿蒙话题作文800字,关于鸿蒙OS 华为最高层发布最新通知:统一口径-华为,智能手机,鸿蒙...
  4. 深入解读首个万亿级语言模型 Switch Transformer
  5. 战略分析思路——商业模式及沙盘推演
  6. argparse模块_Argparse:一个具体案例教会你python命令行参数解析
  7. excel 公式 单引号 concat_Excel两个新函数帮你快速搞定批量连接
  8. java虚拟机_Oracle独家:Java虚拟机学习指南
  9. java fx 内置图标_图标 – 如何在Windows上为javafx本机程序包图标设置自定义图标...
  10. 读《第一次把事情做对》有感