数据结构 并查集
来源:互联网 发布:剑网三捏脸正太数据 编辑:程序博客网 时间:2024/06/10 03:13
package com.dadou.datastructure;import java.util.Scanner;public class UnionFind {/** * @param args */static int f[]=new int[1003];static int x[][]=new int [1003][2];static boolean map[][]=new boolean[1003][1003]; static boolean flag[]=new boolean[1024];public static void main(String[] args) {// TODO Auto-generated method stubint n,maxValue;Scanner scan=new Scanner(System.in);n=scan.nextInt();maxValue=scan.nextInt();for(int i=1;i<=n;i++){x[i][0]=scan.nextInt(); x[i][1]=scan.nextInt(); f[i]=i; }for(int i=1;i<=n;i++)for(int j=i+1;j<=n;j++){if((double)maxValue>=getValue(i,j)){map[i][j]=true;map[j][i]=true;}}scan.nextLine();while(true){String str=scan.nextLine();if(str.indexOf("o")>-1){int k=Integer.parseInt(str.split(" ")[1]);flag[k]=true;for(int i=1;i<=n;i++){if(flag[i]&&map[i][k]){make(k,i);}}}else{int k=Integer.parseInt(str.split(" ")[1]);int kk=Integer.parseInt(str.split(" ")[2]);check(k, kk);}}}private static void make(int i, int k) {// TODO Auto-generated method stubint t=find(i);int j=find(k);if(t!=j)f[j]=t;}private static int find(int i) {// TODO Auto-generated method stubif(f[i]!=i)f[i]=find(f[i]);return f[i];}private static void check(int i,int k){int t=find(i);int j=find(k);if(t==j)System.out.println("success");else System.out.println("failure");}private static double getValue(int i, int j) {// TODO Auto-generated method stubreturn Math.sqrt((x[i][0]-x[j][0])*(x[i][0]-x[j][0])+(x[i][1]-x[j][1])*(x[i][1]-x[j][1]));}}
0 0
- 数据结构-并查集
- 数据结构-并查集
- 并查集【数据结构】
- 【数据结构】并查集
- 数据结构-并查集
- 数据结构 并查集
- 数据结构 并查集
- 数据结构 -- 并查集
- 数据结构---并查集
- 数据结构---并查集
- 数据结构-并查集
- 数据结构 并查集
- 数据结构-并查集
- [数据结构].并查集
- 并查集 数据结构
- 数据结构-并查集
- 【数据结构】并查集
- 数据结构--并查集
- hdu1058 Humble Numbers(dp打表)
- 疯狂单片机--用C++写STM32程序-STM32PIN
- python使用正则表达式
- Linux根文件系统的挂载过程分析
- JSP中include指令和include动作的区别
- 数据结构 并查集
- 单件模式(Singleton)
- 【技术】【转】字节序问题--大端法小端法
- Windows、Ubuntu双系统重装windows系统后grub引导的修复及默认启动项的修改
- c/c++测量程序运行时间
- u-boot-2011.06在基于s3c2440开发板的移植之引导内核与加载根文件系统
- hdu1176 免费馅饼(塔形)
- 2014百度之星资格赛
- 使用openstack的虚拟机模版注意事项