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

其实也可以不使用加权并查集,通过画图可以发现,一个长区间和其包含的区间能够算出一个新区间(即长区间剩余部分),只要这个区间不与已存在的区间冲突,那么这个区间就是正确的,同时将这个新生成的区间记录下来即可,设其为正确区间,从而保证新算出的区间不产生冲突。

#include <queue>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <vector>
#define ll long long
#define inf 0x3f3f3f3f
#define mod 1000000007
using namespace std;
int read()
{
int x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9')
{
if(ch=='-')f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9')
{
x=x*10+ch-'0';
ch=getchar();
}
return x*f;
}
const int N=105;
int a[N][N];
struct node
{
int l,r,c,len;
bool operator < (const node &an) const{
return len<an.len;
}
}b[N];
int main()
{
int T=read();
while(T--){
int n=read(),m=read();
int u,v,c,flag=0;
memset(a,0x3f,sizeof(a));
for(int i=0;i<m;i++){
u=read();v=read();c=read();
if(a[u][v]!=inf) flag=1;
a[u][v]=c;
b[i].l=u;b[i].r=v;
b[i].c=c;b[i].len=v-u;
}
sort(b,b+m);
for(int i=1;i<m;i++){
for(int j=0;j<i;j++){
if(b[i].l==b[j].l&&b[i].r>b[j].r){
u=b[j].r+1;v=b[i].r;
c=b[i].c-b[j].c;
if(v<u) continue;
if(a[u][v]!=inf&&a[u][v]!=c){
flag=1;
break;
}
else a[u][v]=c;
}
if(b[i].r==b[j].r&&b[i].l<b[j].l){
u=b[i].l;v=b[j].l-1;
c=b[i].c-b[j].c;
if(v<u) continue;
if(a[u][v]!=inf&&a[u][v]!=c){
flag=1;
break;
}
else a[u][v]=c;
}
}
if(flag) break;
}
if(flag) puts("false");
else puts("true");
}
return 0;
}

最新文章

  1. JavaScript 基础第十天
  2. [cocos2d-js]cc.RenderTexture几种用法(数字图片、刮刮乐效果)
  3. [CSS] Introduction to CSS Columns
  4. Spark编程实现SQL查询的实例
  5. PostgreSQL的 initdb 源代码分析之十八
  6. Ubuntu下安装Apache2, php5 mysql
  7. (转+原)android获取系统时间
  8. Python处理海量手机号码
  9. jquery 判断当前上传文件大小限制上传格式 搭配thinkphp实现上传即预览(模拟异步上传)
  10. Scala是一门现代的多范式编程语言
  11. H5微场景宽、高度自适应办法
  12. 2018-2019-2 20175235 实验二《Java面向对象程序设计》实验报告
  13. MyBatis 源码分析 - 插件机制
  14. CentOS 7通过RVM来安装指定版本的Ruby
  15. OpenCV学习参考 即时贴
  16. Unity2017烘焙参数设置
  17. Html和websocket初识
  18. 【Python】POST上传APK检测是否存在ZipperDown漏洞
  19. github上关于campbell数据采集的一些代码。
  20. QSettings配置读写-win注册表操作-ini文件读写

热门文章

  1. Apple Tree POJ - 2486
  2. HttpURLConnection与HTTP Client的区别,及多用前者
  3. post和get提交服务器编码过程
  4. iOS生成PDF的关键代码-备忘
  5. Kali linux 2016.2(Rolling) 的详细安装(图文教程)附安装VMare Tools 增强工具
  6. ssm(Spring、Springmvc、Mybatis)实战之淘淘商城-第五天(非原创)
  7. hihocoder offer收割编程练习赛11 B 物品价值
  8. P1372 又是毕业季I
  9. C# 方法 虚方法的调用浅谈 引用kdalan的博文
  10. IE和DOM事件流、普通事件和绑定事件的区别