并查集不知道哪里出错了
2018-03-31 09:30
239 查看
package com.company; import java.util.*; public class Main{ //并查集 static int N, M;//N个数字,M个操作 static int[]bcj=new int[10001]; static Scanner sc=new Scanner(System.in); static int Find(int x){ if(bcj[x]<0) return x; return bcj[x]=Find(bcj[x]); } static void Union(int x, int y){ x=Find(x); y=Find(y); if(x==y) return; bcj[x]+=bcj[y]; bcj[y]=x; } public static void main(String[] args){ N=sc.nextInt(); M=sc.nextInt(); int i, x, y; String[] com={"Union", "Find", "Count"}; for(i=1; i<=N; i++) bcj[i]=-1; for(i=1; i<=M; i++){ if(sc.next()==com[0]){ x=sc.nextInt(); y=sc.nextInt(); Union(x, y); } if(sc.next()==com[1]){//Find x=sc.nextInt(); y=sc.nextInt(); if(Find(x)==Find(y)) System.out.println("True"); System.out.println("False"); } if(sc.next()==com[2]){ x=sc.nextInt(); System.out.println(-bcj[Find(x)]); } } } }
相关文章推荐
- 读取文件中的数据出错,不知道哪里有问题
- 设计一个表格控件,总在这个过程出错,不知道原因出在哪里,高手帮我找找原因?
- 在一个活动中调用碎片里的方法,不知道哪里出错了
- 请求大虾们救我,解决下面的代码!我不知道哪里出错了!!!
- 一个不超过10位的数字,要显示每个数字对应英文,程序不知道哪里出错,10位数字老错
- javascript调试过程中找不到哪里出错的可能原因
- 计时,自己写的...能跑.问题是有的,但不知道是哪里出了问题
- 例程调试出错,不知错哪里??
- 想在编辑框返回一个数值,但是不知哪里出错
- POJ 3009 原来写的不知道哪里错了。
- 不知道从哪里来的
- 不学习真的会落后,最近两年没学习,都不知道被甩到哪里去了
- linux 系统缓慢 SAR -Q 看到好多活跃进程 不知道从哪里查出是哪些进程了 郁闷
- 这个博客好难写啊,不知道从哪里下手
- 不知道输入哪里的问题。。。只能输五个?纠结很久。。
- 类似网易新闻分类排序的源码,已经不知道转自哪里,抱歉
- eclipse 项目报错 但没有提示哪里出错
- 关于UITabbarItem在iOS 8 上显示不知道哪里来的蓝色并且模糊的问题解决办法
- 不知道哪里错了?请大家帮忙……
- 真心不知道哪里错了 。。。