题目链接

并查集可以用于聚类。

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. 【API规范】OpenAPI规范

    OpenAPI规范 openAPI 3.0_百度搜索 OpenAPI Specification 2.0 - CSDN博客 APP相关_API 列表_OpenAPI 2.0_开发指南_移动推送-阿里云 ...

  2. RNN(Recurrent Neural Networks)公式推导和实现

    RNN(Recurrent Neural Networks)公式推导和实现 http://x-algo.cn/index.php/2016/04/25/rnn-recurrent-neural-net ...

  3. sql 根据另一个表的数据更新当前表

    --update mchk --set shwdjh=dbo.erpzhong.zhongbz--from erpzhong--where mchk.dwmch=erpzhong.matno

  4. memcache的资料集

    1. 安装与查看过程(完整) https://www.cyberciti.biz/faq/rhel-fedora-linux-install-memcached-caching-system-rpm/ ...

  5. eclipse help说明链接

    http://help.eclipse.org/luna/index.jsp?topic=%2Forg.eclipse.cdt.doc.user%2Freference%2Fcdt_u_prop_bu ...

  6. 主成分分析(PCA)原理及推导

    原文:http://blog.csdn.net/zhongkejingwang/article/details/42264479 什么是PCA? 在数据挖掘或者图像处理等领域经常会用到主成分分析,这样 ...

  7. 上机题目(0基础)- Java网络操作-打印网页(Java)

    打印一个网页,熟悉Java网络编程: import java.io.BufferedReader; import java.io.IOException; import java.io.InputSt ...

  8. “No module named bs4”问题

    用tushare,import的时候,老报这个错.我的系统是重装的,包都是直接复制过来的.重新安装也不行. 最后,从网上下beautifulsoup4-4.6.0-py2-none-any.whl安装 ...

  9. OpenGL ES 3.0之VertexAttributes,Vertex Arrays,and Buffer Objects(九)

    顶点数据,也称为顶点属性,指每一个顶点数据.指能被用来描述每个顶点的数据,或能被所有顶点使用的常量值.例如你想绘制一个具有颜色的立方体三角形.你指定一个恒定的值用于三角形的所有三个顶点颜色.但三角形的 ...

  10. ZH奶酪:Ionic通过angularJS+tabs-item-hide实现自定义隐藏tab

    参考链接:http://stackoverflow.com/questions/23991852/how-do-i-hide-the-tabs-in-ionic-framework 1.index.h ...