package farm;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
public class Farmland {
public static List<Vertice> vertices = new ArrayList<Vertice>();
public static int[] degree = new int[300];
public static Map<Integer, List<Integer>> adjsMap = new HashMap<Integer, List<Integer> >();
public static boolean[] visited = new boolean[300];
public static int begin;
public static int length;
public static int findProper(int size) {
int result = 0;
for(int i = 0; i < vertices.size(); i++) {
begin = vertices.get(i).getNum();
length = size - 1;
DFS(begin, size-1);
}
return result;
}
public static void DFS(int v, int len) {
visited[v] = true;
if(len == length)
int w;
List<Integer> adjs = adjsMap.get(v);
for(int i = 0; i < degree[v]; i++) {
w = adjs.get(i);
if(visited[w]) {
DFS(w);
}
}
}
public List<Integer> calc(List<String> input) {
List<Integer> resultList = new ArrayList<Integer>();
int caseNum = Integer.parseInt(input.get(0));
int index = 1;
int size = 0;
while(caseNum > 0) {
caseNum--;
vertices.clear();
int verticeNum = Integer.parseInt(input.get(index++));
for(int i = 0; i < verticeNum; i++) {
String verticeStr = input.get(index++);
String[] verticedes = verticeStr.split("\\s");
visited[Integer.parseInt(verticedes[0])] = false;
Vertice vertice = new Vertice(Integer.parseInt(verticedes[0]),
Integer.parseInt(verticedes[1]), Integer.parseInt(verticedes[2]),
Integer.parseInt(verticedes[3]));
degree[vertice.getNum()] = Integer.parseInt(verticedes[3]);
List<Integer> adjList = new ArrayList<Integer>();
for(int j = 4; j < verticedes.length; j++) {
adjList.add(Integer.parseInt(verticedes[j]));
adjsMap.put(vertice.getNum(), adjList);
}
vertices.add(vertice);
}
size = Integer.parseInt(input.get(index++));
resultList.add(findProper(size));
}
return resultList;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
List<String> inputstrList = new ArrayList<String>();
while(scanner.hasNextLine()) {
String tmp = scanner.nextLine();
inputstrList.add(tmp);
}
List<Integer> resultList = new Farmland().calc(inputstrList);
for(Integer result : resultList) {
System.out.println(result);
}
}
}
Farmland
Description
We have a map for farming land in a country. The whole farming land of the country is divided into a set of disjoint farming regions. Each farmer owns only one farming region in this country. There is a boundary fence between two neighboring farming regions. The farmland map for this country can be represented in a plane graph. The following Figure-1 shows one example.