当前位置: 代码迷 >> 综合 >> acg039B - Graph Partition
  详细解决方案

acg039B - Graph Partition

热度:103   发布时间:2023-11-24 00:42:53.0

https://atcoder.jp/contests/agc039/tasks/agc039_b

判断是否存在奇数点环再folyd

#include <bits/stdc++.h>using namespace std;int main() {ios::sync_with_stdio(false);cin.tie(0);int n;cin >> n;vector<string> g(n);for (int i = 0; i < n; i++) {cin >> g[i];}int ans = 0;for (int st = 0; st < n; st++) {vector<int> dist(n, -1);vector<int> que(1, st);dist[st] = 0;for (int b = 0; b < (int) que.size(); b++) {for (int i = 0; i < n; i++) {if (g[que[b]][i] == '1' && dist[i] == -1) {que.push_back(i);dist[i] = dist[que[b]] + 1;}}}for (int i = 0; i < n; i++) {for (int j = 0; j < n; j++) {if (g[i][j] == '1' && dist[i] == dist[j]) {cout << -1 << '\n';return 0;}}}int mx = *max_element(dist.begin(), dist.end());ans = max(ans, mx);}cout << ans + 1 << '\n';return 0;
}

 

  相关解决方案