http://poj.org/problem?id=3895
http://www.voidcn.com/blog/shawnnnnn/article/p-4488601.html
Each of the M lanes of the Park of Polytechnic University of Bucharest connects two of the N crossroads of the park (labeled from 1 to N). There is no pair of crossroads connected by more than one lane and it is possible to pass from each crossroad to each other crossroad by a path composed of one or more lanes. A cycle of lanes is simple when passes through each of its crossroads exactly once.
The administration of the University would like to put on the lanes pictures of the winners of Regional Collegiate Programming Contest in such way that the pictures of winners from the same university to be on the lanes of a same simple cycle. That is why the administration would like to assign the longest simple cycles of lanes to most successful universities. The problem is to find the longest cycles? Fortunately, it happens that each lane of the park is participating in no more than one simple cycle (see the Figure).
The administration of the University would like to put on the lanes pictures of the winners of Regional Collegiate Programming Contest in such way that the pictures of winners from the same university to be on the lanes of a same simple cycle. That is why the administration would like to assign the longest simple cycles of lanes to most successful universities. The problem is to find the longest cycles? Fortunately, it happens that each lane of the park is participating in no more than one simple cycle (see the Figure).
Input
On the first line of the input file the number T of the test cases will be given. Each test case starts with a line with the positive integers N and M, separated by interval (4 <= N <= 4444). Each of the next M lines of the test case contains the labels of one of the pairs of crossroads connected by a lane.
Output
For each of the test cases, on a single line of the output, print the length of a maximal simple cycle.
Sample Input
1 7 8 3 4 1 4 1 3 7 1 2 7 7 5 5 6 6 2
Sample Output
4
Each of the M lanes of the Park of Polytechnic University of Bucharest connects two of the N crossroads of the park (labeled from 1 to N). There is no pair of crossroads connected by more than one lane and it is possible to pass from each crossroad to each other crossroad by a path composed of one or more lanes. A cycle of lanes is simple when passes through each of its crossroads exactly once.
The administration of the University would like to put on the lanes pictures of the winners of Regional Collegiate Programming Contest in such way that the pictures of winners from the same university to be on the lanes of a same simple cycle. That is why the administration would like to assign the longest simple cycles of lanes to most successful universities. The problem is to find the longest cycles? Fortunately, it happens that each lane of the park is participating in no more than one simple cycle (see the Figure).
The administration of the University would like to put on the lanes pictures of the winners of Regional Collegiate Programming Contest in such way that the pictures of winners from the same university to be on the lanes of a same simple cycle. That is why the administration would like to assign the longest simple cycles of lanes to most successful universities. The problem is to find the longest cycles? Fortunately, it happens that each lane of the park is participating in no more than one simple cycle (see the Figure).
Input
On the first line of the input file the number T of the test cases will be given. Each test case starts with a line with the positive integers N and M, separated by interval (4 <= N <= 4444). Each of the next M lines of the test case contains the labels of one of the pairs of crossroads connected by a lane.
Output
For each of the test cases, on a single line of the output, print the length of a maximal simple cycle.
Sample Input
1 7 8 3 4 1 4 1 3 7 1 2 7 7 5 5 6 6 2
Sample Output
4
思路是:先把图存下来,然后使用深度优先搜索计算最大环的长度。我这里使用了面向对象的方法,把无向图和环分别封装成一个类,无向图的对象需要调用方法完成无向图的初始化,而环的对象在构造时便使用深度优先搜索计算好了最大环的长度,而且用实例域记录了下来。所以输出时只要调用环的对象的getter方法就好~
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
int numberOfTestCases = scanner.nextInt();
for (int i = 0; i < numberOfTestCases; i++)
{
Graph graph = new Graph(scanner.nextInt());
int edge = scanner.nextInt();
for (int j = 0; j < edge; j++)
{
graph.addEdge(scanner.nextInt() - 1, scanner.nextInt() - 1); // 此处把输入纠正(1->0, 3->2, 14->13...)
}
Cycle cycle = new Cycle(graph);
System.out.println(cycle.getMaxLengthOfCycle());
}
class Graph
{
private final int V; // 顶点数目
private int E; // 边数目
private List[] adj; // 邻接表
public Graph(int v)
{
V = v;
E = 0;
adj = new List[v];
for (int i = 0; i < adj.length; i++)
{
adj[i] = new ArrayList<Integer>();
}
}
* 返回顶点数目
public int V()
{
return V;
}
* 返回边的数目
public int E()
{
return E;
}
public void addEdge(int v, int w)
{
E++;
adj[v].add(w);
adj[w].add(v);
}
public Iterable<Integer> adj(int v)
{
return adj[v];
}
}
class Cycle
{
private boolean[] marked; // 标记顶点是否已经访问过
private int[] number; // 每个顶点的序号,根据深度优先搜索的顺序编号
private int maxLengthOfCycle; // 最大环的长度
public Cycle(Graph graph)
{
marked = new boolean[graph.V()];
number = new int[graph.V()];
for (int i = 0; i < graph.V(); i++)
{
if (!marked[i])
{
number[i] = 1; // 根据深度优先搜索的顺序编号
dfs(graph, i, i);
}
}
}
* @param graph 要搜索的图
* @param v 要搜索的顶点
* @param u 要搜索的顶点的上一个顶点
private void dfs(Graph graph, int v, int u)
{
marked[v] = true;
for (Integer w : graph.adj(v))
{
if (!marked[w])
{
number[w] = number[v] + 1; // 根据深度优先搜索的顺序编号
dfs(graph, w, v);
}
else if (w != u) // 说明该图有环
{
if (maxLengthOfCycle < number[v] - number[w] + 1)
{
maxLengthOfCycle = number[v] - number[w] + 1;
}
}
}
}
public int getMaxLengthOfCycle()
{
return maxLengthOfCycle;
}
}