Skip to content

Instantly share code, notes, and snippets.

@surinoel
Created July 10, 2019 00:43
Show Gist options
  • Save surinoel/f8a400f591df02047d06d36736aea275 to your computer and use it in GitHub Desktop.
Save surinoel/f8a400f591df02047d06d36736aea275 to your computer and use it in GitHub Desktop.
#include <queue>
#include <vector>
#include <string>
#include <cstring>
#include <iostream>
using namespace std;
#define max(n, m) n > m ? n : m
vector<int> mat[51];
int dist[51];
int main(void) {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
string s; cin >> s;
for (int j = 0; j < n; j++) {
if (s[j] == 'Y') {
mat[i].push_back(j + 1);
}
}
}
int ans = 0;
for (int i = 1; i <= n; i++) {
int tmp = 0;
memset(dist, -1, sizeof(dist));
queue<int> q;
q.push(i);
dist[i] = 0;
while (!q.empty()) {
int x = q.front();
q.pop();
if (dist[x] == 2) break; // 거리가 2인 정점이 나왔다는 것은 거리가 1인 것까지 모두
for (int k = 0; k < mat[x].size(); k++) {
int y = mat[x][k];
if (dist[y] != -1) continue;
dist[y] = dist[x] + 1;
tmp += 1;
q.push(y);
}
}
ans = max(tmp, ans);
}
cout << ans << '\n';
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment