Tarjan缩点求入度为零的点的个数问题
Description:
一堆人需要联系,但如果x 可以联系 y,你联系了x就不用联系y了,你联系一个人都会有固定的花费,问你最小联系多少人,和最小花费
Solution:
Tarjan缩点,求出缩点的入度,如果为0则代表这个缩点需要联系一次,缩点的时候维护好根点到达该缩点的最小值即可
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath> using namespace std;
const int maxn = 1e4 + 1e2;
const int maxm = 2e4 + 2e2; //tarjan
int dfn[maxn],low[maxn],tot; //edge
struct node{
int to,pre;
}e[maxn];
int id[maxn],cnt; //color
int col[maxn],cid;
int in[maxn];
//stack
int stk[maxn],siz;
bool instk[maxn]; //Pdata
int cost[maxn];
int mincost[maxn];
void init()
{
memset(id,-1,sizeof(id));
cnt = tot = cid = siz = 0;
memset(dfn,0,sizeof(dfn));
memset(mincost,-1,sizeof(mincost));
memset(instk,0,sizeof(instk));
memset(stk,0,sizeof(stk));
memset(in,0,sizeof(in));
}
void add(int from,int to)
{
e[cnt].to = to;
e[cnt].pre = id[from];
id[from] = cnt++;
}
void tarjan(int now)
{
dfn[now] = low[now] = ++tot;
stk[siz++] = now;
instk[now] = true; for(int i = id[now];~i;i = e[i].pre)
{
int to = e[i].to;
if(!dfn[to])
{
tarjan(to);
low[now] = min(low[now],low[to]);
}
else if(instk[to])
{
low[now] = min(low[now],dfn[to]);
}
} if(dfn[now] == low[now])
{
++cid;
while(siz > 0 && stk[siz] != now)
{
--siz;
instk[stk[siz]] = false;
col[stk[siz]] = cid;
if(mincost[cid] == -1)mincost[cid] = cost[stk[siz]];
else mincost[cid] = min(mincost[cid],cost[stk[siz]]);
}
}
}
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
init();
for(int i = 1;i <= n;++i)
scanf("%d",&cost[i]);
int from,to;
for(int i = 1;i <= m;++i)
{
scanf("%d%d",&from,&to);
add(from,to);
}
for(int i = 1;i <= n;++i)
{
if(!dfn[i])tarjan(i);
}
for(int now = 1;now <= n;++now)
{
for(int i = id[now];~i;i = e[i].pre)
{
int to = e[i].to;
if(col[now] != col[to])
{
++in[col[to]];
}
}
}
int rescnt = 0,ressum = 0;
for(int i = 1;i <= cid;++i)
{
if(!in[i])
{
ressum += mincost[i];
++rescnt;
}
} printf("%d %d\n",rescnt,ressum);
}
return 0;
}
Tarjan缩点求入度为零的点的个数问题的更多相关文章
- POJ-3352 Road Construction,tarjan缩点求边双连通!
Road Construction 本来不想做这个题,下午总结的时候发现自己花了一周的时间学连通图却连什么是边双连通不清楚,于是百度了一下相关内容,原来就是一个点到另一个至少有两条不同的路. 题意:给 ...
- tarjan 缩点 求 scc
算法学自 BYVoid https://www.byvoid.com/zhs/blog/scc-tarjan/ 这个写得很清楚了 当然 你可能不这么认为 而且 如果是让我 一开始就从这个博客 学 ta ...
- POJ1236 (强连通分量缩点求入度为0和出度为0的分量个数)
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 13804 Accepted: 55 ...
- HDU 4612 Warm up tarjan缩环+求最长链
Warm up Problem Description N planets are connected by M bidirectional channels that allow instant ...
- poj 3177 Redundant Paths【求最少添加多少条边可以使图变成双连通图】【缩点后求入度为1的点个数】
Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11047 Accepted: 4725 ...
- Grouping ZOJ - 3795 (tarjan缩点求最长路)
题目链接:https://cn.vjudge.net/problem/ZOJ-3795 题目大意:给你n个人,m个关系, 让你对这个n个人进行分组,要求:尽可能的分组最少,然后每个组里面的人都没有关系 ...
- BZOJ5450: 轰炸(水题,Tarjan缩点求最长路)
5450: 轰炸 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 43 Solved:18[Submit][Status][Discuss] Desc ...
- 缩点+出入度 poj1236
题目链接:https://vjudge.net/contest/219056#problem/H 题意:先输入n,代表接下来有n个点,接下来n行,第i行里面的数(假设是)a,b...0(到0表示结束) ...
- [BZOJ 2438] [中山市选2011]杀人游戏 Tarjan缩点
这个题很容易想到正解就是缩点找入度为零的点,那么我们考虑一种特殊情况就是,一个入度为零的点我们不访问他就知道他是不是凶手,那么这样的话就是:I. 他是一个真·孤立的点 II. 他在图里但是在他的强联通 ...
随机推荐
- PAT 1021 个位数统计 (15)(C++&Java&Python)
1021 个位数统计 (15)(15 分) 给定一个k位整数N = d~k-1~*10^k-1^ + ... + d~1~*10^1^ + d~0~ (0<=d~i~<=9, i=0,.. ...
- elasticsearch权威指南
elasticsearch权威指南 https://elasticsearch.cn/book/elasticsearch_definitive_guide_2.x/
- java1.8 版本改成 java1.7版本
以前先安装的java1.7 大部分程序应该都是只支持1.7 不支持1.8 但是因为要跑一个别人的项目 要求是java1.8 所以想在电脑上同时装1.7和1.8 到官网上下载1.8 安装 安装完成后 并 ...
- Luogu 2216[HAOI2007]理想的正方形 - 单调队列
Solution 二维单调队列, 这个数组套起来看得我眼瞎... Code #include<cstdio> #include<algorithm> #include<c ...
- qt 5.2.1类和模块的关系图
QT│ ├─ActiveQt│ │ ActiveQt│ │ ActiveQtDepends│ │ ActiveQtVersion│ │ QAxAggregated│ │ QAxB ...
- Virtual Machine Kernel Panic : Not Syncing : VFS : Unable To Mount Root FS On Unknown-Block (0,0)
Virtual Machine Kernel Panic : Not Syncing : VFS : Unable To Mount Root FS On Unknown-Block (0,0) 33 ...
- Laravel Many to Many Polymorphic Relationship
Many to many Polymorphic relationship is also a little bit complicated to understand. For example, i ...
- [Jmeter] Jmeter Plugins
Plugins: Plugins Manager: https://jmeter-plugins.org/wiki/PluginsManager/ Custom Thread Groups: http ...
- activemq , redis
activemq是干什么的?即时消息通信,简单说: A发送消息给activemq 服务,B监听服务获取消息.假如有如下场景: A发送了一个请求,但是这个请求需要做 10 项工作,如果按照正常操作,需要 ...
- 三大框架中各种xml的存放位置
web.xml中classpath:和classpath*: 有什么区别? classpath:只会到你的class路径中查找找文件; classpath*:不仅包含class路径,还包括jar ...