結果

問題 No.1553 Lovely City
ユーザー 👑 potato167potato167
提出日時 2021-06-18 22:30:28
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,990 bytes
コンパイル時間 2,398 ms
コンパイル使用メモリ 215,916 KB
実行使用メモリ 46,556 KB
最終ジャッジ日時 2023-09-04 23:41:43
合計ジャッジ時間 11,463 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
12,116 KB
testcase_01 AC 7 ms
12,348 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 6 ms
12,188 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
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;
	rep(i,M){
		int a,b;
		cin>>a>>b;
		a--,b--;
		G[a].push_back(b);
		H[b].push_back(a);
	}
	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<pair<int,pair<int,int>>>> q(r);
	vector<vector<int>> F(r);
	UFtree A(r);
	rep(i,N){
		int a=ran[i];
		F[a].push_back(i);
		for(auto x:G[i]){
			int b=ran[x];
			q[a].push_back({b,{i,x}});
		}
	}
	for(int i=0;i<r-1;i++){
		ans.push_back({F[i][0],F[i+1][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