hihocoder1066-并查集
来源:互联网 发布:在线随机抽签软件 编辑:程序博客网 时间:2024/05/29 02:50
package com.snnu;import java.util.HashMap;import java.util.Scanner;public class union_find { static int maxn = 100000; static int[] disjointSet = new int[maxn]; static int len; static int initVal = 0; public static void main(String[] args) { Scanner in = new Scanner(System.in); int N = in.nextInt(); HashMap<String, Integer> map = new HashMap<>(); len = N; initData(); for (int i = 0; i < N; i++) { int op = in.nextInt(); String name1 = in.next(); String name2 = in.next(); int fatherx, fathery; if(op == 0) { if (!map.containsKey(name1)) { map.put(name1, initVal++); } if (!map.containsKey(name2)) { map.put(name2, initVal++); } fatherx = find(map.get(name1)); fathery = find(map.get(name2)); if( fatherx != fathery){ union(fatherx, fathery); } }else { fatherx = find(map.get(name1)); fathery = find(map.get(name2)); if(fatherx == fathery){ System.out.println("yes"); }else { System.out.println("no"); } } } } private static void union(int x, int y) { disjointSet[x] = y; } private static int find(int val){ while (val != disjointSet[val]){ val = disjointSet[val]; } return val; } private static void initData() { for (int i = 0; i < len; i++) { disjointSet[i] = i; } initVal = 0; }}
阅读全文
0 0
- hihocoder1066-并查集
- [hihocoder1066]无间道之并查集
- 复习并查集(hihocoder1066)
- HDU3938 并查集 并查集
- 并查集(集并查)
- HDU1232 并查集<并>
- 并查集
- 数据结构-并查集
- 并查集
- 并查集!
- 并查集
- 并查集
- 并查集
- 并查集
- 并查集总结
- 并查集学习
- 并查集
- 并查集
- oracle中写java代码,并调用.
- Spring IOC+AOP原理
- 【数据结构 三】---栈
- Hdu 6058 Kanade's sum【思维+链表模拟】
- 怎么给视频添加电影的古老效果?
- hihocoder1066-并查集
- 学习笔记
- HDOJ 1856 More is better (并查集 + 压缩路径 + 结点深度)
- lightGBM原理、改进简述
- 串结构练习——字符串连接(串)
- 玩出花的职责链模式
- 详解--乘法逆元
- Redis 详解
- libevent之reactor