题目链接

题解

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

考虑对约束 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. Linux 系统安全加固经验总结

    本文为博主原创,转载请注明出处: 目录  1. 禁止root密码登录 2. linux 用户密钥复杂度及有效期设置 3. 检查sudo权限 4.关闭ftp 5.设置文件的属主并指定读写执行权限 6.管 ...

  2. Redis 哨兵模式高可用

    本文为博主原创,未经允许不得转载: 目录: 1. 哨兵 Sentinel 介绍 2. 哨兵架构特点及工作原理 3. redis哨兵架构搭建步骤 4. 哨兵数据丢失 5. spring boot 整合  ...

  3. Feign 实现微服务调用及进行服务熔断与降级

    本文为博主原创,未经允许不得转载: 1. Feign 日志级别配置 2. Feign client 封装调用 3. Feign 定义熔断降级方法 4. 通过 FallbackFactory 工厂 实现 ...

  4. centos7_Lnmp编译安装

    17年面试运维岗位的时候,面试官要求输出一份lnmp编译的操作文档,于是有了如下安装nginx+php+mysql,进入正题: 准备环境 环境:centos7.3 软件:nginx-1.12.1 + ...

  5. SV 设计特性

    过程语句块特性 ABC 过程块语句 always_comb 防止多驱动的问题:赋值块左侧的语句无法被另一个过程块赋值 if语句没有写else,sv会提示警告,sv认为是latch always不会再仿 ...

  6. SpringMVC01——回顾MVC

    1.1什么是MVC MVC是模型(Model).视图(View).控制器(Controller)的简写,是一种软件设计规范. 是将业务逻辑.数据.显示分离的方法来组织代码. MVC主要作用是降低了视图 ...

  7. [转帖]Sqlserver数据库中char、varchar、nchar、nvarchar的区别及查询表结构

    https://www.cnblogs.com/liuqifeng/p/10405121.html varchar 和 nvarchar区别: varchar(n)长度为 n 个字节的可变长度且非 U ...

  8. [转帖]查看mysql分区名和各分区数据量

    – 查看mysql分区名和各分区数据量 SELECT table_name, partition_name, table_rows FROM information_schema.PARTITIONS ...

  9. [转帖]TiDB Lightning 监控告警

    https://docs.pingcap.com/zh/tidb/v6.5/monitor-tidb-lightning tidb-lightning 支持使用 Prometheus 采集监控指标 ( ...

  10. 【转帖】Alpaca 7B:斯坦福从LLaMA-7B微调的语言模型

    https://www.jianshu.com/p/f8f8f660d2c3 https://crfm.stanford.edu/2023/03/13/alpaca.html https://crfm ...