Problem Description
A network in Country X uses several lines to connect several nodes. Communication between nodes is bidirectional. For security reasons, an important packet must be forwarded exactly twice to its destination. The packet may be generated at any node. We need to know how many different forwarding paths there are in the network.
Source and destination addresses can be the same, but intermediate nodes must be different.
The network shown in the figure below.
1 - > 2 - > 3 - > 1 is allowed
1 - > 2 - > 1 - > 2 or 1 - > 2 - > 3 - > 2 are illegal.
Input format
The first behavior of input data is two integers N M, which represent the number of nodes and the number of connected lines (1 <=N<=10000; 0 <=M<=100000).
Next there are M rows, two integers u and V for each behavior, indicating that the nodes u and V are connected (1 < = u, V < = N, u! = v).
The input data guarantees that at most one edge of any two points is connected, and there is no double edge and self-ring.
Output format
Output an integer representing the number of paths that meet the requirements.
Sample input 1
3 3
1 2
2 3
1 3
Sample output 1
6
Sample input 2
4 4
1 2
2 3
3 1
1 4
Sample output 2
10
Ideas:
In fact, finding different paths is a typical deep search problem, and its answer tree has only four layers. It shouldn't be very slow to use deep search. But the requirements of the topic are not all different. In essence, it means that we can't go back, that is, a node coming from the father's node, and we can't go back to the father's next step. Nodes, this is not allowed, so we can not simply set up a visited [] array to determine whether a point has been visited (if so, it becomes four points are different).
In fact, as long as we pass in a parameter last at each dfs, we name a father node number, because it is an undirected graph, so the shop traversal adjacent to the child node will certainly contain the father node. If we encounter last, we can skip it, and we do not need a vistied [] array (but, say something like that. If the fruit diagram is a tree, then according to the above bfs, the four points must be different.
Code:
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main
{
static List<Integer> list[];
static int count=0;//Statistics of different numbers
public static void main(String...args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int m=sc.nextInt();
list=(ArrayList<Integer>[])new ArrayList[n+1];
for(int i=1;i<=n;i++)
list[i]=new ArrayList<>();
for(int i=0;i<m;i++)
{
int o1=sc.nextInt();
int o2=sc.nextInt();
list[o1].add(o2);
list[o2].add(o1);//Map building
}
for(int i=1;i<=n;i++)For each different starting point, we do it once bfs,because4The path of a point is directed.
{
find(list[i],i,0,0);
}
System.out.println(count);
}
static void find(List<Integer> l,int o,int cur,int last)//o denotes the adjacent table of which point l is, cur denotes the current level, last denotes which node I came from.
{
if(o==last)return;//Just like the previous point, go back directly.
if(cur==3)//Because it starts at zero, when cur equals three, it's equivalent to fourth.
{
count++;//As long as this store is not equal to last, add one
return;
}
for(int i=0;i<l.size();i++)
{
int p=l.get(i);
if(p==last)
continue;
find(list[p],p,cur+1,o);
}
}
}
When it's a pity, it's overtime. Think about it carefully for a while. Do you have to go to the fourth floor? In fact, as long as you go to the third floor, then subtract 1 from the number of points starting from the third floor is all the possible numbers on the fourth floor, subtract 1 because this point must contain last, just subtract it.
Optimize:
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main
{
static List<Integer> list[];
static int count=0;
public static void main(String...args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int m=sc.nextInt();
list=(ArrayList<Integer>[])new ArrayList[n+1];
for(int i=0;i<m;i++)
{
int o1=sc.nextInt();
int o2=sc.nextInt();
if(list[o1]==null)
list[o1]=new ArrayList<>();
if(list[o2]==null)
list[o2]=new ArrayList<>();
list[o1].add(o2);
list[o2].add(o1);
}
for(int i=1;i<=n;i++)
{
if(list[i]!=null)
find(list[i],i,0,0);
}
System.out.println(count);
}
static void find(List<Integer> l,int o,int cur,int last)
{
if(o==last)return;
if(cur==2)//The only difference is that I have a good understanding of ___________.
{
count+=l.size()-1;
return;
}
for(int i=0;i<l.size();i++)
{
int p=l.get(i);
if(p==last)
continue;
find(list[p],p,cur+1,o);
}
}
}
The above code ac
peace&love