結果
問題 | No.1605 Matrix Shape |
ユーザー |
![]() |
提出日時 | 2021-07-16 21:55:43 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 226 ms / 2,000 ms |
コード長 | 2,578 bytes |
コンパイル時間 | 2,440 ms |
コンパイル使用メモリ | 202,784 KB |
最終ジャッジ日時 | 2025-01-23 01:45:30 |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 34 |
ソースコード
#define _USE_MATH_DEFINES#include <bits/stdc++.h>using namespace std;#define FOR(i,m,n) for(int i=(m);i<(n);++i)#define REP(i,n) FOR(i,0,n)#define ALL(v) (v).begin(),(v).end()using ll = long long;constexpr int INF = 0x3f3f3f3f;constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;constexpr double EPS = 1e-8;constexpr int MOD = 1000000007;// constexpr int MOD = 998244353;constexpr int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};constexpr int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1};template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; }template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; }struct IOSetup {IOSetup() {std::cin.tie(nullptr);std::ios_base::sync_with_stdio(false);std::cout << fixed << setprecision(20);}} iosetup;std::vector<int> eulerian_trail_in_directed_graph(const std::vector<std::vector<int>> &graph, int s = -1) {int n = graph.size(), edges = 0;std::vector<std::vector<int>> tmp(graph);std::vector<int> deg(n, 0);for (int i = 0; i < n; ++i) {deg[i] += tmp[i].size();for (int e : tmp[i]) --deg[e];edges += tmp[i].size();}int not0 = n - std::count(deg.begin(), deg.end(), 0);if (s == -1) {for (int i = 0; i < n; ++i) {if (not0 == 0) {if (!tmp[i].empty()) {s = i;break;}} else if (not0 == 2) {if (deg[i] == 1) {s = i;break;}}}if (s == -1) return {};}if (not0 == 0 || (not0 == 2 && deg[s] == 1)) {std::vector<int> res;std::function<void(int)> dfs = [&tmp, &res, &dfs](int ver) {while (!tmp[ver].empty()) {int nx = tmp[ver].back();tmp[ver].pop_back();dfs(nx);}res.emplace_back(ver);};dfs(s);if (res.size() == edges + 1) {std::reverse(res.begin(), res.end());return res;}}return {};}int main() {constexpr int L = 200000;int n; cin >> n;vector<vector<int>> graph(L + 1);int deg[L + 1]{};REP(i, n) {int h, w; cin >> h >> w;graph[h].emplace_back(w);++deg[h];--deg[w];}bool has_loop = true;REP(i, L + 1) has_loop &= deg[i] == 0;if (!eulerian_trail_in_directed_graph(graph).empty()) {if (has_loop) {int ans = 0;REP(i, L + 1) ans += !graph[i].empty();cout << ans << '\n';} else {cout << 1 << '\n';}} else {cout << 0 << '\n';}return 0;}