题目链接

题解

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

考虑对约束 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. RL 基础 | 讲的很好的 TRPO 博客

    特意存档: 知乎 | 如何看懂TRPO里所有的数学推导细节? 感觉把 idea 讲的很清楚(虽然没有特别仔细看-

  2. Tomcat 与 JVM 中classpath的理解和设置总结

    本文为博主原创,转载请注明出处: 1.介绍 classpath是java运行时环境搜索类和其他资源文件(比如jar\zip等资源)的路径.类路径告诉JDK工具和应用程序在哪里可以找到第三方和用户定义的 ...

  3. 分享一个简单的使用js格式化json的代码

    今天给大家分享一段json格式化代码. 假设json字符串是: {"name":"刘德华","age":25.2,"birthda ...

  4. Error adding module to project: null I(IDEA创建maven项目时遇到此提示)

    1.问题 在已创建一个springboot项目的时候,想要再增添一个Maven项目,发生报错 2.解决 根本原因是创建maven模块时选择的sdk版本过高(选用了openjdk19).选用jdk1.8 ...

  5. APP Inventor的tcp连接扩展插件ClientSocketAl2Ext

    参考原文:https://www.cnblogs.com/bemfa/p/13390251.html 下载地址:https://wwtl.lanzoum.com/ik0Ky1clu41a B站关联学习 ...

  6. linux环境C语言实现:h264与pcm封装成mp4视频格式

    前言 H.264是压缩过的数据,PCM是原始数据,MP4是一种视频封装格式.实际H.264与PCM不能直接合成MP4格式,因为音频格式不对.这里需要中间对音频做一次压缩处理.基本流程为:将PCM音频数 ...

  7. [转帖]SQL Server 2008~2022版本序列号/密钥/激活码 汇总

    https://www.cnblogs.com/cqpanda/p/16148822.html SQL Server 2022# Enterprise: J4V48-P8MM4-9N3J9-HD97X ...

  8. [转帖]详解:Linux Chrony 设置服务器集群同步时间

    https://www.linuxprobe.com/centos7-chrony-time.html 导读 Chrony是一个开源的自由软件,像CentOS 7或基于RHEL 7操作系统,已经是默认 ...

  9. [转帖]vCenter使用 VMCA 续订证书:续订证书时发生意外错误

    https://www.dinghui.org/vcenter-sts-certificate.html 起因:有一处客户vCenter告警:STS签名证书即将过期. 处理办法:系统管理-证书-证书管 ...

  10. [转帖]Windows磁盘性能压测(1)-DiskSpd

    http://www.manongjc.com/detail/59-xrydhtisrajqsxn.html 本文章向大家介绍Windows磁盘性能压测(1)-DiskSpd,主要内容包括其使用实例. ...