Question description
Floyd found a self-driving map from Lao Ma's drawer. There are now N (1 ≤ N ≤ 100) cities on the map, numbered 1, 2, ..., N in sequence . The cities are separated by M (1 ≤ M ≤ 4,500) unidirectional edges . Given the input M unidirectional edges (there are no self-loops and no repeated unidirectional edges on the map), can you help Floyd determine the number N* of bustling cities ? The definition of a prosperous city is: if a city x has a topological relationship with other N-1 cities , then we can call this city x a prosperous city ; the definition of the topological relationship is: if there is a topological relationship between two cities x and y At least one one-way path , such as x → … → y , or y
→ … → _ _ _ _ _ _
_ _ _ _ _ _
enter
The input only contains a set of data:
the first line contains two integers N and M , separated by spaces;
the subsequent M lines (that is, lines 2 to M+1 ) input each unidirectional edge , each line contains two The integers x and y separated by spaces represent the existence of a one- way edge starting from city x and ending in city y (in order to simplify the description of the problem, you can think of the length of the one-way edge xy as any value. It simply serves the purpose of the role of connecting cities) .
output
The output contains unique row integers, the values of N* .
Sample
Sample input copy | Sample output copy |
---|---|
5 5 4 3 4 2 3 2 1 2 2 5 |
2 |
Sample prompts
There are two prosperous cities among the five cities in the given example:
City 1 can only establish topological relationships with City 2 (1 → 2) and City 5 (1 → 2 → 5), so City 1 is not a prosperous city
; 2 can establish a topological relationship with the other four cities, namely city 1 (1 → 2), city 3 (3 → 2), city 4 (4 → 2 or 4 → 3 → 2), and city 5 (2 → 5) , so City 2 is a prosperous city;
City 3 is not a prosperous city because it cannot establish a topological relationship with City 1;
City 4 is not a prosperous city because it cannot establish a topological relationship with City 1;
City 5 is a prosperous city because it can Establish topological relationships with any other city.
#include<iostream>
#include<cstring>
using namespace std;
int n,m,x,y,ans=0,res=0,g[1010][1010];
void floyd(){
for(int k=1;k<=n;k++)
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++){
if(g[i][k]==1&&g[k][j]==1)
g[i][j]=1;
}
}
int main(){
cin>>n>>m;
memset(g,0,sizeof(g));
while(m--){
cin>>x>>y;
g[x][y]=1;
}
floyd();
for(int i=1;i<=n;i++){
ans=0;
for(int j=1;j<=n;j++)
{
if(g[i][j]==1||g[j][i]==1)
ans++;
}
if(ans==n-1) res++;
}
cout<<res<<endl;
}