【带权并查集】【HDOJ】
http://acm.hdu.edu.cn/showproblem.php?pid=3047
Zjnu Stadium
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4799 Accepted Submission(s): 1849
These days, Busoniya want to hold a large-scale theatrical performance in this stadium. There will be N people go there numbered 1--N. Busoniya has Reserved several seats. To make it funny, he makes M requests for these seats: A B X, which means people numbered B must seat clockwise X distance from people numbered A. For example: A is in column 4th and X is 2, then B must in column 6th (6=4+2).
Now your task is to judge weather the request is correct or not. The rule of your judgement is easy: when a new request has conflicts against the foregoing ones then we define it as incorrect, otherwise it is correct. Please find out all the incorrect requests and count them as R.
For every case:
The first line has two integer N(1<=N<=50,000), M(0<=M<=100,000),separated by a space.
Then M lines follow, each line has 3 integer A(1<=A<=N), B(1<=B<=N), X(0<=X<300) (A!=B), separated by a space.
Output R, represents the number of incorrect request.
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct pot{
int id;
int fa;
int len;
}POT[];
int find(int x)
{
if(POT[x].fa==x)return x;
int tt=POT[x].fa;
POT[x].fa=find(POT[x].fa);
POT[x].len+=POT[tt].len;
return POT[x].fa;
}
int cnt ;
void Union(int x,int y,int z)
{
int tx=find(x);
int ty=find(y);
if(tx==ty)
{
if(((POT[x].len)%)!=(POT[y].len+z)%)
cnt++;
}
else
{
if((POT[y].len+z-POT[x].len)>=)
{
POT[tx].fa=ty;
POT[tx].len=(POT[y].len+z-POT[x].len)%;
}
else
{
POT[ty].fa=tx;
POT[ty].len=(-(POT[y].len+z-POT[x].len))%;
}
}
}
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)==)
{
cnt = ;
int a,b,c;
for(int i = ; i <= n ;i++)
{
// POT[i].id=-1;
POT[i].fa=i;
POT[i].len=;
}
while(m--)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
Union(a,b,c); }
cout << cnt<<endl; }
return ;
}
http://acm.hdu.edu.cn/showproblem.php?pid=3635
Dragon Balls
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7617 Accepted Submission(s): 2820
His country has N cities and there are exactly N dragon balls in the world. At first, for the ith dragon ball, the sacred dragon will puts it in the ith city. Through long years, some cities' dragon ball(s) would be transported to other cities. To save physical strength WuKong plans to take Flying Nimbus Cloud, a magical flying cloud to gather dragon balls.
Every time WuKong will collect the information of one dragon ball, he will ask you the information of that ball. You must tell him which city the ball is located and how many dragon balls are there in that city, you also need to tell him how many times the ball has been transported so far.
For each case, the first line contains two integers: N and Q (2 < N <= 10000 , 2 < Q <= 10000).
Each of the following Q lines contains either a fact or a question as the follow format:
T A B : All the dragon balls which are in the same city with A have been transported to the city the Bth ball in. You can assume that the two cities are different.
Q A : WuKong want to know X (the id of the city Ath ball is in), Y (the count of balls in Xth city) and Z (the tranporting times of the Ath ball). (1 <= A, B <= N)
3 3 2
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int fa[],moves[],nums[];
int find(int x)
{
if(x==fa[x])return x;
int t=fa[x];
fa[x]=find(fa[x]);
moves[x]+=moves[t];
//int xx=x; // cout << x << fa[x]<<endl;
return fa[x];
}
void Union(int x,int y)
{
int tt=find(x);
int t2=find(y);
if(tt==t2)return;
nums[t2]+=nums[tt];
moves[tt]=;
fa[tt]=t2;
// cout << tt<<y<<endl;
}
int main()
{
int t;
scanf("%d",&t);
int case1=;
while(t--)
{
printf("Case %d:\n",case1++);
int n,q;
scanf("%d%d",&n,&q);
for(int i = ; i <= n ;i++)
{
fa[i]=i;
moves[i]=;
nums[i]=;
}
while(q--)
{
char ss[];
scanf("%s",ss);
if(ss[]=='T')
{
int a,b;
scanf("%d%d",&a,&b);
Union(a,b);
}
else
{
int c;
scanf("%d",&c);
int tt=find(c);
printf("%d %d %d\n",tt,nums[tt],moves[c]);
}
} }
return ;
}
小结:用结构体存与父节点的关系以及父节点序号,然后在路径压缩过程进行与祖先关系的转化即可
【带权并查集】【HDOJ】的更多相关文章
- POJ 1703 Find them, Catch them(带权并查集)
传送门 Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 42463 Accep ...
- [NOIP摸你赛]Hzwer的陨石(带权并查集)
题目描述: 经过不懈的努力,Hzwer召唤了很多陨石.已知Hzwer的地图上共有n个区域,且一开始的时候第i个陨石掉在了第i个区域.有电力喷射背包的ndsf很自豪,他认为搬陨石很容易,所以他将一些区域 ...
- poj1417 带权并查集 + 背包 + 记录路径
True Liars Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2713 Accepted: 868 Descrip ...
- poj1984 带权并查集(向量处理)
Navigation Nightmare Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 5939 Accepted: 2 ...
- 【BZOJ-4690】Never Wait For Weights 带权并查集
4690: Never Wait for Weights Time Limit: 15 Sec Memory Limit: 256 MBSubmit: 88 Solved: 41[Submit][ ...
- hdu3038(带权并查集)
题目链接: http://acm.split.hdu.edu.cn/showproblem.php?pid=3038 题意: n表示有一个长度为n的数组, 接下来有m行形如x, y, d的输入, 表示 ...
- 洛谷OJ P1196 银河英雄传说(带权并查集)
题目描述 公元五八○一年,地球居民迁移至金牛座α第二行星,在那里发表银河联邦 创立宣言,同年改元为宇宙历元年,并开始向银河系深处拓展. 宇宙历七九九年,银河系的两大军事集团在巴米利恩星域爆发战争.泰山 ...
- poj1984 带权并查集
题意:有多个点,在平面上位于坐标点上,给出一些关系,表示某个点在某个点的正东/西/南/北方向多少距离,然后给出一系列询问,表示在第几个关系给出后询问某两点的曼哈顿距离,或者未知则输出-1. 只要在元素 ...
- poj1611 带权并查集
题意:病毒蔓延,现在有 n 个人,其中 0 号被认为可能感染,然后给出多个社交圈,如果某个社交圈里有人被认为可能被感染,那么所有这个社交圈里的人都被认为可能被感染,现在问有多少人可能被感染. 带权并查 ...
- hdu 1829-A Bug's LIfe(简单带权并查集)
题意:Bug有两种性别,异性之间才交往, 让你根据数据判断是否存在同性恋,输入有 t 组数据,每组数据给出bug数量n, 和关系数m, 以下m行给出相交往的一对Bug编号 a, b.只需要判断有没有, ...
随机推荐
- spark使用正则表达式读入多个文件
String dir = "s3a://example/";String currentDir = dir + "{1[5-9],2[01]}/*.txt";J ...
- sql2008r2安装失败的解决办法
setup fails with: '.', hexadecimal value 0x00, is an invalid character.SQL 2012 Setup issues - hexad ...
- day06_python_1124
01 昨日内容回顾 字典: 增: setdefault() 有责不变,无责添加 dic['key'] = vaulue 删: pop 按照key pop('key') pop('key',None) ...
- Java:程序开机自启动
一.加到开机自动启动程序的注册表: package com.zit; import java.io.IOException; public class Start { public static vo ...
- Dll重定向(尚存否?)
windows核心编程(第五版)的20.6节介绍了Dll重定向. 0x01 Dll重定向简介 产生Dll重定向原因: 应用程序 a.exe 依赖动态链接库 compoent.dll 1.0 版本.但 ...
- centos7安装kvm
一. 安装kvm前的准备工作 1. 清除iptables规则 service iptables save service iptables stop 2. 关闭selinux sed -i 's/SE ...
- <Spark Streaming><本地调试>
写在前面 因为本地电脑没装flume,nginx各种.所以之前写Streaming程序的时候,都是打包了放到集群上跑.就算我在程序代码里不停地logger,调试起来也hin不方便. 于是本地写了两个程 ...
- day 57 data 插件 表的增删改查
一 data的含义 在匹配的元素集合中的所有元素上存储任意相关数据或返回匹配的元素集合中的第一个元素的给定名称的数据存储的值. 1 .data(key, value): 描述:在匹配 ...
- python点滴:判断字符串是否为合法json格式
在一些情况下,我们需要判断字符串是否为合法json格式. 思路很简单:尝试对字符串使用json.loads(),如果不是合法json格式,则会抛出ValueError异常. 示例如下: import ...
- React Native项目集成iOS原生模块
今天学习一下怎么在React Native项目中集成iOS原生模块,道理和在iOS原生项目中集成React Native模块类似.他们的界面跳转靠的都是iOS原生的UINavigationContro ...