本题考查

图,dfs

思路

思路比较简单,深搜就可以,但java代码会爆,C++代码相同思路不会

60分代码

临时没有想出如何优化

import java.util.LinkedList;
import java.util.Scanner;
public class Main {
	static LinkedList<LinkedList<Integer>> list = new LinkedList<LinkedList<Integer>>();
	static boolean[] flag;
	static int start,total=0;
	static void dfs(int index, int num) {
		num++;
		if(num==4) {total++;	return;}
		LinkedList<Integer> tempList = list.get(index);
		flag[index] = true;
		for(int i:tempList)
			if(!flag[i]||(i==start&&num==3))	dfs(i, num);
		flag[index] = false;
	}

	public static void main(String[] args) {
		Scanner scaner = new Scanner(System.in);
		int nodeNum = scaner.nextInt(),edgeNum = scaner.nextInt();
		flag = new boolean[nodeNum];
		for (int i = 0; i < nodeNum; i++)	list.add(new LinkedList<Integer>());
		for (int i = 0; i < edgeNum; i++) {
			int temp1 = scaner.nextInt()-1;
			int temp2 = scaner.nextInt()-1;
			list.get(temp1).add(temp2);
			list.get(temp2).add(temp1);
		}
		scaner.close();
		for(int i=0;i<nodeNum;i++) {
			start=i;
			dfs(i, 0);
		}
		System.out.println(total);
	}
}