题目链接

题解

知识点:贪心,图论建模。

考虑对约束 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的更多相关文章

  1. [LeetCode] Letter Combinations of a Phone Number 电话号码的字母组合

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  2. 17. Letter Combinations of a Phone Number

    题目: Given a digit string, return all possible letter combinations that the number could represent. A ...

  3. 什么是Unicode letter

    起因:从一段代码说起 using System; using System.Collections.Generic; using System.Linq; using System.Text; usi ...

  4. LeetCode——Letter Combinations of a Phone Number

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  5. No.017:Letter Combinations of a Phone Number

    问题: Given a digit string, return all possible letter combinations that the number could represent.A ...

  6. SCI/EI期刊投稿 Reply Letter 常用格式总结

    SCI/EI期刊投稿Reply Letter常用格式总结          整个论文投稿的过程中,会遇到各种问题,需要我们向主编询问或是回复.下面主要总结了responses to the comme ...

  7. 【leetcode】 Letter Combinations of a Phone Number(middle)

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  8. [LeetCode] Letter Combinations of a Phone Number

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  9. [LintCode] Letter Combinations of a Phone Number 电话号码的字母组合

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  10. 69. Letter Combinations of a Phone Number

    Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations ...

随机推荐

  1. mybatis plus 中增删改查及Wrapper的使用

    本文为博主原创,未经允许不得转载: mybatis plus 通过封装  baseMapper 以及 ServiceImpl ,实现对数据库的增删改查操作,baseMapper 是我们通常所说的 da ...

  2. Angular系列教程之自定义指令

    .markdown-body { line-height: 1.75; font-weight: 400; font-size: 16px; overflow-x: hidden; color: rg ...

  3. C# WPF侧边栏导航菜单(Dropdown Menu)

    时间如流水,只能流去不流回! 点赞再看,养成习惯,这是您给我创作的动力! 本文 Dotnet9 https://dotnet9.com 已收录,站长乐于分享dotnet相关技术,比如Winform.W ...

  4. 有n个整数,使前面各数顺序向后m个位置,最后m个数变成最前面m个数,见图 8.43。写一函数实现以上功能在主函数中输入个整数和输出调整后的n个数。

    4,有n个整数,使前面各数顺序向后m个位置,最后m个数变成最前面m个数,见图 8.43.写一函数实现以上功能在主函数中输入个整数和输出调整后的n个数. 我的代码: 1.使用双向链表 void Move ...

  5. Oracle19c 多字符集支持的PDB

    Oracle19c 多字符集支持的PDB 背景 想在一个数据库里面支持多种字符集 突然发现Oracle12c开始已经可以实现一个CDB下面可以有多个不同字符集的PDB了 所以想着今天验证一下. 环境信 ...

  6. [转帖]Redis重大版本整理(Redis2.6-Redis7.0)

    Redis借鉴了Linux操做系统对于版本号的命名规则:node 版本号第二位若是是奇数,则为非稳定版本(例如2.7.2.9.3.1),若是是偶数,则为稳定版本(例如2.6.2.8.3.0.3.2). ...

  7. [转帖]Redis 内存淘汰策略 (史上最全)

    1.前言 Redis内存淘汰策略,是被很多小伙伴忽略的知识盲区,注意,是盲区. 注意,Redis如果内存淘汰策略配置不合理,可能会导致Redis无法服务. 所以,使用此文,对Redis内存淘汰策略专门 ...

  8. [转帖]MegaCli命令

    MegaCli命令 设置jbod模式 1.3 LSI 9260/9261 raid卡配置 LSI 9260/9261 raid卡支持0.1.10.5.6.50.60 常用命令: 清除raid卡原有的配 ...

  9. Nginx 解决 413 问题的配置.

    Nginx 解决 413 问题的配置. Nginx 容易出现一个错误提示问题: worker_processes 1; events { worker_connections 1024; } http ...

  10. git日志输出相关命令

    git log 默认输出所有的日志 git log 默认输出所有的日志 git 日志输出--只看最近的两条或者三条 有些时候我们可能只需要看最近的2或者3条日志 git log -2 日志输出--只看 ...