poj 1308(并查集)
来源:互联网 发布:高丝面膜 知乎 编辑:程序博客网 时间:2024/05/01 06:02
package com.liang.poj;import java.util.Scanner;public class Test1308_Union_Fand_set {static int N = 100001;static int[] node = new int[N];static int[] rank = new int[N];static int count = 1;static boolean tag = true;public static void main(String[] args) throws Exception {Scanner scan = new Scanner(System.in);makeSet();while (scan.hasNext()) {int a = scan.nextInt();int b = scan.nextInt();if (a == -1 && b == -1) {break;}if (a == 0 && b == 0) {if (tag) {if (!judge()) {System.out.println("Case " + count++ + " is not a tree.");} else {System.out.println("Case " + count++ + " is a tree.");}} else {System.out.println("Case " + count++ + " is not a tree.");}makeSet();} else {merge(a, b);}}}public static void makeSet() { // 初始化单元素集合tag = true;for (int i = 1; i < N; i++) {node[i] = 0;rank[i] = 0;}}public static int find(int x) { // 带路径压缩的查找if (node[x] == 0) {node[x] = x;return x;} else {if (x != node[x]) {x = find(node[x]);}return x;}}public static void merge(int a, int b) { // 合并集合a,集合bint fa = find(a);int fb = find(b);if (fa == fb) {if (fa == 0 && fb == 0) {if (rank[fa] >= rank[fb]) {node[fb] = fa;rank[fa]++;} else {node[fa] = fb;rank[fb]++;}} else {tag = false;}} else {if (rank[fa] >= rank[fb]) {node[fb] = fa;rank[fa]++;} else {node[fa] = fb;rank[fb]++;}}}public static boolean judge() {int sum = 0;for (int i = 1; i < node.length; i++) {if (node[i] == 0) {continue;} else {if (i == node[i]) {sum++;}if (sum >= 2) {return false;}}}return true;}}
- poj-1308 并查集
- poj 1308(并查集)
- poj 1308(并查集)
- POJ-1308 并查集
- poj 1308(并查集)
- POJ 1308/并查集
- poj 1308 并查集
- POJ 1308 并查集
- Poj 并查集
- poj并查集
- POJ 并查集
- poj 1308 并查集判断“树”
- 并查集浅析(poj 1308)
- 并查集之poj 1308
- POJ 1308(并查集判断树)
- POJ 1308(简单并查集)
- 数据结构 并查集 Poj 1308
- poj 2524 并查集
- ListView添加事件并获取选中项的值
- IE8的调试工具使用详解-web标准开发人员专用
- Ogre的资源加载策略
- Spacetsk(SRM545-div1-2-div2-3)
- SetAndSet(SRM545-div1-3)
- poj 1308(并查集)
- C++之异常(exception)
- 构造函数与析构函数
- C++之vector使用
- 有向树
- SAM基因芯片分析软件
- C++之Erase-remove惯用法
- SqlDataAdapter;DataSet;DataTable;DataTableCollection 关联
- javascript对字符串进行html编码