codeforces 107A(dfs)
题意描述
The German University in Cairo (GUC) dorm houses are numbered from 1 to n. Underground water pipes connect these houses together. Each pipe has certain direction (water can flow only in this direction and not vice versa), and diameter (which characterizes the maximal amount of water it can handle).
For each house, there is at most one pipe going into it and at most one pipe going out of it. With the new semester starting, GUC student and dorm resident, Lulu, wants to install tanks and taps at the dorms. For every house with an outgoing water pipe and without an incoming water pipe, Lulu should install a water tank at that house. For every house with an incoming water pipe and without an outgoing water pipe, Lulu should install a water tap at that house. Each tank house will convey water to all houses that have a sequence of pipes from the tank to it. Accordingly, each tap house will receive water originating from some tank house.
In order to avoid pipes from bursting one week later (like what happened last semester), Lulu also has to consider the diameter of the pipes. The amount of water each tank conveys should not exceed the diameter of the pipes connecting a tank to its corresponding tap. Lulu wants to find the maximal amount of water that can be safely conveyed from each tank to its corresponding tap.
有m条边,n个点,每个点最多有一个入水口和出水口,要求找到所有的水塔和最小的直径值
思路
如果一个点的入度为0并且出度不为0,就从这个结点开始dfs,dfs过程中记录最小的结点值。如果dfs到了一个出度为0的点,就跳出循环输出。
AC代码
#include<bits/stdc++.h>
#define x first
#define y second
#define IOS ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
typedef pair<long,long> PLL;
typedef pair<char,char> PCC;
typedef long long LL;
const int N=3*1010;
const int M=150;
const int INF=0x3f3f3f3f;
const int MOD=998244353;
int n,m;
int g[N][N],in[N],out[N];
void solve(){
cin>>n>>m;
for(int i=0;i<m;i++){
int x,y,z;cin>>x>>y>>z;
g[x][y]=z;
in[y]=x;
out[x]=y;
}
int cnt=0;
for(int i=1;i<=n;i++){
if(in[i]==0){
if(out[i]!=0){
cnt++;
}
}
}
cout<<cnt<<endl;
for(int i=1;i<=n;i++){
if(in[i]==0){
if(out[i]!=0){
int res=INF;
int u=i;
while(1){
int v=out[u];
if(!v) break;
res=min(res,g[u][v]);
u=v;
}
cout<<i<<' '<<u<<' '<<res<<endl;
}
}
}
}
int main(){
IOS;
solve();
return 0;
}
- 使用Linux命令发送邮件(r2笔记94天)
- 如何用python轻松破解wifi密码( 源码 )
- 海量数据迁移之通过shell估算数据量 (r2笔记93天)
- 使用sklearn进行数据挖掘
- 批量转换分区表为普通表(r2笔记92天)
- 使用sklearn做特征工程
- 【专业技术】CSS作用及用法
- 生产环境sql语句调优实战第六篇(r2笔记91天)
- 关于分区表的move操作(r2笔记90天)
- 简单分析oracle的数据存储(r2笔记89天)
- 机器学习线性分类算法:感知器原理
- 通过shell脚本来查看Undo中资源消耗高的sql(r2笔记88天)
- 关于分页查询的优化思路(r3笔记第7天)
- 用机器学习方法对影评与观影者情感判定
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- Python 实现随机森林预测宽带客户离网(附源数据与代码)
- 嘿,你知道新冠疫情现在什么样了吗?
- 关于双目立体视觉的三大基本算法及发展现状的总结
- 权限维持之打造不一样的映像劫持后门
- SM3加密算法开源项目推荐
- 图片压缩原理
- 使用 Vue 3.0,你可能不再需要Vuex了
- ffmpeg视频处理神器学习基础笔记
- 5分钟就能做一个Excel动态图表,你确定不学学?(纯gif教学)
- 使用Logstash接收Netflow日志并发送到syslog服务器
- Flask搭建ES搜索引擎(二)
- Java 通过RestHighLevelClient 使用ES的date_histogram 根据年月日做统计
- Debug HashMap
- NSum及股票系列
- 从0开始搭建编程框架——主框架和源码