笔画
2015-08-24 18:44
281 查看
import java.util.*; public class Main { static Main ybh = new Main(); private class StackX { private final int SIZE=1000; private int[] st; private int top; public StackX(){ st = new int[SIZE]; top = -1; } public void push(int j){ st[++top] = j; } public int pop(){ return st[top--]; } public int peek(){ return st[top]; } public boolean isEmpty(){ return top==-1; } } private class Vertex { public int value; public boolean wasVisited; public Vertex(int a){ this.value = a; this.wasVisited = false; } } public static void main(String[] args) { Scanner scan = new Scanner(System.in); int num = scan.nextInt(); Vertex vertexList[] = new Vertex[num]; int[][]adjArr = new int[num][num]; int m = 1; int nVerts = 0; while(num>0){ vertexList[nVerts++] = ybh.new Vertex(m); m++; int a = scan.nextInt(); int b = scan.nextInt(); adjArr[a-1][b-1] = 1; adjArr[b-1][a-1] = 1; num--; } //判断是否是连通图 boolean flag = dfs(vertexList,adjArr,nVerts); if(flag){ //判断每行1的个数,放进数组,循环数组,若素组元素均是偶数返回true,如果素组中有两个元素是奇数页返回true,否则返回false; int itemCount = 0; List<Integer> ll = new ArrayList<Integer>(); for (int i = 0; i < adjArr.length; i++) { for (int j = 0; j < adjArr.length; j++) { if(adjArr[i][j] == 1) itemCount++; } ll.add(itemCount); itemCount = 0; } int []arr = new int[adjArr.length]; Iterator<Integer> it = ll.iterator(); int index = 0; while (it.hasNext()) { arr[index++] = it.next(); } int odd = 0; int even = 0; for (int i = 0; i < arr.length; i++) { if(arr[i]%2==0) even++; else odd++; } if(even==arr.length || odd==2) System.out.println(true); else System.out.println(false); } else System.out.println(false); } private static boolean dfs(Vertex[] vertexList, int[][] adjArr,int n) { StackX theStack = ybh.new StackX(); vertexList[0].wasVisited = true; theStack.push(0); int count = 1; while (!theStack.isEmpty()) { int v = getAdjUnvisiedVertex(vertexList,theStack.peek(),adjArr,n); if(v==-1) theStack.pop(); else{ vertexList[v].wasVisited = true; theStack.push(v); count++; } } for (int i = 0; i < n; i++) { vertexList[i].wasVisited = false; } if(count == n) return true; else return false; } private static int getAdjUnvisiedVertex(Vertex[] vertexList,int v,int[][] adjArr,int nVerts) { for (int j = 0; j < nVerts; j++) { if(adjArr[v][j]==1 && vertexList[j].wasVisited == false) return j; } return -1; } }
相关文章推荐
- STL学习----入门(1)[stack]
- UIAlert的使用
- Asp.Net中的三种分页方式
- 关于AutoCompleteTextView实现前几个历史提示
- linux之chattr命令
- 【POJ 3662】Telephone Lines
- Censored! - POJ 1625(ac自动机+简单dp+高精度运算)
- Asp.Net中的三种分页方式
- 一次奇怪的调优
- swift 动画
- 【转】Ubuntu 12.04 KVM+Qemu 搭建虚拟机
- Nginx出现413 Request Entity Too Large错误解决方法
- STL学习----入门(1)[unordered_set]
- hdoj 1052 Tian Ji -- The Horse Racing【田忌赛马,贪心】
- Atlassian 产品安装、管理中出现的问题集锦
- 数据库的三大范式
- broadAnywhere:Broadcast组件权限绕过漏洞(Bug: 17356824)
- 2015.8.24
- 黑马程序员——集合(二)Set
- Uva-11988 - Broken Keyboard (a.k.a. Beiju Text)(链表)