BNU 20860——Forwarding Emails——————【强连通图缩点+记忆化搜索】
Forwarding Emails
This problem will be judged on UVA. Original ID: 12442
64-bit integer IO format: %lld Java class name: Main
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!
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——————【强连通图缩点+记忆化搜索】的更多相关文章
- LightOJ1417 Forwarding Emails(强连通分量+缩点+记忆化搜索)
题目大概是,每个人收到信息后会把信息发给他认识的一个人如此下去,问一开始要把信息发送给谁这样看到信息的人数最多. 首先找出图中的SCC并记录每个SCC里面的点数,如果传到一个SCC,那么里面的人都可以 ...
- ZOJ3795 Grouping(强连通分量+缩点+记忆化搜索)
题目给一张有向图,要把点分组,问最少要几个组使得同组内的任意两点不连通. 首先考虑找出强连通分量缩点后形成DAG,强连通分量内的点肯定各自一组,两个强连通分量的拓扑序能确定的也得各自一组. 能在同一组 ...
- [BZOJ1589] [Usaco2008 Dec]Trick or Treat on the Farm 采集糖果(tarjan缩点 + 记忆化搜索)
传送门 先用tarjan缩点,再记忆话搜索一下 #include <stack> #include <cstdio> #include <cstring> #inc ...
- UVa11324 The Largest Clique(强连通分量+缩点+记忆化搜索)
题目给一张有向图G,要在其传递闭包T(G)上删除若干点,使得留下来的所有点具有单连通性,问最多能留下几个点. 其实这道题在T(G)上的连通性等同于在G上的连通性,所以考虑G就行了. 那么问题就简单了, ...
- poj3592 强连通+记忆化搜索
题意:有一片 n*m 的矿地,每一格有矿.或这传送门.或者挡路岩石.除了岩石不能走以外,其他的格子都能够向右或向下走,走到一个非岩石的格子.对于每一个矿点,经过它就能得到它的所有矿石,而对于每一个传送 ...
- 【UVA11324】 The Largest Clique (Tarjan+topsort/记忆化搜索)
UVA11324 The Largest Clique 题目描述 给你一张有向图 \(G\),求一个结点数最大的结点集,使得该结点集中的任意两个结点 \(u\) 和 \(v\) 满足:要么 \(u\) ...
- [ACM_动态规划] 数字三角形(数塔)_递推_记忆化搜索
1.直接用递归函数计算状态转移方程,效率十分低下,可以考虑用递推方法,其实就是“正着推导,逆着计算” #include<iostream> #include<algorithm> ...
- 【BZOJ-3895】取石子 记忆化搜索 + 博弈
3895: 取石子 Time Limit: 1 Sec Memory Limit: 512 MBSubmit: 263 Solved: 127[Submit][Status][Discuss] D ...
- hdu3555 Bomb (记忆化搜索 数位DP)
http://acm.hdu.edu.cn/showproblem.php?pid=3555 Bomb Time Limit: 2000/1000 MS (Java/Others) Memory ...
随机推荐
- The 'microsoft.jet.oledb.4.0' provider is not registered on the local machin
1,2选取目标站点,然后3的高级设置,4启用32位的应用程序的属性变为true就可以了.当然,网络上还有其他的版本,你也可以尝试下. 原文地址:http://weblogs.asp.net/kenco ...
- Android 学习笔记 文本文件的读写操作
activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&qu ...
- linux内存布局------深入理解计算机系统
- MultiDataTrigger
MultiDataTrigger是多条件数据触发器 和MltiTrigger是同样的,只不过前者是数据,后者是属性. 这个是基本的使用语法 <MultiDataTrigger> <M ...
- 修改Tomcat主目录
在默认安装后,tomcat的主目录是webapps/root目录,如果我们想改变tomcat的主目录的话可以这样做: 1 %TOMCAT_HOME%/webapps/下直接创建,这种方法有一个缺点,就 ...
- Mybatis注解开发
mybatis 的常用注解: @Insert:实现新增 @Update:实现更新 @Delete:实现删除 @Select:实现查询 @Result:实现结果集封装 @Results:可以与 @Res ...
- 题解 P2350 【[HAOI2012]外星人】
题目链接 还是本宝宝写题解的一贯习惯 $ :$ 先吐槽吐槽这道题$……$ 相信不少同学第一眼一定没有看懂题.(因为我也没看懂) ~~初中~~数学知识: 对于函数 $ f(x)$ 有 $f^{-1}(x ...
- arx创建作用域(于)当前文档全局变量
普通的全局变量是作用于整个AutoCAD程序中,所有的文档都将能存取,某些时候,我们需要某一个全局变量仅仅在当前文档有效,便于程序之间通信或者变量值记忆. 在高版本的objectARX向导中,已经为我 ...
- vmware vSphere虚拟网络(一)
为了更好的了解vSphere网络虚拟化解决方案,这里引入了一些概念,以便我们更好的了解虚拟网络. 一.网卡: 物理网卡称为vmnic,在ESXi中,第一块物理网卡叫做vmnic0,第二块叫做vmnic ...
- Python之路Python文件操作
Python之路Python文件操作 一.文件的操作 文件句柄 = open('文件路径+文件名', '模式') 例子 f = open("test.txt","r&qu ...