Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
Re:把答案凑出来了。。。真是神奇~~~mark一下~~In Reply To:把答案凑出来了。。。真是神奇~~~mark一下~~ Posted by:1152230301 at 2012-02-06 14:26:04 public class Main { public static boolean isCommon(Set<Integer> set1,Set<Integer> set2) { Iterator<Integer> it1=set1.iterator(); while(it1.hasNext()) { if(set2.contains(it1.next())) { return true; } } return false; } public static void main(String args[])throws Exception { Scanner cin = new Scanner(System.in); ArrayList<Integer> list=new ArrayList<Integer>(); while(cin.hasNextInt()) { int intn=cin.nextInt();//序列对 int intm=cin.nextInt();//总人数 if(intm==0&&intn==0) { break; } else { TreeSet<Integer>[] set=new TreeSet[intm]; for(int i=0;i<intm;i++) { int x=cin.nextInt(); int y=cin.nextInt(); set[i]=new TreeSet<Integer>(); if(x==y) { set[i].add(x); } else { set[i].add(x);set[i].add(y); } } for(int i=0;i<intm;i++) { for(int j=0;j<intm;j++) { if(Main.isCommon(set[i],set[j])&&(i!=j)) { set[i].addAll(set[j]); set[j].clear(); set[j].add(0); } } } int sum=0; int zeroSet=0; for(int i=0;i<intm;i++) { Iterator<Integer> it=set[i].iterator(); while(it.hasNext()) { if(it.next()==0) { zeroSet++; } else { sum++; } } } list.add(intn-sum+intm-zeroSet); } } Iterator<Integer> it=list.iterator(); int i=1; while(it.hasNext()) { System.out.println("Case "+i+": "+it.next()); i++; } } } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator