結果

問題 No.1553 Lovely City
ユーザー 沙耶花沙耶花
提出日時 2021-05-07 04:46:19
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
(最新)
J_TLE  
(最初)
実行時間 -
コード長 1,499 bytes
コンパイル時間 5,032 ms
コンパイル使用メモリ 270,284 KB
実行使用メモリ 31,960 KB
最終ジャッジ日時 2023-09-04 22:08:27
合計ジャッジ時間 21,138 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 84 ms
25,000 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 2 ms
4,380 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 150 ms
25,328 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 114 ms
20,628 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 <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];
			if(i+1==ind.size())swap(x,y);
			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