結果

問題 No.1553 Lovely City
ユーザー 👑 potato167potato167
提出日時 2021-06-18 22:47:33
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,023 bytes
コンパイル時間 2,969 ms
コンパイル使用メモリ 218,644 KB
実行使用メモリ 38,904 KB
最終ジャッジ日時 2023-09-04 23:54:26
合計ジャッジ時間 11,194 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
12,252 KB
testcase_01 AC 7 ms
12,428 KB
testcase_02 AC 104 ms
36,492 KB
testcase_03 AC 8 ms
12,500 KB
testcase_04 AC 6 ms
12,376 KB
testcase_05 AC 7 ms
12,280 KB
testcase_06 AC 6 ms
12,664 KB
testcase_07 AC 7 ms
12,600 KB
testcase_08 AC 138 ms
27,032 KB
testcase_09 WA -
testcase_10 AC 177 ms
33,912 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 121 ms
24,688 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 127 ms
29,224 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
using std::cout;
using std::cin;
using std::endl;
using ll=long long;
using ld=long double;
ll I=1167167167167167167;
ll Q=998244353;
#define rep(i,a) for (ll i=0;i<a;i++)
template<class T> using _pq = priority_queue<T, vector<T>, greater<T>>;
template<class T> ll LB(vector<T> &v,T a){return lower_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> ll UB(vector<T> &v,T a){return upper_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}else return 0;}
template<class T> bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}else return 0;}
template<class T> void So(vector<T> &v) {sort(v.begin(),v.end());}
template<class T> void Sore(vector<T> &v) {sort(v.begin(),v.end(),[](T x,T y){return x>y;});}
template<class T> void print_tate(vector<T> &v) {rep(i,v.size()) cout<<v[i]<<"\n";}
void yneos(bool a){if(a) cout<<"Yes"<<"\n"; else cout<<"No"<<"\n";}

int order[200200];
vector<vector<int>> G(200200);
vector<vector<int>> H(200200);
int ran[200200];
int r=0;

void dfs(int a,int &now){
	if(order[a]!=-1) return ;
	order[a]=-2;
	for(auto x:G[a]){
		dfs(x,now);
	}
	order[a]=now;
	now++;
}

void dfs_2(int a){
	if(ran[a]!=-1) return ;
	ran[a]=r;
	for(auto x:H[a]) dfs_2(x);
}

namespace po167{
struct UFtree
{
	std::vector<int> par;
	std::vector<int> wei;
	UFtree(int n):par(n),wei(n){
		for(int i=0;i<n;i++){
			wei[i]=1,par[i]=i;
		}
	}
	//根っこを返す
	int root(int a){
		if(a==par[a]) return a;
		return par[a]=root(par[a]);
	}
	//trueなら1,falseなら0
	int same(int a,int b){
		if(root(a)==root(b)) return 1;
		else return 0;
	}
	//a,bが違う根っこの元なら結合する,結合したらtrueを返す
	bool unite(int a,int b){
		a=root(a),b=root(b);
		if(a==b) return false;
		if(wei[a]<wei[b]) std::swap(a,b);
		par[b]=a;
		wei[a]+=wei[b];
		return true;
	}
};
}
using po167::UFtree;

//おちこんだりもしたけれど、私はげんきです。
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int N,M;
	cin>>N>>M;
	UFtree A(N);
	rep(i,M){
		int a,b;
		cin>>a>>b;
		a--,b--;
		if(a==b) continue;
		G[a].push_back(b);
		H[b].push_back(a);
		A.unite(a,b);
	}
	vector<pair<int,int>> ans;
	rep(i,N) order[i]=-1,ran[i]=-1;
	int now=0;
	rep(i,N){
		dfs(i,now);
	}
	int p[N];
	rep(i,N) p[order[i]]=i;
	//cout<<endl;
	rep(i,N){
		int a=p[N-1-i];
		if(ran[a]==-1) dfs_2(a),r++;
	}

	vector<vector<int>> F(r);
	map<int,int> m;
	rep(i,N){
		int a=ran[i];
		F[a].push_back(i);
		//cout<<a<<" ";
	}
	for(int i=0;i<r;i++){
		int a=A.root(F[i][0]);
		if(m.count(a)){
			ans.push_back({m[a],F[i][0]});
			m[a]=F[i][0];
		}
		else m[a]=F[i][0];
	}
	//cout<<endl<<ans.size()<<endl<<endl;
	rep(i,r){
		int L=F[i].size();
		//cout<<L<<endl;
		if(L==1) continue;
		rep(j,L-1) ans.push_back({F[i][j],F[i][j+1]});
		ans.push_back({F[i][L-1],F[i][0]});
	}
	cout<<ans.size()<<"\n";
	rep(i,ans.size()){
		cout<<ans[i].first+1<<" "<<ans[i].second+1<<"\n";
	}
	return 0;
}

0