結果
| 問題 |
No.1553 Lovely City
|
| コンテスト | |
| ユーザー |
kwm_t
|
| 提出日時 | 2021-06-19 14:31:50 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 279 ms / 2,000 ms |
| コード長 | 2,175 bytes |
| コンパイル時間 | 5,249 ms |
| コンパイル使用メモリ | 249,488 KB |
| 実行使用メモリ | 24,392 KB |
| 最終ジャッジ日時 | 2024-06-22 22:02:26 |
| 合計ジャッジ時間 | 12,430 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 26 |
ソースコード
#include "bits/stdc++.h"
#include "atcoder/all"
using namespace std;
using namespace atcoder;
//using mint = modint1000000007;
//const int mod = 1000000007;
//using mint = modint998244353;
//const int mod = 998244353;
//const int INF = 1e9;
//const long long LINF = 1e18;
//const bool debug = false;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i,l,r)for(int i=(l);i<(r);++i)
#define rrep(i, n) for (int i = (n-1); i >= 0; --i)
#define rrep2(i,l,r)for(int i=(r-1);i>=(l);--i)
#define all(x) (x).begin(),(x).end()
#define allR(x) (x).rbegin(),(x).rend()
#define endl "\n"
#define P pair<int,int>
template<typename A, typename B> inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; }
template<typename A, typename B> inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; }
vector<int>to[200005];
map<int, vector<int>>mp;
int h[200005];
std::vector<int>topological_sort(int leader) {
for (auto v : mp[leader]) for (auto e : to[v]) h[e]++;
std::stack<int>st;
std::vector<int>ans;
for (auto v : mp[leader])if (h[v] == 0) st.push(v);
while (!st.empty()) {
int v = st.top(); st.pop();
ans.push_back(v);
for (auto u : to[v]) {
--h[u];
if (h[u] == 0) st.push(u);
}
}
return ans;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m; cin >> n >> m;
vector<int>u(m), v(m);
dsu uf(n);
rep(i, m) {
cin >> u[i] >> v[i];
u[i]--; v[i]--;
to[u[i]].push_back(v[i]);
uf.merge(u[i], v[i]);
}
rep(i, m) {
mp[uf.leader(u[i])].push_back(u[i]);
mp[uf.leader(u[i])].push_back(v[i]);
}
vector < pair<int, int>>ans;
rep(i, n) if (i == uf.leader(i)) {
sort(all(mp[i]));
mp[i].erase(unique(all(mp[i])), mp[i].end());
auto v = topological_sort(i);
if (mp[i].size() == v.size()) {
rep(j, (int)v.size() - 1) ans.push_back({ v[j] + 1,v[j + 1] + 1 });
}
else {
rep(j, mp[i].size()) ans.push_back({ mp[i][j] + 1,mp[i][(j + 1) % mp[i].size()] + 1 });
}
/*rep(i, v.size()) {
cout << v[i] << " ";
}
cout << endl;*/
}
cout << ans.size() << endl;
rep(i, ans.size()) cout << ans[i].first << " " << ans[i].second << endl;
return 0;
}
kwm_t