CCF authentication 201709-4 Communication Network Java implementation

Source: Internet
Author: User

Question number: 201709-4
Question Name: Communication network
Time limit: 1.0s
Memory Limit: 256.0MB
Problem Description: Problem describes the army of a country byNIn order to improve security, the department has established aMPath, each path can only be transmitted in one direction, that is, a section from the departmentato the DepartmentbThe pathway can only be accessed byaTobPassing information. Information can be transferred in a transit way, i.e. ifaThe ability to pass information tobbYou can also pass information toC, youaThe ability to pass information toC。 A message may eventually reach the destination through multiple relays.
Because the secrecy is doing well, not all departments are aware of each other's existence. Only when the two departments can directly or indirectly transfer information, they know each other's existence. Departments do not know which departments to tell other departments.

Gives an example of 4 departments, the one-way side of the diagram represents the pathway. Department 1 can send messages to all departments, department 4 can receive messages from all departments, so department 1 and Department 4 know the existence of all other departments. There is no way to send messages between Department 2 and department 3, so department 2 and department 3 don't know each other's existence.
Now excuse me, how many departments are aware of allNThe presence of a department. Or, how many departments are aware of the number of departments (including themselves) that happens to beN。 Input format the first line entered contains two integersN,M, representing the number of departments and the number of one-way access. All departments from 1 toNLabel.
NextMRows, two integers per linea,b, indicating that the departmentato the DepartmentbThere is a one-way access. The output format outputs a line that contains an integer that represents the answer. Sample Input 4 4
0 S
1 3
2 4
3 4 Sample Output 2 Sample Description Department 1 and Department 4 know the existence of all other departments. Evaluation use case size and conventions for 30% of the evaluation case, 1≤N≤10,1≤M≤20;
For 60% of the evaluation use cases, 1≤N≤100,1≤M≤1000;
For 100% of the evaluation use cases, 1≤N≤1000,1≤M≤10000.

This is a depth-first search of graph theory.

To interpret it as:

To enter a graph, we determine that there are several nodes that can be accessed by all other nodes and several nodes.

The topic is a single map, but the problem is that you can access all nodes and can be accessed by all nodes, so there is no difference between the two-way graph,

It is possible to understand which nodes in the diagram have access to all other nodes.

Need to use the idea of depth-first traversal, specifically this implementation:

There are n points m roads

1 Set up an adjacency table: an array of n lengths, a list of each position in the array, and the list of all nodes that can be reached from point Line[i in the chain.

2 Create a n*n two-dimensional table graph represents the entire graph, we want to do a depth-first traversal of the adjacency table line,

In line root from 1 to n take I as root node number root, get line[root] linked list,

All node I in the list can be reached from root, and we will mark 1 in Graph[root][i] and graph[i][root] to indicate that they together with

At the same time I can reach the node, root can also be reached indirectly, so we again on the line[i] list of all the node markings on the graph to be labeled connected.

To prevent the loop path in the diagram, I use a visited table to mark the same root for a deep traversal, access to a node I will set visited[i] to 1 means access, skip access

Here we want to be in depth first, so we have to recursion inside the loop.

Finally, we're going to figure out a row that all the data is 1 and that means he's connected to all the dots.

Java code:

1 Importjava.util.ArrayList;2 Importjava.util.List;3 ImportJava.util.Scanner;4 5  Public classmain{6      PublicScanner fin;//Standard input7      Public intN,m;//Number of points and number of paths8      Public intVisited[];//Mark whether each depth search iterates over the target node mode infinite recursion9      PublicList<integer>[] Line;//I list all node numbers that I can reachTen      Public int[] graph;//Two-dimensional table I J Mark I and J have a pathway between One      Public intRoot//record the root node of each traversal A      Public intcount=0;//The end result Oh -      -      Public Static voidMain (string[] args) { the         NewMain (). run (); -     } -      Public voidrun () { - init (); +         //Enter each route -          for(inti=1;i<=m;i++) { +             intA =fin.nextint (); A             intb =fin.nextint (); atLine[a].add (b);//a node can reach the B node -         } -         //Depth-first traversal of each node, updating the two-dimensional table graph sets the two-dimensional table with paths between the I and J two nodes to a corresponding position of 1 -          for(inti=1;i<=n;i++) { -             //Initializes a visited array each time a child node is traversed from the root node -visited =New int[N+1]; inRoot =i; - DFS (i); to         } +         //statistics to know the number of nodes for all other nodes -          for(inti=1;i<=n;i++) { the              for(intj=1;j<=n;j++) { *                 if(graph[i][j]==0) { $                      Break;Panax Notoginseng                 } -                 if(j==N) { theCount + +; +                 } A             } the         } + System.out.println (count); -          $          $     } -      Public voidDfsintcur) { -         //the root node and the current child node are able to access theGraph[root][cur] = 1; -Graph[cur][root] = 1;WuyiVisited[cur] = 1; the         //the node list traversal that can be reached on the CUR node -          for(intI=0;i<line[cur].size (); i++) { Wu             if(Visited[line[cur].get (i)]==0) {//If the current child node has not been accessed - Dfs (Line[cur].get (i)); About             } $         } -     } -      -      Public voidinit () { AFin =NewScanner (system.in); +n =fin.nextint (); them =fin.nextint (); -visited =New int[N+1]; $line =NewList[n+1]; the          for(inti=1;i<=n;i++) { theLine[i] =NewArraylist<>(); the         } theGraph =New int[N+1] [N+1]; -          in     } the      the}

CCF authentication 201709-4 Communication Network Java implementation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.