#include <string>
#include <vector>
#include <iostream>
using namespace std;
 
int graph[110][110];
int N;
int ans;
 
void floyd()
{
    for(int k = 1; k <= N; k++)
    {
        for(int i = 1; i <= N; i++)
        {
            for(int j = 1; j <= N; j++)
            {
                if(graph[i][k] == 1 && graph[k][j] == 1)
                {
                    graph[i][j] = 1;
                }
            }
        }
    }
}
 
int solution(int n, vector<vector<int>> results) 
{
    N = n;
    
    for(int i = 0; i < results.size(); i++)
    {
        graph[results[i][0]][results[i][1]] = 1;
    }
    
    floyd();
    
    for(int i = 1; i <= N; i++)
    {
        int cnt = 0;
        
        for(int j = 1; j <= N; j++)
        {
            if(graph[i][j] == 1 || graph[j][i] == 1)
            {
                cnt++;
            }
        }
        
        if(cnt == N-1)
        {
            ans++;
        }
    }
    
    return ans;
}
cs

+ Recent posts