生活随笔
收集整理的這篇文章主要介紹了
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;}@Overridepublic 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:多级并查集的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。