hihocoder 1638:多级并查集
题目链接
并查集可以用于聚类。
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
class Main {
int N = (int) (1e4 + 7);
int father[] = new int[N << 1];
class FatherDis {
int father;
int dis;
FatherDis(int father, int dis) {
this.father = father;
this.dis = dis;
}
@Override
public String toString() {
return String.format("(father=%d,dis=%d)", father, dis);
}
}
FatherDis find(int x) {
if (father[x] == x) {
return new FatherDis(x, 0);
}
FatherDis f = find(father[x]);
f.dis++;
if ((f.dis & 1) == 0) {
father[x] = f.father + N;
} else {
father[x] = f.father;
}
return f;
}
Main() {
Scanner cin = new Scanner(System.in);
int t = cin.nextInt();
while (t-- > 0) {
int n = cin.nextInt(), m = cin.nextInt();
for (int i = 1; i <= n; i++) father[i] = father[i + N] = i;
int fail = -1;
for (int i = 0; i < m; i++) {
int x = cin.nextInt(), u = cin.nextInt(), v = cin.nextInt();
if (fail != -1) continue;//读完数据再说
FatherDis fu = find(u), fv = find(v);
boolean sameLevel = (fu.dis & 1) == (fv.dis & 1);
if (x == 0) {
if (fu.father != fv.father) {
if (sameLevel) {
father[fu.father] = fv.father + N;
} else {
father[fu.father] = fv.father;
}
} else {
if (!sameLevel) {//不同类别
fail = i;
}
}
} else {
if (fu.father != fv.father) {
if (sameLevel) {
father[fu.father] = fv.father;
} else {
father[fu.father] = fv.father + N;
}
} else {
if (sameLevel) {//同一类别
fail = i;
}
}
}
}
if (fail == -1) {
System.out.println("great");
} else {
System.out.println("sad");
System.out.println(fail + 1);
}
}
}
public static void main(String[] args) {
new Main();
}
}
hihocoder 1638:多级并查集的更多相关文章
- 【hihocoder】欧拉路径 并查集判连通
#include<iostream> #include<cstdio> #include<string> #include<cstring> #incl ...
- HihoCoder 1638 : 小Hi的天平 (2-sat+并查集)
描述 小Hi给小Ho邮寄了一个天平.收到天平后,小Ho想知道天平在运输过程中是否损坏,为此它准备了A类物品和B类物品共n个(可能只有A类物品,也可能只有B类物品),但无法确定一个物品是哪一类.A类物品 ...
- hihoCoder 1515 分数调查(带权并查集)
http://hihocoder.com/problemset/problem/1515 题意: 思路: 带权并查集的简单题,计算的时候利用向量法则即可. #include<iostream&g ...
- hihoCoder #1291 : Building in Sandbox 逆向处理+并查集维护
/** 题目:#1291 : Building in Sandbox 链接:https://hihocoder.com/problemset/problem/1291 题意:就是一个三维的空间里,按照 ...
- ACM学习历程—Hihocoder 1291 Building in Sandbox(dfs && 离线 && 并查集)
http://hihocoder.com/problemset/problem/1291 前几天比较忙,这次来补一下微软笔试的最后一题,这题是这次微软笔试的第四题,过的人比较少,我当时在调试B题,没时 ...
- hihocoder 1066 无间道之并查集
#1066 : 无间道之并查集 时间限制:20000ms 单点时限:1000ms 内存限制:256MB 描述 这天天气晴朗.阳光明媚.鸟语花香,空气中弥漫着春天的气息……额,说远了,总之,小Hi和小H ...
- 【hihoCoder第十四周】无间道之并查集
就是基础的并查集.0代表合并操作,1代表查询操作.一开始以为会卡路径压缩,忐忑的交了一版裸并查集,结果AC了.数据还是很水的. 以后坚持做hiho,当额外的练习啦~ #include <bits ...
- [hihoCoder]无间道之并查集
题目大意: #1066 : 无间道之并查集 时间限制:20000ms 单点时限:1000ms 内存限制:256MB 描述 这天天气晴朗.阳光明媚.鸟语花香,空气中弥漫着春天的气息……额,说远了,总之, ...
- hihoCoder 树结构判定(并查集)
思路:树满足两个条件: 1.顶点数等于边数加一 2.所有的顶点在一个联通块 那么直接dfs或者并查集就可以了. AC代码 #include <stdio.h> #include<st ...
随机推荐
- Recover Binary Search Tree leetcode java
题目: Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without chan ...
- Python3 使用 matplotlib 画折线图
ChartUtil.py import matplotlib.pyplot as plt from pylab import mpl def plotLine(xData,yData,xLabel,c ...
- Win10系统80端口被系统进程占用
一.问题 有系统需要用到80端口,为了方便,但是发现80端口被占用,执行netstat -ano 发现80端口竟然被一个System process占用了,当然这个是不能被杀掉的 二.解决问题 在网上 ...
- CSS中的图片路径问题
CSS中的背景图片写了相对路径,为什么不显示那? [解决方法] CSS中的背景图片路径应该写成相对于当前CSS文件的路径,而不是针对网站根目录的相对路径.
- HDU 1541 Stars (线段树)
Problem Description Astronomers often examine star maps where stars are represented by points on ...
- ASP入门(二)-创建Access数据库
通常来说,ASP程序是搭配Access数据库来使用的,因此在安装完ASP环境后,为了方便建立和管理数据库,我们还需要安装Access数据库. Access是Microsoft Office家族中的一员 ...
- C#.NET常见问题(FAQ)-如何让listView如何选中一行
把FullRowSelect设置为True 更多教学视频和资料下载,欢迎关注以下信息: 我的优酷空间: http://i.youku.com/acetaohai123 我的在线论坛: ht ...
- .geodatabase与gdb的相互转换
.geodatabase长得是gdb的全称,确实它们有一定的关系,但也有区别. 简单认识一下 有人也问过我,gdb外表像个文件夹,是怎么实现的.gdb数据库是ESRI特有的数据库,它是一些数据集定义. ...
- jQuery 超屏加载
jQuery 超屏加载,当文档超出屏幕的高度时,加载最新下个列数据 $(window).scroll(function () { var height = $(document).height(); ...
- Selenium2(WebDriver)总结(一)---启动浏览器、设置profile&加载插件
本文主要记录下在使用selenium2/webdriver时启动各种浏览器的方法.以及如何加载插件.定制浏览器信息(设置profile)等 环境搭建可参考我的另一篇文章:http://www.cnbl ...