Communications network ccf

Question number: 201709-4
Questions Name: Communications network
time limit: 1.0s
Memory Limit: 256.0MB
Problem Description:
Problem Description
  A country military by N number departments, in order to improve security, is established between the department M of paths, each path pass in only one message, i.e., from a sector a to a sector b only by the passage of a to b is transmitted information. Information can be transmitted by way of the relay, i.e., if a can of information transmitted to B , B can pass information to the C , then a can pass information to the C . A message may eventually reach the destination through multiple transit.
  Due to security work done well, not all know each other all departments each other's existence. Only when the information can be transmitted directly or indirectly between the two departments, they did know each other each other's existence. Not himself know which department to tell between departments in other sectors.

  Figure above gives an example of a sector 4, FIG unidirectional edges represent paths. Sector 1 may send a message to all sectors, all sectors can receive messages 4 sectors, sector 1 and sector 4 so aware of the existence of all other sectors. Between sectors 2 and 3 do not in any way sectors may send a message, so that each sector 2 and sector 3 does not know each other's presence.
  Now I ask, how many departments know all N There is a division. Or, how many departments there are number of departments know (including myself) is just N .
Input Format
  The first line of input contains two integers N M , respectively, and the number of sectors unidirectional path. All departments from 1 to N label.
  Next M rows, each row two integers a b , represents a sector a to a sector b has a one-way path.
Output Format
  Output one line containing an integer that represents the answer.
Sample input
4 4
1 2
1 3
2 4
3 4
Sample Output
2
Sample Description
  Sectors 1 and 4 know the existence of all other sectors.
Evaluation scale cases and agreed with
  对于30%的评测用例,1 ≤  N ≤ 10,1 ≤  M ≤ 20;
  对于60%的评测用例,1 ≤  N ≤ 100,1 ≤  M ≤ 1000;
  对于100%的评测用例,1 ≤  N ≤ 1000,1 ≤  M ≤ 10000。

 

 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<cstring>
 4 #include<queue>
 5 
 6 #define INF 1000000000
 7 
 8 using namespace std;
 9 
10 vector<int> v[1100];
11 int isConnected[1100][1100];
12 int vis[1100];
13 
14 void dfs(int src, int cur)
15 {
16     vis[src] = 1;
17     isConnected[src][cur] = isConnected[cur][src] = 1;
18     
19     for(int i = 0; i < v[src].size(); ++i)
20     {
21         if(!vis[v[src][i]])
22         {
23             dfs(v[src][i], cur);
24         }
25             
26     }
27 }
28 
29 int main()
30 {
31      int n, m;
32      scanf("%d%d", &n, &m);
33      for(int i = 1; i <= m; ++i)
34      {
35          int a, b;
36          scanf("%d%d", &a, &b);
37          v[a].push_back(b);
38      }
39     
40     for(int i = 1; i <= n; ++i)
41     {
42         memset(vis, 0, sizeof(vis));
43         dfs(i, i);
44     }
45     
46     int ans = 0;
47     for(int i = 1; i <= n; ++i)
48     {
49         int j; 
50         for(j = 1; j <= n; ++j)
51         {
52             if(!isConnected[i][j])
53                 break;
54         }
55         if(j == n+1)
56             ++ans;
57         
58     }
59     
60     printf("%d", ans);
61     
62     
63     return 0;
64 }

 

Guess you like

Origin www.cnblogs.com/FengZeng666/p/11516209.html