結果

問題 No.1553 Lovely City
ユーザー kwm_tkwm_t
提出日時 2021-06-19 14:03:53
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 2,194 bytes
コンパイル時間 5,381 ms
コンパイル使用メモリ 246,640 KB
実行使用メモリ 814,432 KB
最終ジャッジ日時 2023-09-05 00:48:19
合計ジャッジ時間 8,548 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
8,176 KB
testcase_01 AC 4 ms
8,216 KB
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 MLE -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
権限があれば一括ダウンロードができます

ソースコード

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;
std::vector<int>topological_sort(int leader) {
	std::vector<int>h(mp[leader].size());
	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(n), v(n);
	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, 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