結果
問題 | No.1553 Lovely City |
ユーザー | shimarut |
提出日時 | 2021-06-19 14:32:22 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 389 ms / 2,000 ms |
コード長 | 3,867 bytes |
コンパイル時間 | 3,788 ms |
コンパイル使用メモリ | 192,540 KB |
実行使用メモリ | 24,540 KB |
最終ジャッジ日時 | 2024-06-22 22:02:41 |
合計ジャッジ時間 | 14,363 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,816 KB |
testcase_01 | AC | 2 ms
6,940 KB |
testcase_02 | AC | 42 ms
21,308 KB |
testcase_03 | AC | 2 ms
6,940 KB |
testcase_04 | AC | 2 ms
6,944 KB |
testcase_05 | AC | 3 ms
6,944 KB |
testcase_06 | AC | 2 ms
6,940 KB |
testcase_07 | AC | 2 ms
6,940 KB |
testcase_08 | AC | 265 ms
15,348 KB |
testcase_09 | AC | 305 ms
15,956 KB |
testcase_10 | AC | 314 ms
20,276 KB |
testcase_11 | AC | 224 ms
14,504 KB |
testcase_12 | AC | 299 ms
21,816 KB |
testcase_13 | AC | 245 ms
14,056 KB |
testcase_14 | AC | 249 ms
15,668 KB |
testcase_15 | AC | 230 ms
16,036 KB |
testcase_16 | AC | 285 ms
17,016 KB |
testcase_17 | AC | 222 ms
16,628 KB |
testcase_18 | AC | 383 ms
24,408 KB |
testcase_19 | AC | 386 ms
24,412 KB |
testcase_20 | AC | 384 ms
24,540 KB |
testcase_21 | AC | 389 ms
24,452 KB |
testcase_22 | AC | 379 ms
24,416 KB |
testcase_23 | AC | 378 ms
24,460 KB |
testcase_24 | AC | 378 ms
24,404 KB |
testcase_25 | AC | 379 ms
24,388 KB |
testcase_26 | AC | 378 ms
24,408 KB |
testcase_27 | AC | 385 ms
24,540 KB |
ソースコード
#pragma region #define _USE_MATH_DEFINES #include <iostream> #include <string> #include <algorithm> #include <cmath> #include <cstdlib> #include <vector> #include <map> #include <queue> #include <stack> #include <set> #include <list> #include <iomanip> #include <cstdint> #include <bitset> #include <sstream> #include <regex> #include <fstream> #include <array> #include <atcoder/all> using namespace atcoder; using mint = modint1000000007; //using mint = modint998244353; //using mint = modint; #pragma region using using namespace std; typedef long long ll; using vi = vector<int>; using vvi = vector<vi>; using vl = vector<ll>; using vvl = vector<vl>; using vmint = vector<mint>; using vvmint = vector<vmint>; using pint = pair<int, int>; using pll = pair<ll, ll>; using vpint = vector<pint>; using vvpint = vector<vpint>; using vd = vector<double>; using vvd = vector<vd>; //#define rep(i, s, e) for (int(i) = (s); (i) < (e); ++(i)) #define rep(i, e) for (int(i) = 0; (i) < (e); ++(i)) #define rrep(i, s) for (int(i) = (s) - 1; (i) >= 0; --(i)) #define all(x) x.begin(),x.end() #define rall(x) x.rbegin(),x.rend() #pragma endregion #pragma region UnionFind struct UnionFind { vector<int> par; UnionFind(int n) : par(n, -1) {} void init(int n) { par.assign(n, -1); } int root(int x) { if (par[x] < 0) return x; else return par[x] = root(par[x]); } bool issame(int x, int y) { return root(x) == root(y); } bool merge(int x, int y) { x = root(x); y = root(y); if (x == y) return false; if (par[x] > par[y]) swap(x, y); par[x] += par[y]; par[y] = x; return true; } int size(int x) { return -par[root(x)]; } }; #pragma endregion #pragma region GCD ll gcd(ll a, ll b) { if (b == 0)return a; return gcd(b, a%b); } #pragma endregion #pragma region LCM ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } #pragma endregion #pragma region chmin template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } #pragma endregion #pragma region chmax template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } #pragma endregion #pragma region Dijkstra vl dijkstra(vector<vector<pair<int, ll>>> v, int s) { ll INF = 1e18; int MAX = 1e6; vl res(MAX, INF); priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> q; q.push({ 0,s }); while (!q.empty()) { int now; ll d; tie(d, now) = q.top(); q.pop(); if (!chmin(res[now], d))continue; for (auto p : v[now]) { int next; ll c; tie(next, c) = p; if (res[next] <= res[now] + c)continue; q.push({ res[now] + c,next }); } } return res; } #pragma endregion #pragma region degからrad double dtor(double deg) { return deg * M_PI / 180; } #pragma endregion #pragma region radからdeg double rtod(double r) { return r * 180 / M_PI; } #pragma endregion #pragma region グリッド内チェック bool out(int x, int y, int h, int w) { if (x < 0 || h <= x || y < 0 || w <= y)return true; else return false; } #pragma endregion #pragma endregion int main() { int n, m; cin >> n >> m; dsu d(n); vvi v(n); rep(i, m) { int a, b; cin >> a >> b; --a, --b; d.merge(a, b); v[a].push_back(b); } auto g = d.groups(); vpint res; vi in(n); for (auto c : g) { for (int x : c)for (int y : v[x])++in[y]; queue<int> q; for (int x : c)if (in[x] == 0)q.push(x); vi did; while (!q.empty()) { int now = q.front(); did.push_back(now + 1); q.pop(); for (int next : v[now]) { --in[next]; if (in[next] == 0)q.push(next); } } if (did.size() == (int)c.size())rep(i, (int)did.size() - 1)res.push_back({ did[i],did[i + 1] }); else rep(i, (int)c.size())res.push_back({ c[i] + 1,c[(i + 1) % c.size()] + 1 }); } cout << res.size() << endl; for (auto p : res)cout << p.first << " " << p.second << endl; }