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