题目链接

并查集可以用于聚类。

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:多级并查集的更多相关文章

  1. 【hihocoder】欧拉路径 并查集判连通

    #include<iostream> #include<cstdio> #include<string> #include<cstring> #incl ...

  2. HihoCoder 1638 : 小Hi的天平 (2-sat+并查集)

    描述 小Hi给小Ho邮寄了一个天平.收到天平后,小Ho想知道天平在运输过程中是否损坏,为此它准备了A类物品和B类物品共n个(可能只有A类物品,也可能只有B类物品),但无法确定一个物品是哪一类.A类物品 ...

  3. hihoCoder 1515 分数调查(带权并查集)

    http://hihocoder.com/problemset/problem/1515 题意: 思路: 带权并查集的简单题,计算的时候利用向量法则即可. #include<iostream&g ...

  4. hihoCoder #1291 : Building in Sandbox 逆向处理+并查集维护

    /** 题目:#1291 : Building in Sandbox 链接:https://hihocoder.com/problemset/problem/1291 题意:就是一个三维的空间里,按照 ...

  5. ACM学习历程—Hihocoder 1291 Building in Sandbox(dfs && 离线 && 并查集)

    http://hihocoder.com/problemset/problem/1291 前几天比较忙,这次来补一下微软笔试的最后一题,这题是这次微软笔试的第四题,过的人比较少,我当时在调试B题,没时 ...

  6. hihocoder 1066 无间道之并查集

    #1066 : 无间道之并查集 时间限制:20000ms 单点时限:1000ms 内存限制:256MB 描述 这天天气晴朗.阳光明媚.鸟语花香,空气中弥漫着春天的气息……额,说远了,总之,小Hi和小H ...

  7. 【hihoCoder第十四周】无间道之并查集

    就是基础的并查集.0代表合并操作,1代表查询操作.一开始以为会卡路径压缩,忐忑的交了一版裸并查集,结果AC了.数据还是很水的. 以后坚持做hiho,当额外的练习啦~ #include <bits ...

  8. [hihoCoder]无间道之并查集

    题目大意: #1066 : 无间道之并查集 时间限制:20000ms 单点时限:1000ms 内存限制:256MB 描述 这天天气晴朗.阳光明媚.鸟语花香,空气中弥漫着春天的气息……额,说远了,总之, ...

  9. hihoCoder 树结构判定(并查集)

    思路:树满足两个条件: 1.顶点数等于边数加一 2.所有的顶点在一个联通块 那么直接dfs或者并查集就可以了. AC代码 #include <stdio.h> #include<st ...

随机推荐

  1. Git SVN 版本控制 简介 总结 MD

    Git 使用准备 主流的 Git 托管网站 GitLab,主流网站,私有仓库也完全免费,功能更强大,页面精美,操作方便 GitHub,最著名的免费Git托管网站,缺点是免费的不支持私有项目 OSChi ...

  2. RecyclerView的使用(2)之多Item布局的载入

    原创文章,转载请注明 http://blog.csdn.net/leejizhou/article/details/50708349 李济洲的博客 上一篇介绍的了RecyclerView的基础使用ht ...

  3. 用keras实现lstm 利用Keras下的LSTM进行情感分析

    1    I either LOVE Brokeback Mountain or think it’s great that homosexuality is becoming more accept ...

  4. wifidog接口文档(转)

    目录(?)[-] 网关心跳协议 请求信息 回复格式 例子 用户状态心跳协议 请求格式 注意 回复格式 状态码 例子 跳转协议 请求格式 例子 注册协议 请求格式 例子 wifidog是搭建无线热点认证 ...

  5. c++ string wstring 字符串替换

      int CStringTool::Replace(std::wstring& strContent, std::wstring& strReplace, std::wstring  ...

  6. 【摘录】在Windows平台上使用Objective-C

    虽然到目前为止最好的Objective-C 编码平台来自苹果公司,但它们绝不仅适用于苹果公司的平台.Objective-C 在Linux.BSD 甚至Windows 等其他平台都有相当久远的历史.根据 ...

  7. mysql zerofill 的使用

    转自:http://www.jquerycn.cn/blog/mysql/ 那这个int[M]中M是什么意义喃,在定义数值型数据类型的时候,可以在关键字括号内指定整数值(如:int(M),M的最大值为 ...

  8. spring结合mybatis实现数据库读写分离

    随着系统用户访问量的不断增加,数据库的频繁访问将成为我们系统的一大瓶颈之一.由于项目前期用户量不大,我们实现单一的数据库就能完成.但是后期单一的数据库根本无法支撑庞大的项目去访问数据库,那么如何解决这 ...

  9. 013-Go通archive/zip生成ZIP文件

    package main import( "io/ioutil" "os" "bytes" "archive/zip" ...

  10. Velocity使用总结

    一.载入Velocity依赖包 <dependency> <groupId>org.apache.velocity</groupId> <artifactId ...