挑战程序竞赛系列(10):2.4并查集
来源:互联网 发布:mac上能玩哪些网络游戏 编辑:程序博客网 时间:2024/05/29 16:46
挑战程序竞赛系列(10):2.4并查集
详细代码可以fork下Github上leetcode项目,不定期更新。
练习题如下:
- POJ 2236: Wireless Network
- POJ 1703: Find them, Catch them
- AOJ 2170: Marked Ancestor
POJ 2236: Wireless Network
写个Union,在距离范围内的电脑可以认为是同属于一个集合,进行合并。当然还需要注意电脑当前的状态是否修复,如果未修复,即使在指定距离内,也不能合并。
即使做了些状态记录,Java代码还是慢啊。
代码如下:
public class SolutionDay15_L2236 { static class Union{ int[] id; int[] sz; public Union(int size){ id = new int[size]; sz = new int[size]; for (int i = 0; i < size; ++i){ id[i] = i; sz[i] = 1; } } public void union(int i, int j){ int p = find(i); int q = find(j); if (p == q) return; if (sz[p] < sz[q]){ id[p] = q; sz[q] += sz[p]; } else{ id[q] = p; sz[p] += sz[q]; } } public int find(int i){ while (id[i] != i){ i = id[i]; } return i; } public boolean connect(int i, int j){ return find(i) == find(j); } } private static boolean distance(int[] c1, int[] c2, int d){ int x = c1[0] - c2[0]; int y = c1[1] - c2[1]; return (x * x + y * y) <= d * d; } public static void main(String[] args) { Scanner in = new Scanner(System.in); String[] n = in.nextLine().trim().split(" "); int N = Integer.parseInt(n[0]); int D = Integer.parseInt(n[1]); boolean[] status = new boolean[N]; boolean[][] distance = new boolean[N][N]; Union union = new Union(N); int[][] coord = new int[N][2]; for (int i = 0; i < N; ++i){ n = in.nextLine().trim().split(" "); coord[i][0] = Integer.parseInt(n[0]); coord[i][1] = Integer.parseInt(n[1]); } for (int i = 0; i < N; ++i){ for (int j = i + 1; j < N; ++j){ if(distance(coord[i], coord[j], D)){ distance[i][j] = true; distance[j][i] = true; } } } while (in.hasNextLine()){ String[] operates = in.nextLine().trim().split(" "); if (operates[0].equals("O")){ int c = Integer.parseInt(operates[1]) - 1; status[c] = true; for (int j = 0; j < N; ++j){ if (j == c) continue; if (status[j] && distance[j][c]) union.union(c, j); } } if (operates[0].equals("S")){ int c1 = Integer.parseInt(operates[1]) - 1; int c2 = Integer.parseInt(operates[2]) - 1; System.out.println(union.connect(c1, c2) ? "SUCCESS" : "FAIL"); } } in.close(); }}
POJ 1703: Find them, Catch them
思路:
copy一团罪犯,敌人的敌人是我们的朋友。
代码如下:
public class SolutionDay15_L1703 { static class Union{ int[] id; int[] sz; public Union(int size){ id = new int[size]; sz = new int[size]; for (int i = 0; i < size; ++i){ id[i] = i; sz[i] = 1; } } public int find(int i){ while (id[i] != i){ i = id[i]; } return i; } public boolean same(int i, int j){ return find(i) == find(j); } public void union(int i, int j){ int p = find(i); int q = find(j); if (p == q) return; if (sz[p] < sz[q]){ id[p] = q; sz[q] += sz[p]; } else{ id[q] = p; sz[p] += sz[q]; } } } public static void main(String[] args) { Scanner in = new Scanner(System.in); int T = in.nextInt(); for (int i = 0; i < T; ++i){ int N = in.nextInt(); int M = in.nextInt(); Union u = new Union(2 * N); for (int j = 0; j < M; ++j){ String opera = in.next(); int c1 = in.nextInt() - 1; int c2 = in.nextInt() - 1; if (opera.equals("A")){ if (u.same(c1, c2)){ System.out.println("In the same gang."); } else if (u.same(c1, c2 + N)){ System.out.println("In different gangs."); } else{ System.out.println("Not sure yet."); } } else{ u.union(c1, c2 + N); u.union(c1 + N, c2); } } } in.close(); }}
AOJ 2170: Marked Ancestor
非常easy,dfs找父结点即可,如果mark,则把该结点的父亲改为自己即可。
代码如下:
public static int find(int i){ while (i != id[i]) i = id[i]; return i; } static int[] id; public static void main(String[] args) throws IOException { Scanner in = new Scanner(System.in); while (true){ int N = in.nextInt(); int Q = in.nextInt(); if (N == 0 && Q == 0) break; id = new int[N]; for (int i = 1; i < N; ++i){ int p = in.nextInt() - 1; id[i] = p; } long sum = 0; for (int i = 0; i < Q; ++i){ String opera = in.next(); int c = in.nextInt() - 1; if (opera.equals("Q")){ sum += (find(c) + 1); } else{ id[c] = c; } } System.out.println(sum); } }
阅读全文
0 0
- 挑战程序竞赛系列(10):2.4并查集
- POJ 1182-食物链 [并查集] 《挑战程序设计竞赛》2.4
- 挑战程序竞赛系列(9):2.4优先队列
- 《挑战程序设计竞赛》2.4.2 数据结构-并查集 POJ1182 2236 1703 AOJ2170
- POJ 2236 Wireless Network [并查集] 《挑战程序设计竞赛》2.4
- 挑战程序竞赛系列(81):4.3 LCA(1)
- 挑战程序竞赛系列(82):4.3 LCA(2)
- 挑战程序竞赛系列(1):2.3动态规划
- 挑战程序竞赛系列(4):2.1深度优先搜索
- 挑战程序竞赛系列(5):2.1广度优先搜索
- 挑战程序竞赛系列(6):2.1穷尽搜索
- 挑战程序竞赛系列(11):2.5最短路径
- 挑战程序竞赛系列(12):2.5最小生成树
- 挑战程序竞赛系列(13):2.6辗转相除法
- 挑战程序竞赛系列(14):2.6素数
- 挑战程序竞赛系列(15):2.6快速幂运算
- 挑战程序竞赛系列(16):3.1最大化最小值
- 挑战程序竞赛系列(17):3.1最大化平均值
- OFDPA软件概述
- mac添加AppleId失败的解决方法
- 5. Longest Palindromic Substring
- GDAL编译过程中的几个问题
- Python的thread
- 挑战程序竞赛系列(10):2.4并查集
- redis持久化分析
- Java基础知识总结(绝对经典)
- Websphere8.5.5.2 应用启动异常 Failed to load webapp: Context root /* is already bound.
- 回音消除(far和near数据分析)
- Docker私有仓库Registry使用
- 【JavaSE系列-基础篇6】——数组
- BZOJ1087 洛谷P1896 [SCOI2005]互不侵犯King
- DirectoryEntry使用-实现域登录