結果

問題 No.1553 Lovely City
ユーザー 沙耶花沙耶花
提出日時 2021-05-05 23:07:20
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 1,454 bytes
コンパイル時間 4,822 ms
コンパイル使用メモリ 269,836 KB
実行使用メモリ 31,960 KB
最終ジャッジ日時 2023-09-04 22:07:41
合計ジャッジ時間 17,626 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 WA -
testcase_02 AC 86 ms
25,080 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
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 <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!=0){
				A.push_back(ind[s[0][0]]);
				B.push_back(ind[s[i][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