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. 他在图里但是在他的强联通 ...
随机推荐
- C++中stl的map
总结: 在map中插入数据有三种方法: 1.用insert插入pair数据: mapstudent.insert(pair<int,string>(1,"studentone&q ...
- hdu 1735(贪心) 统计字数
戳我穿越:http://acm.hdu.edu.cn/showproblem.php?pid=1735 对于贪心,二分,枚举等基础一定要掌握的很牢,要一步一个脚印走踏实 这是道贪心的题目,要有贪心的意 ...
- 关于RNA-Seq数据去接头(Adapter)这事需要讲一讲
关于RNA-Seq数据去接头(Adapter)这事需要讲一讲 RNA-Seq adapter barcode cutadapt 首先来了解一下三个概念: 1.adapter是一段短的序列已知的核酸链, ...
- CH6202 黑暗城堡
一道最短路+生成树 原题链接 实际上就是生成树的中每个点到节点\(1\)的距离等于原图中这个点到节点\(1\)的最短距离,求这样的生成树的棵数. 先用\(SPFA\)或\(Dijkstra\)求出所有 ...
- 关于python的字符编码
理论特别多,金角大王讲的非常细致和深入浅出. 我来个简短的总结: python2的编码:默认是ascii,可以改变成gbk,utf-8等,但是用什么编码写的,就存储成什么编码.如果搬到linux,默认 ...
- EXISTS 和 IN 的区别
exists子句的用法 select * from 表1 where exists (select * from 表2 where 表1.列名=表2.列名); exists子句返回的结果并不是从数据库 ...
- jmeter对需要登录的接口进行性能测测试
只需要一步: https://www.testwo.com/blog/7253
- BP神经网络在python下的自主搭建梳理
本实验使用mnist数据集完成手写数字识别的测试.识别正确率认为是95% 完整代码如下: #!/usr/bin/env python # coding: utf-8 # In[1]: import n ...
- Fiddler建好代理后,能连到手机,但手机不能上网了是什么原因
依次 tools(工具) >> fiddler options(fiddler选项) >> connections( 连接) >>allow remot ...
- 2019.02.09 bzoj2560: 串珠子(状压dp+简单容斥)
传送门 题意简述:nnn个点的带边权无向图,定义一个图的权值是所有边的积,问所有nnn个点都连通的子图的权值之和. 思路: fif_ifi表示保证集合iii中所有点都连通其余点随意的方案数. gig ...