【BZOJ】1202: [HNOI2005]狡猾的商人(并查集+前缀和)
http://www.lydsy.com/JudgeOnline/problem.php?id=1202

用并查集+前缀和。
前缀和从后向前维护和,并查集从前往后合并
对于询问l, r
如果l-1和r是一个集合(在这里,并查集每个集合都是一个可行的区间),那么直接判断s[l-1]-s[r]是否等于所给值
否则要合并l-1和r的集合(画图可知,p[fx]=fy时,s[fx]=s[y]-s[x]+所给值,这是由并查集的实现决定的)
而在并查集里维护前缀和就是用s[x]+=s[fa],而在并查集中,x会被合并到一个新的集合的根中,所以不会重复累计x的fa。
#include <cstdio>
#include <cstring>
#include <string>
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
#define for1(i,a,n) for(i=a;i<=n;++i)
#define for2(i,a,n) for(i=a;i<n;++i)
#define for3(i,a,n) for(i=a;i>=n;--i)
#define for4(i,a,n) for(i=a;i>n;--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define read(a) a=getnum()
#define print(a) printf("%d", a)
inline int getnum() { int ret=0; char c; int k=1; for(c=getchar(); c<'0' || c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0' && c<='9'; c=getchar()) ret=ret*10+c-'0'; return k*ret; } const int N=1005;
int s[N], p[N]; int ifind(const int &x) {
if(x==p[x]) return x;
int fa=p[x]; p[x]=ifind(p[x]);
s[x]+=s[fa];
return p[x];
} int main() {
int n, m, w, i, t, x, y, fx, fy, ok;
read(w);
while(w--) {
read(n); read(m);
ok=1;
CC(s, 0);
for1(i, 0, n+2) p[i]=i;
for1(i, 1, m) {
read(x); read(y); read(t); --x;
fx=ifind(x); fy=ifind(y);
if(fx!=fy) {
p[fx]=fy;
s[fx]=s[y]-s[x]+t;
}
else if(s[x]-s[y]!=t) { ok=0; break; }
}
ok?puts("true"):puts("false");
}
return 0;
}
Description
Input
Output
Sample Input
3 3
1 2 10
1 3 -5
3 3 -15
5 3
1 5 100
3 5 50
1 2 51
Sample Output
false
HINT
Source
【BZOJ】1202: [HNOI2005]狡猾的商人(并查集+前缀和)的更多相关文章
- bzoj 1202: [HNOI2005]狡猾的商人 并查集好题
1202: [HNOI2005]狡猾的商人 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2946 Solved: 1384[Submit][Sta ...
- BZOJ 1202: [HNOI2005]狡猾的商人( 差分约束 )
好像很多人用并查集写的... 前缀和, 则 sumt - sums-1 = v, 拆成2条 : sumt ≤ sums-1 + v, sums-1 ≤ sumt - v 就是一个差分约束, 建图跑SP ...
- bzoj1202: [HNOI2005]狡猾的商人(并查集 差分约束)
1202: [HNOI2005]狡猾的商人 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 4127 Solved: 1981[Submit][Sta ...
- BZOJ1202 [HNOI2005]狡猾的商人 并查集维护前缀和
1202: [HNOI2005]狡猾的商人 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1935 Solved: 936[Submit][Stat ...
- BZOJ-1202 狡猾的商人 并查集+前缀和
我记得这个题,上次之前做的时候没改完,撂下了,今天突然想改发现,woc肿么A 了= =看来是我记错了.. 1202: [HNOI2005]狡猾的商人 Time Limit: 10 Sec Memory ...
- BZOJ 1202 [HNOI2005]狡猾的商人(并查集)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1202 [题目大意] 给出一些区间和的数值,问是否存在矛盾 [题解] 用并查集维护前缀和 ...
- BZOJ 1202 [HNOI2005]狡猾的商人:并查集(维护距离)
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1202 题意: 有一个账本,记录了n个月的盈亏. 每个月的数值:正为盈,负为亏. 你知道m个 ...
- bzoj 1202 [HNOI2005]狡猾的商人——带偏移量的并查集
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1202 带偏移量的并查集. 注意先 find() 再调用 dis !!! 自己的对拍太水了. ...
- BZOJ 1202: [HNOI2005]狡猾的商人 [带权并查集]
题意: 给出m个区间和,询问是否有区间和和之前给出的矛盾 NOIp之前做过hdu3038..... 带权并查集维护到根的权值和,向左合并 #include <iostream> #incl ...
- BZOJ——1202: [HNOI2005]狡猾的商人
http://www.lydsy.com/JudgeOnline/problem.php?id=1202 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: ...
随机推荐
- HDOJ 1272 并查集
小希的迷宫 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- 【VirtualBox】VirtualBox的桥接网络模式,为啥网络不稳定?
网桥模式访问外网非常慢,经常卡死,ping时断时续 七搞八搞,反复重启了几次 TMD 就好了,也不知道什么情况,VirtualBox还是不太好使啊..... 网桥模式 设置 如下: 参考资料: ht ...
- css+div绝对定位
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" ...
- 【leetcode】Palindrome Partitioning
Palindrome Partitioning Given a string s, partition s such that every substring of the partition is ...
- 21.左旋转字符串[LeftRotateString]
[题目] 定义字符串的左旋转操作:把字符串前面的若干个字符移动到字符串的尾部.如把字符串AB1234 左旋转2位得到字符串1234AB.请实现字符串左旋转的函数.要求时间对长度为n的字符串操作的复杂度 ...
- 17.把字符串转换成整数[atoi]
[题目] 把字符串转换成整数,需要考虑字符串有效性. [代码] C++ Code 123456789101112131415161718192021222324252627282930313233 ...
- Java for LeetCode 032 Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- NanoApe Loves Sequence-待解决
NanoApe Loves Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/131072 K (Java ...
- pycharm上运行django服务器端、以及创建app方法
快来加入群[python爬虫交流群](群号570070796),发现精彩内容. 安装Django 下载Django包,解压缩. CMD 进入解压路径下. 执行:python setup.py in ...
- svn 提交冲突(目录下删除文件)
[root@v01 webtest]# svn ci -m "delete kkk" svn: Commit failed (details follow): svn: Abort ...