[HDU3038]How Many Answers Are Wrong(并查集)
2024-10-01 03:21:47
和某题类似,只不过奇偶换成了和。
——代码
#include <cstdio>
#include <iostream>
#define N 1000001 int n, m, ans;
int f[N], d[N]; inline int read()
{
int x = , f = ;
char ch = getchar();
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -;
for(; isdigit(ch); ch = getchar()) x = (x << ) + (x << ) + ch - '';
return x * f;
} inline int find(int x)
{
if(x ^ f[x])
{
int fx = f[x];
f[x] = find(f[x]);
d[x] += d[fx];
}
return f[x];
} int main()
{
int i, x, y, z, fx, fy;
while(~scanf("%d %d", &n, &m))
{
ans = ;
for(i = ; i <= n; i++) f[i] = i, d[i] = ;
for(i = ; i <= m; i++)
{
x = read() - ;
y = read();
z = read();
fx = find(x);
fy = find(y);
if(fx == fy)
{
if((d[x] - d[y]) ^ z) ans++;
}
else
{
d[fx] = d[y] - d[x] + z;
f[fx] = fy;
}
}
printf("%d\n", ans);
}
return ;
}
最新文章
- 如何向新手程序员介绍Java编程
- SDRAM读写一字(下)
- Support for Xpm library: no问题
- time元素
- 谷歌验证 (Google Authenticator) 的实现原理是什么?
- hdu1171
- PHP面试题之文件目录操作
- bootstrap注意事项(六)按钮
- 杭电oj A + B Again
- Redis14--jedis实现主从模式。
- 点评阿里JAVA手册之MySQL数据库 (建表规约、索引规约、SQL语句、ORM映射)
- 可选参数、命名参数、.NET的特殊类型、特性
- 100 - k8s源码分析-准备工作
- mac下chrome 长截图(不使用插件)
- Php中的goto用法
- vb.net
- (最详细)小米Note 2的usb调试模式在哪里开启的教程
- docker下运行labview2010
- C#中关于as关键字的使用
- mysql不存在插入否则更新