結果

問題 No.1553 Lovely City
ユーザー 沙耶花沙耶花
提出日時 2021-05-05 22:51:33
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 201 ms / 2,000 ms
コード長 1,466 bytes
コンパイル時間 5,023 ms
コンパイル使用メモリ 269,944 KB
実行使用メモリ 31,952 KB
最終ジャッジ日時 2023-09-04 22:06:34
合計ジャッジ時間 13,581 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 86 ms
25,164 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 132 ms
21,116 KB
testcase_09 AC 144 ms
22,080 KB
testcase_10 AC 149 ms
25,140 KB
testcase_11 AC 107 ms
18,736 KB
testcase_12 AC 160 ms
27,012 KB
testcase_13 AC 119 ms
19,528 KB
testcase_14 AC 130 ms
21,004 KB
testcase_15 AC 119 ms
20,468 KB
testcase_16 AC 151 ms
23,264 KB
testcase_17 AC 111 ms
20,720 KB
testcase_18 AC 200 ms
31,952 KB
testcase_19 AC 201 ms
31,660 KB
testcase_20 AC 197 ms
31,728 KB
testcase_21 AC 198 ms
31,256 KB
testcase_22 AC 198 ms
31,460 KB
testcase_23 AC 199 ms
31,672 KB
testcase_24 AC 200 ms
31,452 KB
testcase_25 AC 199 ms
31,416 KB
testcase_26 AC 201 ms
31,472 KB
testcase_27 AC 199 ms
31,600 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace atcoder;
using mint = modint1000000007;
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define Inf 1000000000

vector<int> A,B;

void go(vector<int> ind,vector<int> a,vector<int> b){
	int n = ind.size();
	sort(ind.begin(),ind.end());
	
	scc_graph S(n);
	
	rep(i,a.size()){
		int x = distance(ind.begin(),lower_bound(ind.begin(),ind.end(),a[i]));
		int y = distance(ind.begin(),lower_bound(ind.begin(),ind.end(),b[i]));
		S.add_edge(x,y);
	}
	
	vector<vector<int>>  s = S.scc();
	if(s.size()==n){
		rep(i,s.size()){
			if(i+1!=s.size()){
				A.push_back(ind[s[i][0]]);
				B.push_back(ind[s[i+1][0]]);
			}
			
		}
	}
	else{
		rep(i,ind.size()){
			int x = ind[i],y = ind[(i+1)%n];
			A.push_back(x);
			B.push_back(y);
		}
	}
	
}

int main(){
	
	int N,M;
	cin>>N>>M;
	
	vector<int> a(M),b(M);
	
	dsu D(N);
	
	rep(i,M){
		scanf("%d %d",&a[i],&b[i]);
		a[i]--;b[i]--;
		D.merge(a[i],b[i]);
	}
	
	vector<int> pos(N);
	vector<vector<int>> ret = D.groups();
	rep(i,ret.size()){
		rep(j,ret[i].size()){
			pos[ret[i][j]] = i;
		}
	}
//	cout<<'a'<<endl;
	vector<vector<int>> aa(ret.size()),bb(ret.size());
	
	rep(i,M){
		int ind = pos[a[i]];
		aa[ind].push_back(a[i]);
		bb[ind].push_back(b[i]);		
	}
	
	rep(i,ret.size()){
		go(ret[i],aa[i],bb[i]);
	}
	//cout<<'a'<<endl;
	cout<<A.size()<<endl;
	rep(i,A.size()){
		printf("%d %d\n",A[i]+1,B[i]+1);
	}
	
    return 0;
}
0