Forwarding Emails

Time Limit: 1000ms
Memory Limit: 131072KB

This problem will be judged on UVA. Original ID: 12442
64-bit integer IO format: %lld      Java class name: Main

Type:

None

 

None

Graph Theory

2-SAT

Articulation/Bridge/Biconnected Component

Cycles/Topological Sorting/Strongly Connected Component

Shortest Path

Bellman Ford

Dijkstra/Floyd Warshall

Euler Trail/Circuit

Heavy-Light Decomposition

Minimum Spanning Tree

Stable Marriage Problem

Trees

Directed Minimum Spanning Tree

Flow/Matching

Graph Matching

Bipartite Matching

Hopcroft–Karp Bipartite Matching

Weighted Bipartite Matching/Hungarian Algorithm

Flow

Max Flow/Min Cut

Min Cost Max Flow

DFS-like

Backtracking with Pruning/Branch and Bound

Basic Recursion

IDA* Search

Parsing/Grammar

Breadth First Search/Depth First Search

Advanced Search Techniques

Binary Search/Bisection

Ternary Search

Geometry

Basic Geometry

Computational Geometry

Convex Hull

Pick's Theorem

Game Theory

Green Hackenbush/Colon Principle/Fusion Principle

Nim

Sprague-Grundy Number

Matrix

Gaussian Elimination

Matrix Exponentiation

Data Structures

Basic Data Structures

Binary Indexed Tree

Binary Search Tree

Hashing

Orthogonal Range Search

Range Minimum Query/Lowest Common Ancestor

Segment Tree/Interval Tree

Trie Tree

Sorting

Disjoint Set

String

Aho Corasick

Knuth-Morris-Pratt

Suffix Array/Suffix Tree

Math

Basic Math

Big Integer Arithmetic

Number Theory

Chinese Remainder Theorem

Extended Euclid

Inclusion/Exclusion

Modular Arithmetic

Combinatorics

Group Theory/Burnside's lemma

Counting

Probability/Expected Value

Others

Tricky

Hardest

Unusual

Brute Force

Implementation

Constructive Algorithms

Two Pointer

Bitmask

Beginner

Discrete Logarithm/Shank's Baby-step Giant-step Algorithm

Greedy

Divide and Conquer

Dynamic Programming

Tag it!

[PDF Link]

J

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

Output for Sample Input

3
3
1 2
2 3
3 1
4
1 2
2 1
4 3
3 2
5
1 2
2 1
5 3
3 4
4 5
Case 1: 1
Case 2: 4
Case 3: 3

题目大意:现在要转发邮件,有n个人,有n条边,表示u--->v可以转发邮件。有一个人想要在这n个人中选一个人作为起点进行邮件转发。想要让转发给的人尽量多,问从哪里开始转发可以满足条件。

解题思路:想转发给的人尽量多,即要求让经过的点尽量多。我们可以在有向图上进行强连通缩点。然后在缩点形成的DAG上进行DFS。然后遍历判断从哪个连通块出发能经过最多的点,在求出该连通块的最小顶点编号即为结果。

#include<bits/stdc++.h>
using namespace std;
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
const int maxn=50050;
vector<int>G[maxn];
int pre[maxn],lowlink[maxn],sccno[maxn],dfs_clock,scc_cnt;
int dp[maxn],d[maxn];
stack<int>S;
set<int>ST[maxn];//新的DAG需要去重边
//tarjan 模板
void dfs(int u){
pre[u]=lowlink[u]=++dfs_clock;
S.push(u);
for(int i=0;i<G[u].size();i++){
int v=G[u][i];
if(!pre[v]){
dfs(v);
lowlink[u]=min(lowlink[u],lowlink[v]);
}else if(!sccno[v]){
lowlink[u]=min(lowlink[u],pre[v]);
}
}
if(lowlink[u]==pre[u]){
scc_cnt++;
for(;;){
int x=S.top();S.pop();
sccno[x]=scc_cnt;
if(x==u)
break;
}
}
}
void find_scc(int n){
dfs_clock=scc_cnt=0;
while(!S.empty())
S.pop();
memset(sccno,0,sizeof(sccno));
memset(pre,0,sizeof(pre));
for(int i=0;i<n;i++){
if(!pre[i])
dfs(i);
}
}
int DFS(int u){
if(dp[u])
return dp[u];
dp[u]=d[u];
for(set<int>::iterator it=ST[u].begin();it!=ST[u].end();it++){
int v=*it;
if(!dp[v]){
dp[u]+=DFS(v);
}else {
dp[u]+=dp[v];
}
}
return dp[u];
}
int main(){
int t,n,a,b,cnt=0;
scanf("%d",&t);
while(t--){
memset(dp,0,sizeof(dp));
memset(d,0,sizeof(d));
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d%d",&a,&b);
a--,b--;
G[a].push_back(b);
}
find_scc(n);
for(int i=0;i<n;i++){
d[sccno[i]]++;
}
for(int i=0;i<n;i++){
for(int j=0;j<G[i].size();j++){
int v=G[i][j];
if(sccno[i]!=sccno[v])//SB了,开始忘了加这个条件,要是不同的连通块
ST[sccno[i]].insert(sccno[v]);
}
}
for(int i=1;i<=scc_cnt;i++){//在连通块形成的DAG上记忆化搜索
if(!dp[i]){
DFS(i);
}
}
int sum=0,ck=0;
for(int i=1;i<=scc_cnt;i++){
if(dp[i]>sum){
ck=i;
sum=dp[i];
}
}
int ok=0;
for(int i=0;i<n;i++){
if(sccno[i]==ck){
ok=i;
break;
}
}
printf("Case %d: %d\n",++cnt,ok+1);
for(int i=0;i<=n;i++)
G[i].clear();
for(int i=1;i<=scc_cnt;i++){
ST[i].clear();
}
}
return 0;
} /*
55
4
1 2
2 1
4 3
3 2 5
1 2
2 1
5 3
3 4
4 5
*/

  

BNU 20860——Forwarding Emails——————【强连通图缩点+记忆化搜索】的更多相关文章

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

    题目大概是,每个人收到信息后会把信息发给他认识的一个人如此下去,问一开始要把信息发送给谁这样看到信息的人数最多. 首先找出图中的SCC并记录每个SCC里面的点数,如果传到一个SCC,那么里面的人都可以 ...

  2. ZOJ3795 Grouping(强连通分量+缩点+记忆化搜索)

    题目给一张有向图,要把点分组,问最少要几个组使得同组内的任意两点不连通. 首先考虑找出强连通分量缩点后形成DAG,强连通分量内的点肯定各自一组,两个强连通分量的拓扑序能确定的也得各自一组. 能在同一组 ...

  3. [BZOJ1589] [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果(tarjan缩点 + 记忆化搜索)

    传送门 先用tarjan缩点,再记忆话搜索一下 #include <stack> #include <cstdio> #include <cstring> #inc ...

  4. UVa11324 The Largest Clique(强连通分量+缩点+记忆化搜索)

    题目给一张有向图G,要在其传递闭包T(G)上删除若干点,使得留下来的所有点具有单连通性,问最多能留下几个点. 其实这道题在T(G)上的连通性等同于在G上的连通性,所以考虑G就行了. 那么问题就简单了, ...

  5. poj3592 强连通+记忆化搜索

    题意:有一片 n*m 的矿地,每一格有矿.或这传送门.或者挡路岩石.除了岩石不能走以外,其他的格子都能够向右或向下走,走到一个非岩石的格子.对于每一个矿点,经过它就能得到它的所有矿石,而对于每一个传送 ...

  6. 【UVA11324】 The Largest Clique (Tarjan+topsort/记忆化搜索)

    UVA11324 The Largest Clique 题目描述 给你一张有向图 \(G\),求一个结点数最大的结点集,使得该结点集中的任意两个结点 \(u\) 和 \(v\) 满足:要么 \(u\) ...

  7. [ACM_动态规划] 数字三角形(数塔)_递推_记忆化搜索

    1.直接用递归函数计算状态转移方程,效率十分低下,可以考虑用递推方法,其实就是“正着推导,逆着计算” #include<iostream> #include<algorithm> ...

  8. 【BZOJ-3895】取石子 记忆化搜索 + 博弈

    3895: 取石子 Time Limit: 1 Sec  Memory Limit: 512 MBSubmit: 263  Solved: 127[Submit][Status][Discuss] D ...

  9. hdu3555 Bomb (记忆化搜索 数位DP)

    http://acm.hdu.edu.cn/showproblem.php?pid=3555 Bomb Time Limit: 2000/1000 MS (Java/Others)    Memory ...

随机推荐

  1. Xcode打包提交至itunes connect后,提交审核成功,随后出现二进制文件无效

    1.问题描述 Xcode打包提交至itunes connect后,提交审核成功,应用处于待审核状态,过了大概半个小时状态更改为二进制文件无效 2.原因分析 2.1 登陆在苹果中预留的邮箱 ---- 邮 ...

  2. 第十一篇 logging模块

    logging模块是Python中内置的很强大的一个日志模块,它可以帮我们记录程序运行的情况,对于后续排错有很好的帮助. logging模块定义了下表所示的日志级别,按照严重程度由低到高排列: 级别 ...

  3. Selenium API(二)

    1.定位一组元素 WebDriver提供了8种定位一组元素的方法. driver.find_elements_by_css_selector() driver.find_elements_by_tag ...

  4. Intellij IDEA神器那些让人爱不释手的小技巧

      完整的IDEA使用教程,GitHub地址: https://github.com/judasn/IntelliJ-IDEA-Tutorial 概述 之前写了一篇介绍IntellIJ IDEA的文章 ...

  5. kali linux之手动漏洞挖掘二

    漏洞挖掘原则/宗旨----所有变量,所有头(cookie中的变量),逐个变量删除 身份认证 常用若口令/基于字典的密码爆破/锁定帐号 信息收集(手机号,身份证,住址信息等等) 密码嗅探 会话sessi ...

  6. bzoj2115(线性基)

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2115 题意:求图中路径1~n上最大边权 xor 和 思路:参见 blog http://b ...

  7. WKWebView 的使用和封装

    WKWebView 的使用和封装 前言 项目中有个新闻资讯模块展示公司和相关行业的最新动态. 这个部分基本是以展示网页为主,内部可能会有一些 native 和 JS 代码的交互. 因为是新项目,所以决 ...

  8. [Swift]八大排序算法(三):选择排序 和 简单选择排序

    排序分为内部排序和外部排序. 内部排序:是指待排序列完全存放在内存中所进行的排序过程,适合不太大的元素序列. 外部排序:指的是大文件的排序,即待排序的记录存储在外存储器上,待排序的文件无法一次装入内存 ...

  9. Jenkins持续集成企业实战系列之Jenkins配置演示-----03

    注:原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.    最初接触Jenkins也是由于公司需求,根据公司需求Java代码项目升级的.(公司是 ...

  10. grafana使用小节

    安装准备 安装grafana 安装mysql grafana操作步骤 新建数据源,支持mysql 数据库连接失败处理: https://www.jianshu.com/p/684bc3a77ac9 新 ...