CCF-CSP 201709-4 communication network, ccf-csp201709-4

Source: Internet
Author: User

CCF-CSP 201709-4 communication network, ccf-csp201709-4

Problem description
The army of a country is composed of N departments. To improve security, M channels are established between departments. Each channel can only transmit information in one way, that is, a channel from department a to department B can only transmit information from a to B. Information can be transmitted in transit mode, that is, if a can pass the information to B and B can pass the information to c, a can pass the information to c. A piece of information may eventually reach the destination through multiple transfers.
Since the confidentiality work is well done, not all departments know each other's existence. Only when two departments can directly or indirectly transmit information between them can they know each other's existence. Departments do not tell other departments that they know.

An example of four departments is given. One-way edge in the figure indicates a path. Department 1 can send messages to all departments, and Department 4 can receive messages from all departments, so department 1 and Department 4 know the existence of all other departments. No message can be sent between Department 2 and department 3. Therefore, Department 2 and department 3 do not know each other.
How many departments know the existence of all N departments. Or, the number of departments (including their own) knows exactly N.
Input Format
The first line contains two integers, N and M, indicating the number of departments and the number of one-way channels. Number of all departments from 1 to N.
In the next M row, each row has two integers, a and B, indicating that Department a has a one-way path to department B.
Output Format
The output line contains an integer indicating the answer.
Sample Input
4
1 2
1 3
2 4
3 4
Sample output
2
Example
Department 1 and Department 4 know the existence of all other departments.
Scale and conventions of evaluation cases
For 30% of evaluation cases, 1 ≤ N ≤ 10, 1 ≤ M ≤ 20;
For 60% of evaluation cases, 1 ≤ N ≤ 1000, 1 ≤ M ≤;
For 100% of evaluation cases, 1 ≤ N ≤ 10000, 1 ≤ M ≤.

Solution:

Use an isconnect two-dimensional array to store whether the two sides can reach each other. The value can be 1 and cannot be 0. Then, use dfs to traverse all vertices,

First of all, it is declared that dfs will facilitate all the points of intercommunication with it, right! <*-*>

The Code is as follows:

Import java. util. collections;

Public class communication1 {

Static int isconnect [] [] = new int [1001] [1001];

/**
* @ Param args
*/
Public static void main (String [] args ){
// TODO Auto-generated method stub
Pipeline SC = new pipeline (System. in );
Int result = 0;
Int n = SC. nextInt ();
Int m = SC. nextInt ();
Int a [] [] = new int [m] [2];
For (int I = 0; I <m; I ++ ){
A [I] [0] = SC. nextInt ();
A [I] [1] = SC. nextInt ();
}

For (int v = 1; v <= n; v ++ ){
Int book [] = new int [n + 1];
Dfs (v, a, n, v, book, m );
}
For (int I = 1; I <= n; I ++ ){
For (int j = 1; j <= n; j ++ ){
If (isconnect [I] [j] = 0)
Break;
If (j = n) // locate the vertex in the matrix that is connected to all other vertices.
Result ++;
}

}
System. out. println (result );

}

Private static void dfs (int v, int [] [] a, int n, int cur, int book [], int m ){
// TODO Auto-generated method stub
Isconnect [v] [cur] = isconnect [cur] [v] = 1;
For (int j = 0; j <m; j ++ ){
If (a [j] [0] = cur & book [a [j] [1] = 0 ){
Book [cur] = 1; // mark
Cur = a [j] [1]; // move the vertex
Isconnect [v] [cur] = isconnect [cur] [v] = 1;
Dfs (v, a, n, cur, book, m); // You Can unmark or unmark
Cur = a [j] [0]; // return to the previous starting point
}
}
}
}
But unfortunately, it is too complicated to get 60 points.

Solution 2:

Solution: using the concept of the adjacent table, you can first understand the basic concepts of the adjacent table if you do not understand it <*-*>. The adjacent table has advantages for sparse graphs, and then use dfs to traverse.

The Code is as follows:

Import java. util. collections;
Import java. util. ArrayList;

Public class communication {

Static int [] [] isconnect = new int [1, 1024] [2, 1024];
@ SuppressWarnings ("rawtypes ")
Static ArrayList [] arraylist = new ArrayList [1, 1024];
@ SuppressWarnings ("unchecked ")
Public static void main (String [] args ){
Pipeline SC = new pipeline (System. in );
Int result = 0;
Int n = SC. nextInt ();
Int m = SC. nextInt ();
For (int I = 1; I <= n; I ++ ){
Arraylist [I] = new ArrayList <Integer> ();
}
For (int I = 0; I <m; I ++ ){
Int s, e;
S = SC. nextInt ();
E = SC. nextInt ();
Arraylist [s]. add (e); // This graph is a directed graph. The subscript of an array is the starting point. The element in the corresponding set is the end point connected to it.
}
For (int I = 1; I <= n; I ++) {// traverse each vertex as the starting point
Int [] book = new int [n + 1];
Dfs (book, I );
}
For (int I = 1; I <= n; I ++ ){
Int j;
For (j = 1; j <= n; j ++ ){
If (isconnect [I] [j] = 0 ){
Break;
}
If (j = n ){
Result ++;
}
}
}
System. out. println (result );
}

Public static void dfs (int book [], int cur, int v, int mid ){
Book [cur] = 1;
Isconnect [v] [cur] = isconnect [cur] [v] = 1;
For (int I = 0; I <arraylist [cur]. size (); I ++ ){
If (book [Integer. parseInt (String. valueOf (arraylist [cur]. get (I)] = 0 ){
Mid = cur;
Cur = Integer. parseInt (String. valueOf (arraylist [cur]. get (I )));
Dfs (book, cur, v, mid); // There is no need to unmark it because we want
Cur = mid;
}
}
}
}

This reduces the complexity of the algorithm and scores 100 points! <*-*>

 

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.