题目链接

题解

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

考虑对约束 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. 每天学五分钟 Liunx 0100 | 服务篇:进程状态

    多任务和 CPU 时间片 前面说了 Liunx 是多用户多任务的,所谓的多任务就是多个进程"同时"执行.比如,同时开多个软件(进程),对于用户来说好像每个软件(进程)都在工作,但是 ...

  2. [java] - 获取上传到服务器上的文件路径

    request.getSession().getServletContext().getRealPath("upload/" );

  3. [转帖]日常Bug排查-读从库没有原子性?

    https://zhuanlan.zhihu.com/p/658508920 3 人赞同了该文章 日常Bug排查系列都是一些简单Bug排查.问题虽小,但经常遇到,了解这些问题,会让我们少走点弯路,提升 ...

  4. [转帖]datax安装+配置+使用文档

    1 DataX离线同步工具DataX3.0介绍 DataX 是阿里巴巴集团内被广泛使用的离线数据同步工具/平台,实现包括 MySQL.Oracle.SqlServer.Postgre.HDFS.Hiv ...

  5. [转帖] 如何kill一条TCP连接?

    https://www.cnblogs.com/codelogs/p/16838850.html 原创:扣钉日记(微信公众号ID:codelogs),欢迎分享,转载请保留出处. 简介# 如果你的程序写 ...

  6. Docker镜像精简方法之二 COPY vs ADD 与镜像层

    Docker镜像精简方法之二 COPY vs ADD 与镜像层 摘要 昨天只是讲了一下大体的思路. 但是没有实操. 今天想着修改一下默认的打包镜像的命令,验证一下效果 原始命令 FROM adopto ...

  7. [转帖]OutOfMemoryError内存溢出相关的JVM参数

    原文在这里: OutOfMemoryError内存溢出相关的JVM参数 JVM提供了很多处理内存溢出的相关参数,本文主要来讲解下这些参数,当你遇到内存溢出的时候可能会对你非常有帮助,这些参数主要有: ...

  8. 关于SSL证书的学习与总结

    关于证书 证书是用来实现https通信加密的基础, 有证书才能够进行相关的TLS层的加密处理. 本文简要讲解一下证书的申请,创建以及使用等. 第一部分: PKI 公共密钥基础 其实有很多家企业在做PK ...

  9. 一文搞懂Go GC演进史,讲的太细致了!

    最近在和 Go就业训练营 的朋友讨论Go GC的问题,发现了刘丹冰老师总结的内容,写的太好了,和大家分享一下. 我们的讨论和思考也整理到这篇文章中了,希望对你有启发. 垃圾回收(Garbage Col ...

  10. typeof的用法和注意点

    基本数据类型和查看数据类型 1==>js有六种基本数据类型. String Boolean Number null underfined Symbol [6种] 但是<你不知道的javas ...