結果

問題 No.1553 Lovely City
ユーザー kwm_tkwm_t
提出日時 2021-06-19 14:31:50
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 290 ms / 2,000 ms
コード長 2,175 bytes
コンパイル時間 4,310 ms
コンパイル使用メモリ 247,108 KB
実行使用メモリ 24,088 KB
最終ジャッジ日時 2023-09-05 00:51:27
合計ジャッジ時間 13,505 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
8,156 KB
testcase_01 AC 4 ms
8,232 KB
testcase_02 AC 290 ms
24,088 KB
testcase_03 AC 5 ms
8,312 KB
testcase_04 AC 5 ms
8,396 KB
testcase_05 AC 5 ms
8,156 KB
testcase_06 AC 4 ms
8,140 KB
testcase_07 AC 5 ms
8,180 KB
testcase_08 AC 140 ms
16,596 KB
testcase_09 AC 145 ms
16,996 KB
testcase_10 AC 165 ms
19,196 KB
testcase_11 AC 111 ms
15,160 KB
testcase_12 AC 206 ms
20,708 KB
testcase_13 AC 121 ms
15,848 KB
testcase_14 AC 132 ms
16,328 KB
testcase_15 AC 132 ms
16,216 KB
testcase_16 AC 159 ms
17,712 KB
testcase_17 AC 126 ms
17,552 KB
testcase_18 AC 236 ms
23,368 KB
testcase_19 AC 234 ms
22,756 KB
testcase_20 AC 233 ms
23,312 KB
testcase_21 AC 233 ms
21,964 KB
testcase_22 AC 230 ms
21,652 KB
testcase_23 AC 230 ms
22,732 KB
testcase_24 AC 232 ms
22,460 KB
testcase_25 AC 234 ms
22,652 KB
testcase_26 AC 226 ms
21,140 KB
testcase_27 AC 227 ms
21,364 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, 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;
}
0