2013
11-13

# Cycles of Lanes

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).

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.

For each of the test cases, on a single line of the output, print the length of a maximal simple cycle.

1
7 8
3 4
1 4
1 3
7 1
2 7
7 5
5 6
6 2

4

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
import java.util.List;
public class Main {
private int n;//顶点个数
private boolean[] used;//节点状态,值为false的是未访问的
private List< ArrayList< Integer>> G;//邻接表
private int maxlen=0;//最小环的长度
private int[] num;//记录搜索到某顶点时已搜索过的顶点数

public Main(int n,List< ArrayList< Integer>> G){
this.n=n;
this.G=G;
used=new boolean[n+1];
num=new int[n+1];
}

private void dfs(int v, int t)  {

num[v] = t;  //搜索到v时,已搜索过的顶点数
used[v] = true;
int x = G.get(v).size();
for(int i = 0; i < x; i++){ //对V的每一个邻接点
if(!used[G.get(v).get(i)]){ //没有发现环
dfs(G.get(v).get(i), t + 1);
}
else  //发现环
{
if(maxlen < num[v] - num[G.get(v).get(i)] + 1)
maxlen = num[v] - num[G.get(v).get(i)] + 1;
}
}
}
public void go(){
for(int i = 1; i <= n; i++){ //遍历每一个顶点
if(!used[i])
dfs(i, 1);  //深度优先搜索
}
if(maxlen > 2) System.out.printf("%d\n", maxlen);
else System.out.println("0");
}

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t=sc.nextInt();
while(t-->0){
int n=sc.nextInt();
int m=sc.nextInt();

List< ArrayList< Integer>> G;
G = new ArrayList< ArrayList< Integer>>();//初始化邻接表
for(int i = 1;i<=n+1;i++)
for(int i=0;i< m;i++){
int u = sc.nextInt();
int v = sc.nextInt();
}

Main ma=new Main(n,G);
ma.go();

}
}
}

1. Pingback: bvlgari serpenti ladies Knockoff

2. Pingback: ring love gold Nachahmung

3. Good task for the group. Hold it up for every yeara??s winner. This is a excellent oppotunity for a lot more enhancement. Indeed, obtaining far better and much better is constantly the crucial. Just like my pal suggests on the truth about ab muscles, he just keeps obtaining much better.

4. a是根先忽略掉，递归子树。剩下前缀bejkcfghid和后缀jkebfghicd，分拆的原则的是每个子树前缀和后缀的节点个数是一样的，根节点出现在前缀的第一个，后缀的最后一个。根节点b出现后缀的第四个位置，则第一部分为四个节点，前缀bejk，后缀jkeb，剩下的c出现在后缀的倒数第2个，就划分为cfghi和 fghic，第3部分就为c、c