CF1850H The Third Letter
题解
知识点:贪心,图论建模。
考虑对约束 a b d 建边 \(a \mathop{\to}\limits^d b\) 与 \(b \mathop{\to}\limits^{-d} a\) ,这里也可以建单向边,但需要缩点,会麻烦很多。
若约束是合法的,那么遍历整张图得到的点权是不会矛盾的。因此,我们在一个连通块内任取一个点作为 \(0\) 并开始遍历整张图,访问过的点直接根据边权赋值,并标记下次不需要访问。最后,只需要检查所有约束是否得到满足即可。
时间复杂度 \(O(n)\)
空间复杂度 \(O(n)\)
代码
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
vector<pair<int, int>> g[200007];
tuple<int, int, int> c[200007];
bool vis[200007];
ll val[200007];
bool dfs(int u) {
for (auto [v, w] : g[u]) {
if (vis[v]) continue;
vis[v] = 1;
val[v] = val[u] + w;
dfs(v);
}
return true;
}
bool solve() {
int n, m;
cin >> n >> m;
for (int i = 1;i <= n;i++) vis[i] = val[i] = 0, g[i].clear();
for (int i = 1;i <= m;i++) {
int u, v, d;
cin >> u >> v >> d;
g[u].push_back({ v,d });
g[v].push_back({ u,-d });
c[i] = { u,v,d };
}
for (int i = 1;i <= n;i++) {
if (vis[i]) continue;
dfs(i);
}
for (int i = 1;i <= m;i++) {
auto [u, v, d] = c[i];
if (val[v] != val[u] + d) return false;
}
cout << "YES" << '\n';
return true;
}
int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t = 1;
cin >> t;
while (t--) {
if (!solve()) cout << "NO" << '\n';
}
return 0;
}
CF1850H The Third Letter的更多相关文章
- [LeetCode] Letter Combinations of a Phone Number 电话号码的字母组合
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- 17. Letter Combinations of a Phone Number
题目: Given a digit string, return all possible letter combinations that the number could represent. A ...
- 什么是Unicode letter
起因:从一段代码说起 using System; using System.Collections.Generic; using System.Linq; using System.Text; usi ...
- LeetCode——Letter Combinations of a Phone Number
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- No.017:Letter Combinations of a Phone Number
问题: Given a digit string, return all possible letter combinations that the number could represent.A ...
- SCI/EI期刊投稿 Reply Letter 常用格式总结
SCI/EI期刊投稿Reply Letter常用格式总结 整个论文投稿的过程中,会遇到各种问题,需要我们向主编询问或是回复.下面主要总结了responses to the comme ...
- 【leetcode】 Letter Combinations of a Phone Number(middle)
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- [LeetCode] Letter Combinations of a Phone Number
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- [LintCode] Letter Combinations of a Phone Number 电话号码的字母组合
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- 69. Letter Combinations of a Phone Number
Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations ...
随机推荐
- Laravel - Eloquent 模型查询
Laravel 的 Eloquent ORM 提供了漂亮.简洁的 ActiveRecord 实现来和数据库进行交互.每个数据库表都有一个对应的「模型」可用来跟数据表进行交互.你可以通过模型查找数据表内 ...
- [转帖]KV数据库调研
https://zhuanlan.zhihu.com/p/499313638 Redis作为NoSQL领域的代表,拥有很高的读写性能,支持比较丰富的数据类型,但是Redis也存在一些缺陷. l 内存数 ...
- 【转帖】查看mysql库大小,表大小,索引大小
https://www.cnblogs.com/lukcyjane/p/3849354.html 说明: 通过MySQL的 information_schema 数据库,可查询数据库中每个表占用的空间 ...
- Python学习之十九_程序运行时间的验证
Python学习之十九_程序运行时间的验证 背景 最近一段时间比较忙. 而且还遇到了一个lua脚本优化redis访问的场景. 想着自己还在学习python(时断时续) 所以想借着这个场景,学习一下py ...
- SUBMIT指定用户名错误
1.SUBMIT说明 在ABAP中,SUBMIT关键字用于运行另一个ABAP程序.通过SUBMIT关键字,可以在当前程序内部调用其他程序,而无需关闭当前程序. SUBMIT语句的一般语法如下: &qu ...
- BAdI:INVOICE_UPDATE 导致MM Invoice Doc. Missing
Symptom:发票校验过程中,对应发票号生成,FI凭证也产生,但是对应RBKP,RSEG中无相应的发票. 原先这一问题SAP早给出过解释,参照note:1876234 Cause:在SD MM模块中 ...
- 人均瑞数系列,瑞数 5 代 JS 逆向分析
声明 本文章中所有内容仅供学习交流使用,不用于其他任何目的,不提供完整代码,抓包内容.敏感网址.数据接口等均已做脱敏处理,严禁用于商业用途和非法用途,否则由此产生的一切后果均与作者无关! 本文章未经许 ...
- python3 牛客网:OJ在线编程常见输入输出练习(ACM模式)
牛客网: 校招笔试真题_C++工程师.golang工程师_牛客网 其他语言输入输出见链接 1.输入两个数,输入数据包括多组. while True: try: a = list(map(int,inp ...
- 【三】AI Studio 项目详解——单机多机训练分布式训练--PARL
相关文章 [一]-环境配置+python入门教学 [二]-Parl基础命令 [三]-Notebook.&pdb.ipdb 调试 [四]-强化学习入门简介 [五]-Sarsa&Qlear ...
- 5.9 Windows驱动开发:内核InlineHook挂钩技术
在上一章<内核LDE64引擎计算汇编长度>中,LyShark教大家如何通过LDE64引擎实现计算反汇编指令长度,本章将在此基础之上实现内联函数挂钩,内核中的InlineHook函数挂钩其实 ...