【题目链接】 http://www.lydsy.com/JudgeOnline/problem.php?id=1202

【题目大意】

  给出一些区间和的数值,问是否存在矛盾

【题解】

  用并查集维护前缀和之间的距离,每个节点保存到根节点的数值差,
  如果保存的数值差的差与前缀和之差不相等,则矛盾

【代码】

#include <cstdio>
#include <algorithm>
using namespace std;
const int N=110;
int n,m,f[N],d[N];
int sf(int x){
if(f[x]==x)return x;
int fx=f[x];
f[x]=sf(f[x]);
d[x]+=d[fx];
return f[x];
}
const int M=1010;
int T;
int st[M],en[M],cost[M];
bool check(){
for(int i=1;i<=m;i++){
int x=st[i]-1,y=en[i];
int fx=sf(x),fy=sf(y);
if(fx!=fy){
f[fx]=fy;
d[fx]=cost[i]-d[x]+d[y];
}else{
//printf("%d %d %d\n",d[y],d[x],cost[i]);
if(d[x]-d[y]!=cost[i])return 0;
}
}return 1;
}
int main(){
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
for(int i=0;i<=n;i++)f[i]=i,d[i]=0;
for(int i=1;i<=m;i++)scanf("%d%d%d",&st[i],&en[i],&cost[i]);
if(check())puts("true");
else puts("false");
}return 0;
}

最新文章

  1. jquery.ajax
  2. java读取项目中文件路径及乱码解决
  3. jQuery ajax的traditional参数的作用///////////////////////////////////zzzzzzzzzzz
  4. canvas关于getImageData跨域问题解决方法
  5. Linux JDK 安装及卸载 http://www.cnblogs.com/benio/archive/2010/09/14/1825909.html
  6. 前端开发与Seo基础
  7. Lua中cJson的读写
  8. oracle错误之 ora-01017
  9. Intent的概念及应用(一)
  10. 【codeforces 516B】Drazil and Tiles
  11. 我的Python笔记04
  12. 2、java基础
  13. 「造个轮子」——cicada(轻量级 WEB 框架)
  14. arcgis api 3.x for js 入门开发系列五地图态势标绘(附源码下载)
  15. 安卓APP环境搭建
  16. Spring cron表达式详解
  17. github .net core
  18. 【Hadoop】搭建完全分布式的hadoop【转】
  19. Windows:chm 文件打开出现“已取消到该网页的导航”的解决方案
  20. JZ2440 裸机驱动 第12章 I2C接口

热门文章

  1. 爬虫--requests讲解
  2. bzoj 1050 并查集
  3. 网页实现插入图片—css与html的区别
  4. recycleView实现item点击更改该item颜色,其它item颜色变回
  5. Java垃圾收集算法
  6. Ubuntu 14.04 ThinkPad E431无线网卡驱动安装
  7. devm_xxx机制
  8. 微信支付:curl 出错,错误码: 60
  9. python基础===jieba模块,Python 中文分词组件
  10. 64_g1