带权并查集 HDU - 3047
题意: 一圈座位有n个,给出m组序号之间的关系,比如,1 2 150 代表2号坐在1号位置序号+150,看m组数据有多少组冲突的。
思路: 带权并查集模板。
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<queue>
#include<vector>
#include<stdlib.h>
using namespace std;
#define N 50010
int f[N],n,m,val[N],cnt;
int getf(int v)
{
if(f[v]==v)
return v;
int t=f[v];
f[v]=getf(f[v]);/*压缩路径*/
val[v]+=val[t];/*记录当前节点到祖先节点的权值和(在压缩路径的过程中,遍历到他的祖先,加上权值)*/
return f[v];
}
void marge(int u,int v,int s)
{
int t1=getf(u),t2=getf(v);
if(t1!=t2)
{
f[t2]=t1;/*搞清楚节点之间的父子关系!!!*/
val[t2]=val[u]-val[v]+s;/*向量*/
}
else if(val[v]!=val[u]+s)/*祖先相同,之间距离关系也应该正确,否则矛盾*/
cnt++;
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
cnt=0;
for(int i=1; i<=n; i++)
f[i]=i,val[i]=0;
int v,u,s;
for(int i=1; i<=m; i++)
{
scanf("%d%d%d",&u,&v,&s);
marge(u,v,s);
}
printf("%d\n",cnt);
}
return 0;
}
带权并查集 HDU - 3047的更多相关文章
- HDU 3047 Zjnu Stadium(带权并查集)
http://acm.hdu.edu.cn/showproblem.php?pid=3047 题意: 给出n个座位,有m次询问,每次a,b,d表示b要在a右边d个位置处,问有几个询问是错误的. 思路: ...
- 【带权并查集】HDU 3047 Zjnu Stadium
http://acm.hdu.edu.cn/showproblem.php?pid=3047 [题意] http://blog.csdn.net/hj1107402232/article/detail ...
- HDU 3047 带权并查集 入门题
Zjnu Stadium 题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=3047 Problem Description In 12th Zhejian ...
- hdu 5441 Travel 离线带权并查集
Travel Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5441 De ...
- HDU 3038 - How Many Answers Are Wrong - [经典带权并查集]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3038 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...
- Valentine's Day Round hdu 5176 The Experience of Love [好题 带权并查集 unsigned long long]
传送门 The Experience of Love Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Ja ...
- hdu 1829-A Bug's LIfe(简单带权并查集)
题意:Bug有两种性别,异性之间才交往, 让你根据数据判断是否存在同性恋,输入有 t 组数据,每组数据给出bug数量n, 和关系数m, 以下m行给出相交往的一对Bug编号 a, b.只需要判断有没有, ...
- HDU 5176 The Experience of Love 带权并查集
The Experience of Love Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/O ...
- HDU 1829 A Bug's Life 【带权并查集/补集法/向量法】
Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes ...
随机推荐
- Vmware Workstation 15 Pro安装Arch Linux并配置Docker
主机配置: CPU:Intel Core i7-7700HQ 2.8GHz 2.8GHz 内存:16GB 操作系统:Windows 10 Home 64bit 双显卡:Intel HD Graphic ...
- async/await实现图片的串行、并行加载
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- tomcat&servlet初记
tomcat&servlet初记 1. web相关概念 2. web服务器软件的学习:tomcat 3. servlet入门学习 web相关概念 1,软件架构 1,cs架构:客户端/服务器端 ...
- Qt_QChart的使用记录(小白)
主要是记录柱状图的数值显示,散点图的点坐标显示(防止后续忘记,把文件都贴出来,方便复查) 资源库: WarehouseInputOrOutput.pro QT += core gui QT += ch ...
- 广告行业中那些趣事系列6:BERT线上化ALBERT优化原理及项目实践(附github)
摘要:BERT因为效果好和适用范围广两大优点,所以在NLP领域具有里程碑意义.实际项目中主要使用BERT来做文本分类任务,其实就是给文本打标签.因为原生态BERT预训练模型动辄几百兆甚至上千兆的大小, ...
- 编写程序实现输入x,y,判断属于第几象限。
x = float(input("请输入横坐标:")) y = float(input("请输入纵坐标:")) if x > 0 and y > 0 ...
- day1 执行用例
故事1 今天的工作,是执行已写好的"回馈"模块用例 每个子模块的用例都有优先级之分 肯定要先进行通过性测试,验证正常流 因此,我优先执行各个子模块优先级最高的那条用例,进行通过性测 ...
- python的基本数据类型简介
python的基本数据类型有:数字-numbers.字符串-str.列表-list.元组-tuple.字典-dict.布尔-bool.集合-set 下面来个概览先大概了解一下,后面博文中咱再细说- 1 ...
- go-admin基于Gin + Vue + Element UI的前后端分离权限管理系统
✨ 特性 遵循 RESTful API 设计规范 基于 GIN WEB API 框架,提供了丰富的中间件支持(用户认证.跨域.访问日志.追踪ID等) 基于Casbin的 RBAC 访问控制模型 JWT ...
- 结巴分词demo
#encoding=utf-8 from __future__ import unicode_literals import sys sys.path.append("../") ...