結果

問題 No.1553 Lovely City
ユーザー kwm_tkwm_t
提出日時 2021-06-19 14:30:28
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 405 ms / 2,000 ms
コード長 2,138 bytes
コンパイル時間 4,323 ms
コンパイル使用メモリ 246,308 KB
実行使用メモリ 30,424 KB
最終ジャッジ日時 2023-09-05 00:51:04
合計ジャッジ時間 15,998 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
8,132 KB
testcase_01 AC 5 ms
8,384 KB
testcase_02 AC 142 ms
27,340 KB
testcase_03 AC 5 ms
8,296 KB
testcase_04 AC 5 ms
8,188 KB
testcase_05 AC 5 ms
8,248 KB
testcase_06 AC 5 ms
8,440 KB
testcase_07 AC 5 ms
8,164 KB
testcase_08 AC 210 ms
20,048 KB
testcase_09 AC 238 ms
20,620 KB
testcase_10 AC 253 ms
24,212 KB
testcase_11 AC 168 ms
18,396 KB
testcase_12 AC 285 ms
26,944 KB
testcase_13 AC 182 ms
18,688 KB
testcase_14 AC 210 ms
20,048 KB
testcase_15 AC 192 ms
20,052 KB
testcase_16 AC 269 ms
21,856 KB
testcase_17 AC 181 ms
21,396 KB
testcase_18 AC 405 ms
30,336 KB
testcase_19 AC 388 ms
30,020 KB
testcase_20 AC 389 ms
30,424 KB
testcase_21 AC 383 ms
29,036 KB
testcase_22 AC 400 ms
28,740 KB
testcase_23 AC 397 ms
29,932 KB
testcase_24 AC 397 ms
29,404 KB
testcase_25 AC 401 ms
29,768 KB
testcase_26 AC 392 ms
28,092 KB
testcase_27 AC 399 ms
28,412 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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, set<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])].insert(u[i]);
		mp[uf.leader(u[i])].insert(v[i]);
	}
	vector<pair<int, int>>ans;
	rep(i, n) if (i == uf.leader(i)) {
		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 {
			vector<int>v;
			for (auto j : mp[i])v.push_back(j);
			rep(j, v.size())ans.push_back({ v[j] + 1,v[(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;
}
0