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: 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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/375700.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

大数相加

输入&#xff1a; 两个用字符串表示的大整数 如a1111111111111,b222222222222222 输出&#xff1a; 两个数的和 Solution&#xff1a; #include<iostream> #include<algorithm> #include<string>using namespace std;int add(const char&,const char&…

Linux的进程与服务(一)

启动的配置文件/etc/inittab&#xff0c;修改完配置文件以后 init q立即生效 # Default runlevel. The runlevels used by RHS are: # 0 - halt (Do NOT set initdefault to this) # 1 - Single user mode # 2 - Multiuser, without NFS (The same as 3, if you do not h…

Linux 修改swap虚拟内存大小

swap是内存的交换区&#xff1b;换句话说&#xff0c;如果内存不够用了&#xff0c;那么系统会在硬盘上存储一些内存中不常用的数据&#xff0c;之后将这部分数据在存储中析构掉&#xff1b;这样内存就又有剩余空间可以运行东东啦&#xff0c;这个过程也就是所谓的交换&#xf…

统计文章中的单词

输入&#xff1a; 字符串&#xff0c;其中可能包含空格&#xff0c;TAB&#xff0c;回车等&#xff0c;规定&#xff0c;仅字母数字和单引号算作单词部分 输出&#xff1a; 单词的个数 Solution&#xff1a; #include<iostream> #include<string>using namespac…

迈向世界 拓展未来

一切都会过去&#xff0c;只有真理永存&#xff0c;只有愿意越过事实前进一步的人&#xff0c;才能理解事实&#xff0c;这就是科学。时代在发展&#xff0c;科技更是日新月异彻底改变着我们的生活方式。现在的我们就是跟着科技发展的脚步&#xff0c;奔着梦想&#xff0c;一直…

JS - 跳转页面

<!-- 第一种&#xff1a; --><script type"text/javascript">window.location.href "login.jsp?backurl" window.location.href;</script><!-- 第二种&#xff1a; --><script type"text/javascript"&g…

分享一个用安卓手机就能引导pc安装linux系统办法

1、首先安卓手机下载软件DriveDroid.apk http://pan.baidu.com/s/1qW4pbT6 2、下载linux镜像文件放手机存储卡存储&#xff0c;放到Download/images/以下 3、打开软件会自己主动读取这个目录以下镜像&#xff0c;也能够在软件里面下载须要的镜像文件 4、软件设置usb连接模式 5、…

SharePoint 2013 开发——其他社交功能

博客地址&#xff1a;http://blog.csdn.net/FoxDave上一篇讲了如何获取用户配置文件的相关属性&#xff0c;它属于SharePoint 2013社交功能的一个小的构成部分。社交功能是SharePoint 2013改进的一大亮点。可以在现有网站上开启社交功能或者新建一个专门用于社交用途的社区网站…

第一个Qt+opencv程序

简单安装好Qt和编译安装好opencv后&#xff0c;简单实现第一个Qtopencv程序&#xff1a;读取并显示一张图片&#xff0c;这里我的Qt版本时5.9.1&#xff0c;opencv版本是4.0.1&#xff0c;版本的影响不大。 首先我们用Qt创建一个控制台项目&#xff0c;即在创建项目时选择Qt C…

redis学习笔记——应用场景

最近在看redis入门指南&#xff0c;现在就自己的学习情况说说自己的理解。 字符串类型&#xff08;String&#xff09; 字符串类型是Redis中最基本的类型&#xff0c;能存储任意形式的字符串&#xff0c;包括二进制数据。如一张照片也可以用字符串类型存储。注意字符串类型键允…

Unity的Cover flow的实现包(2个)

苹果的mac机上预览图片&#xff0c;有一个所谓的cover flow的效果&#xff0c;这里收集到两个&#xff0c;两个实现效果略有不同。 1、老外的实现 https://github.com/rakkarage/Unity3D-CoverFlow 这个焦点图片在到最后位置前会模拟一个抖动效果 2、国人的实现 http://game.ce…

卸载全部appx应用(包括应用商店)

在PowerShell中粘贴&#xff1a; Get-AppXPackage | Remove-AppxPackage 转载于:https://www.cnblogs.com/Bob-wei/p/4691854.html

求两个整数数组乘积最小值

输入&#xff1a; 三行&#xff1a; 第一行&#xff1a;n&#xff0c;表示数组的个数 第二行&#xff1a;数组a各个元素的值 第三行&#xff1a;数组b各个元素的值输出&#xff1a; 式子&#xff1a;a[0]*b[0]a[1]*b[1]...a[n-1]*b[n-1];的最小值 其中数组a中元素顺序可以调整…

Xcode快捷键整理

做iOS开发&#xff0c;避免不了的就是要和Xcode打交道&#xff0c;如果对Xcode的快捷方式熟练的话&#xff0c;这样会对我们项目开发速度有很大的提高。也是对你能力的一种提升&#xff0c;下面就是Abel整理的一些资料&#xff0c;希望有一些帮助&#xff1a; 1、文件类 新建项…

Perl语言编程学习笔记2

1. Perl中变量的常用表示 ${var} 相当于 $var $Dog::days 在Dog包里面的变量$days $#days days 的最后一个索引 $days->[2] $days 引用的数组的的第3个元素 $days[0][2] 多维数组 $days{200}{Feb} 多维哈希 $days{2000,"Feb"} 多维哈希仿真 emulation days[…

关于跨平台下end-of-line问题

因为项目需要&#xff0c;同时开发了Linux/Windows版本的程序&#xff0c;后面在实际测试过程中&#xff0c;发现两个版本之间的体现出来的行为有些不一致&#xff0c;大致情况如下&#xff1a; 1、Linux下代码能编译通过&#xff0c;Windows下编译通过 2、Linux下程序功能正常…

QImage与Mat之间的相互转换

Mat转QImage Mat matimread("c:/dev/test.jpg"); cvtColor(mat, mat, CV_BGR2RGB); QImage image(mat.data, mat.cols, mat.rows, mat.step, QImage::Format_RGB888);Qt与OpenCv之间传输数据时&#xff0c;QImage::Format_RGB888是最兼容的模式. OpenCv 加载BGR格式…

Graphviz从入门到不精通

1、安装Graphviz &#xff08;windows 版本&#xff0c;后面说linux下的安装&#xff09; 1.1&#xff09;下载安装文件 从graphviz官网下载 http://www.graphviz.org/Download.php 或者从我的百度网盘下载 http://pan.baidu.com/s/1i3mzunV 下载后双击安装&#xff0c;跟着提示…

条款32: 尽可能地推迟变量的定义

// 此函数太早定义了变量"encrypted" string encryptPassword(const string& password) {string encrypted;//默认构造函数初始化if (password.length() < MINIMUM_PASSWORD_LENGTH) {throw logic_error("Password is too short");}进行必要的操作&…

xilinx-arm-linux交叉编译链安装

1、下载交叉编译链 xilinx-2011.09-50-arm-xilinx-linux-gnueabi.bin&#xff0c;直接搜这个&#xff0c;网上有资源。 2、安装依赖库 sudo apt-get install lib32ncurses5 lib32z1 3、修改shell sudo dpkg-reconfigure -plow dash&#xff0c;在出现的界面中选no 4、安装 ./xi…