题目来自2016网易游戏笔试题
package graph; import java.util.ArrayList; import java.util.LinkedList; import prac.friendset2; public class FriendSet { private ArrayList<Object> vertexList;// 存储点的链表 private int[][] edges;// 邻接矩阵,用来存储边 private int numOfEdges;// 边的数目 public FriendSet(int n) { // 初始化矩阵,一维数组,和边的数目 edges = new int[n][n]; vertexList = new ArrayList<Object>(n); numOfEdges = 0; } // 得到结点的个数 public int getNumOfVertex() { return vertexList.size(); } // 得到边的数目 public int getNumOfEdges() { return numOfEdges; } // 返回结点i的数据 public Object getValueByIndex(int i) { return vertexList.get(i); } // 插入结点 public void insertVertex(Object vertex) { vertexList.add(vertexList.size(), vertex); } // 插入边 public void insertEdge(int v1, int v2, int weight) { edges[v1][v2] = weight; numOfEdges++; } // 插入边 public void insertEdge(int v1, int v2) { edges[v1][v2] = 1; edges[v2][v1] = 1; numOfEdges++; } // 得到第一个邻接结点的下标 public int getFirstNeighbor(int index) { for (int j = 0; j < vertexList.size(); j++) { if (edges[index][j] > 0) { return j; } } return -1; } // 根据前一个邻接结点的下标来取得下一个邻接结点 public int getNextNeighbor(int v1, int v2) { for (int j = v2 + 1; j < vertexList.size(); j++) { if (edges[v1][j] > 0) return j; } return -1; } // 私有函数,广度优先遍历 private void broadFirstSearch(boolean[] isVisited, int i) { int u, w; int size=1; LinkedList<Integer> queue = new LinkedList<Integer>(); // 访问结点i System.out.print(getValueByIndex(i) + " "); isVisited[i] = true; // 结点入队列 queue.addLast(i); while (!queue.isEmpty()) { u = ((Integer) queue.removeFirst()).intValue(); w = getFirstNeighbor(u); //u的第1个w节点 while (w != -1) { //一个u 连接n个w if (!isVisited[w]) { size++; // 访问该结点 System.out.print(getValueByIndex(w) + " "); // 标记已被访问 isVisited[w] = true; // 入队列 queue.addLast(w); } // 寻找下一个邻接结点 w = getNextNeighbor(u, w); } } System.out.println("这个圈 共有 "+size+" 个人"); } // 对外公开函数,广度优先遍历 public void broadFirstSearch() { boolean[] isVisited = new boolean[getNumOfVertex()]; int numOfVertex=getNumOfVertex(); for (int i = 0; i < numOfVertex; i++) isVisited[i] = false; for (int i = 0; i < numOfVertex; i++) { if (!isVisited[i]) broadFirstSearch(isVisited, i); } } public static void main(String args[]) { FriendSet graph = getGraph(); graph.broadFirstSearch(); } private static FriendSet getGraph() { int n = 9, e = 9;// 分别代表结点个数和边的数目 FriendSet graph = new FriendSet(15); for (int i=0;i<15;i++) { graph.insertVertex(i);// 插入结点 } // 插入九条边 graph.insertEdge(1, 2); graph.insertEdge(2, 3); graph.insertEdge(5, 4); graph.insertEdge(3, 4); graph.insertEdge(6, 7); graph.insertEdge(8, 6); graph.insertEdge(9, 10); graph.insertEdge(10, 11); return graph; } }输出结果:
看代码
package prac; /** * Created by zhangchen on 2015/9/14. */ import java.util.*; /** * Created by zhangchen on 2015/9/14. */ public class friendset2 { public static void main(String[] args) { // Scanner scanner = new Scanner(System.in); // int n = scanner.nextInt(); // scanner.nextLine(); // FriendShip[] friendShips = new FriendShip[n]; // for (int i = 0; i < n; i++) { // friendShips[i] = new FriendShip(); // friendShips[i].f1 = scanner.nextInt(); // friendShips[i].f2 = scanner.nextInt(); // } int n=8; FriendShip[] friendShips = new FriendShip[n]; getData(friendShips); List<Set<Integer>> circles = new ArrayList<>(); for (int i = 0; i < n; i++) { boolean flag = false; boolean more = false; int connectwith = -1; for (int j = 0; j < circles.size(); j++) { if (circles.get(j).contains(friendShips[i].f1)) { circles.get(j).add(friendShips[i].f2); if (connectwith >= 0 && connectwith != j) more = true; if (connectwith == -1) connectwith = j; flag = true; } else if (circles.get(j).contains(friendShips[i].f2)) { circles.get(j).add(friendShips[i].f1); if (connectwith >= 0 && connectwith != j) more = true; if (connectwith == -1) connectwith = j; flag = true; } if (more) { for (int e : circles.get(j)) { circles.get(connectwith).add(e); } circles.remove(j); } } if (!flag) { Set<Integer> circle = new HashSet<>(); circle.add(friendShips[i].f1); circle.add(friendShips[i].f2); circles.add(circle); } } Collections.sort(circles, new Comparator<Set<Integer>>() { @Override public int compare(Set<Integer> o1, Set<Integer> o2) { return o2.size() - o1.size(); } }); for (int k = 0; k < circles.size(); k++) System.out.println(circles.get(k).size()); } private static void getData(FriendShip[] friendShips) { FriendShip friendShip = new FriendShip(); friendShip.f1 = 1; friendShip.f2 = 2; friendShips[0] = friendShip; friendShip = new FriendShip(); friendShip.f1 = 10; friendShip.f2 = 11; friendShips[1] = friendShip; friendShip = new FriendShip(); friendShip.f1 = 4; friendShip.f2 = 5; friendShips[2] = friendShip; friendShip = new FriendShip(); friendShip.f1 = 4; friendShip.f2 = 3; friendShips[3] = friendShip; friendShip = new FriendShip(); friendShip.f1 = 6; friendShip.f2 = 7; friendShips[4] = friendShip; friendShip = new FriendShip(); friendShip.f1 = 9; friendShip.f2 = 10; friendShips[5] = friendShip; friendShip = new FriendShip(); friendShip.f1 = 3; friendShip.f2 = 2; friendShips[6] = friendShip; friendShip = new FriendShip(); friendShip.f1 = 7; friendShip.f2 = 2; friendShips[7] = friendShip; } } class FriendShip { int f1; int f2; }