强大的dfs(用处1——拓扑排序【xdoj1025】,用处二——求强联通分量【ccf高速公路】)当然dfs用处多着咧
xdoj 1025

题目分析:看着好吓人啊。。其实分析清晰也很简单。每种物体既可以直接买到或者由其他物体制作得到。那么我们就取两者的最小值。制作的花费我们可以这样求得
如果x的原材料有y 那么x和y之间有一条有向边 。然后按照拓扑排序的顺序自底而上求出各个物体制作所需的价值。。很简单吗?!
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
const int N=1e4+;
vector < vector <int> > g(N);
int val[N];
bool vis[N];
int n,m,w;
void dfs (int rt) {
vis[rt]=;
if (g[rt].size()==) return ;// 最底层直接返回
int cost=w;
for (int i=;i<g[rt].size();i++) {
int next=g[rt][i];
if (!vis[next]) dfs(next);
cost+=val[next];//cost制作费用
}
val[rt]=min (cost,val[rt]);//取两者最小值
return ;
}
int main ()
{
int T;
scanf ("%d",&T);
while (T--) {
scanf ("%d %d %d",&n,&m,&w);
for (int i=;i<n;i++) g[i].clear();
memset (vis,,sizeof(vis));
for (int i=;i<n;i++) {
scanf ("%d",&val[i]);
int num; scanf ("%d",&num);
for (int j=;j<num;j++) {
int u; scanf ("%d",&u);
g[i].push_back(u);
}
}
for (int i=;i<n;i++)
if (!vis[i]) dfs (i);
int sum=;
for (int i=;i<m;i++) {
int x; scanf ("%d",&x);
sum+=val[x];
}
printf ("%d\n",sum);
}
return ;
}
ccf 高速公路 求联通分量 这个算法好像叫trajin 向这些科学家致敬
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <stack>
#include <vector>
using namespace std;
const int N=1e4+;
vector < vector <int> > g(N);
stack <int> ss;
bool instack[N];
bool vis[N];
int dfn[N],low[N];// dfn 保存遍历序号 low 这个点所能到达的最小点
int ans;
int n,m;
int cnt;//访问顺序
void dfs (int rt) {
vis[rt]=;
instack[rt]=; ss.push(rt);
dfn[rt]=low[rt]=++cnt;
for (int i=;i<g[rt].size();i++) {
int next=g[rt][i];
if (!vis[next]) {
dfs (next);
low[rt]=min (low[rt],low[next]);
}
else if (instack[next]) {
low[rt]=min (low[rt],low[next]);
}
}
if (dfn[rt]==low[rt]) {// dfn[rt]==low[rt] 表示遍历一个联通分变量啦
int num=;
int tmp=ss.top(); ss.pop(); instack[tmp]=;//在栈中 说明这个点的后继子孙还没有访问完
while (dfn[tmp]!=low[tmp]) { num++; tmp=ss.top(); ss.pop(); instack[tmp]=;}
ans+=(num-)*num/;
}
return ;
}
int main ()
{
ios::sync_with_stdio(false);
cin>>n>>m;
for (int i=;i<=m;i++) {
int x,y; cin>>x>>y;
g[x].push_back(y);
}
for (int i=;i<=n;i++)
if (!vis[i]) dfs (i);
printf ("%d\n",ans);
return ;
}
强大的dfs(用处1——拓扑排序【xdoj1025】,用处二——求强联通分量【ccf高速公路】)当然dfs用处多着咧的更多相关文章
- Vijos P1023Victoria的舞会3【贪心+DFS求强联通分量】
链接:Click Me! P1023Victoria的舞会3 Accepted 标签:Victoria的舞会[显示标签] 描写叙述 Victoria是一位颇有成就的艺术家,他因油画作品<我爱北京 ...
- CodeForces 1213F (强联通分量分解+拓扑排序)
传送门 •题意 给你两个数组 p,q ,分别存放 1~n 的某个全排列: 让你根据这两个数组构造一个字符串 S,要求: (1)$\forall i \in [1,n-1],S_{pi}\leq S _ ...
- 【DFS】【拓扑排序】【动态规划】Gym - 100642A - Babs' Box Boutique
给你10个箱子,有长宽高,每个箱子你可以决定哪个面朝上摆.把它们摞在一起,边必须平行,上面的不能突出来,问你最多摆几个箱子. 3^10枚举箱子用哪个面.然后按长为第一关键字,宽为第二关键字,从大到小排 ...
- hihoCoder #1185 : 连通性·三(强联通分量+拓扑排序)
#1185 : 连通性·三 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 暑假到了!!小Hi和小Ho为了体验生活,来到了住在大草原的约翰家.今天一大早,约翰因为有事要出 ...
- 拓扑排序详解(梅开二度之dfs版按字典序输出拓扑路径+dfs版输出全部拓扑路径
什么是拓扑排序? 先穿袜子再穿鞋,先当孙子再当爷.这就是拓扑排序! 拓扑排序说白了其实不太算是一种排序算法,但又像是一种排序(我是不是说了个废话qwq) 他其实是一个有向无环图(DAG, Direct ...
- uvaLA4255 Guess BFS+拓扑排序
算法指南白书 思路:“连续和转化成前缀和之差” #include <stdio.h> #include <string.h> #include <iostream> ...
- Poj 3683-Priest John's Busiest Day 2-sat,拓扑排序
Priest John's Busiest Day Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8872 Accept ...
- Legal or Not(拓扑排序判环)
http://acm.hdu.edu.cn/showproblem.php?pid=3342 Legal or Not Time Limit: 2000/1000 MS (Java/Others) ...
- CF798E. Mike and code of a permutation [拓扑排序 线段树]
CF798E. Mike and code of a permutation 题意: 排列p,编码了一个序列a.对于每个i,找到第一个\(p_j > p_i\)并且未被标记的j,标记这个j并\( ...
随机推荐
- Docker Container的概述
·通过Image创建(copy) ·在Image layer之上建立一个container layer(可读写) ·类比对象:类和实例(Image相当于抽象的一个类,Container相当于实例化的一 ...
- RabbitMQ入门_12_发布方确认
参考资料:https://www.rabbitmq.com/confirms.html 通过 ack 机制,我们可以确保队列中的消息一定能被消费到.那我们有办法保证消息发布方一定把消息发送到队列了吗? ...
- Java 常用对象-Scanner类
2017-11-02 16:33:11 Scanner类:一个可以使用正则表达式来解析基本类型和字符串的简单文本扫描器. Scanner 使用分隔符模式将其输入分解为标记,默认情况下该分隔符模式与空白 ...
- 谈谈http与https
今天简单的来说一下http和https, 简单来讲: HTTP 是 超文本协议,TCP 端口是 80 HTTPS 是一种配合了SSL协议的.加密的HTTP 协议 ,TCP端口是 443 HTTP ...
- UVA557 汉堡 Burger
题面 https://www.luogu.org/problemnew/show/UVA557 这里顺便整理一下二维格点随机游走问题. 遇到这种问题时,需注意分母的计算问题. 设x为起点到终点的距离. ...
- python-day18--匿名函数
一.lambda表达式 1.匿名函数的核心:一些简单的需要用函数去解决的问题,匿名函数的函数体只有一行 2.参数可以有多个,用逗号隔开 3.返回值和正常的函数一样可以是任意的数据类型 4.练习: 请把 ...
- h1042 N!大数乘int
计算10000以内某个数的阶乘,即大数乘以int,考虑到一个int存一个数位过于繁琐且浪费空间,采用万进制 一个int存四个位数,但注意除了最高位,其他位不够四位数时要加上前导0凑够四位: 例1234 ...
- OAF中下载附件之后页面失效,报过时的数据异常,浏览器后退异常
我在使用了下载功能之后,再往页面添加行或进行保存,页面老是报浏览器后退的异常. 猜测是因为我的下载按钮使用的submitButton,它隐式的包含了一个submit动作,且我在代码中有一个Commit ...
- [Leetcode] Unique binary search trees 唯一二叉搜索树
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- [LeetCode] 268. Missing Number ☆(丢失的数字)
转载:http://www.cnblogs.com/grandyang/p/4756677.html Given an array containing n distinct numbers take ...