結果
問題 | No.1605 Matrix Shape |
ユーザー |
|
提出日時 | 2021-07-16 21:44:56 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 44 ms / 2,000 ms |
コード長 | 938 bytes |
コンパイル時間 | 547 ms |
コンパイル使用メモリ | 67,700 KB |
実行使用メモリ | 6,016 KB |
最終ジャッジ日時 | 2024-07-06 08:45:37 |
合計ジャッジ時間 | 2,507 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 34 |
ソースコード
#include <iostream>#include <algorithm>using namespace std;int dg[200009], ds[200009], sz[200009];bool f[200009];const int m = 200000;int uf(int n){if (ds[n] == n) return n;return ds[n] = uf(ds[n]);}int main(){ios::sync_with_stdio(false);cin.tie(0);int n; cin >> n;for (int i = 1; i <= m; i++) {ds[i] = i;sz[i] = 1;}for (int i = 1; i <= n; i++) {int a, b; cin >> a >> b;f[a] = f[b] = true;dg[a]++; dg[b]--;int ua = uf(a), ub = uf(b);if (ua != ub) {ds[ub] = ua;sz[ua] += sz[ub];}}int c = 0, s0 = 0, s1 = 0, sp = 0;for (int i = 1; i <= m; i++) {if (ds[i] == i && f[i]) {c++;sp = sz[i];}if (dg[i] == -1) s0++;if (dg[i] == 1) s1++;if (dg[i] < -1 || dg[i] > 1 || c > 1) {cout << 0 << '\n';return 0;}}if (s0 == 1 && s1 == 1)cout << 1 << '\n';else if (s0 == 0 && s1 == 0)cout << sp << '\n';elsecout << 0 << '\n';return 0;}