結果

問題 No.1553 Lovely City
ユーザー hongrockhongrock
提出日時 2021-06-20 21:49:31
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 271 ms / 2,000 ms
コード長 2,009 bytes
コンパイル時間 1,994 ms
コンパイル使用メモリ 175,212 KB
実行使用メモリ 39,608 KB
最終ジャッジ日時 2023-09-05 01:39:40
合計ジャッジ時間 11,986 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 10 ms
23,928 KB
testcase_01 AC 9 ms
23,852 KB
testcase_02 AC 51 ms
37,264 KB
testcase_03 AC 10 ms
23,912 KB
testcase_04 AC 10 ms
23,984 KB
testcase_05 AC 10 ms
23,904 KB
testcase_06 AC 10 ms
23,860 KB
testcase_07 AC 9 ms
23,840 KB
testcase_08 AC 153 ms
32,764 KB
testcase_09 AC 173 ms
33,272 KB
testcase_10 AC 196 ms
35,752 KB
testcase_11 AC 137 ms
32,044 KB
testcase_12 AC 208 ms
38,152 KB
testcase_13 AC 146 ms
31,564 KB
testcase_14 AC 156 ms
32,804 KB
testcase_15 AC 144 ms
33,032 KB
testcase_16 AC 179 ms
33,532 KB
testcase_17 AC 133 ms
33,492 KB
testcase_18 AC 259 ms
39,596 KB
testcase_19 AC 264 ms
39,532 KB
testcase_20 AC 255 ms
39,480 KB
testcase_21 AC 264 ms
39,456 KB
testcase_22 AC 267 ms
39,596 KB
testcase_23 AC 266 ms
39,432 KB
testcase_24 AC 271 ms
39,608 KB
testcase_25 AC 265 ms
39,604 KB
testcase_26 AC 269 ms
39,480 KB
testcase_27 AC 267 ms
39,428 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;

#define rep(i, a, n) for(int i=(a); i<(n); ++i)
#define per(i, a, n) for(int i=(a); i>(n); --i)
#define pb emplace_back
#define mp make_pair
#define clr(a, b) memset(a, b, sizeof(a))
#define all(x) (x).begin(),(x).end()
#define lowbit(x) (x & -x)
#define fi first
#define se second
#define lson o<<1
#define rson o<<1|1
#define gmid l[o]+r[o]>>1

using LL = long long;
using ULL = unsigned long long;
using pii = pair<int,int>;
using PLL = pair<LL, LL>;
using UI = unsigned int;

const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const double EPS = 1e-8;
const double PI = acos(-1.0);

const int N = 2e5 + 10;

vector<int> V[N], G[N], A[N], P[N];
vector<pii> ans;
int n, m, deg[N], idx[N];
int com, sz[N];

void bfs(int x){
	queue<int> Q;
	Q.emplace(x);
	idx[x] = ++com;
	sz[com] = 0;

	while(!Q.empty()){
		x = Q.front(); Q.pop();
		P[com].pb(x);
        ++sz[com];

		for(int j : G[x]){
			if(!idx[j]){
				idx[j] = com;
				Q.emplace(j);
			}
		}
	}
}

int main(){
	scanf("%d %d", &n, &m);
	int x, y;
	clr(deg, 0);
	while(m--){
		scanf("%d %d", &x, &y);
		V[x].pb(y);
		++deg[y];

		G[x].pb(y);
		G[y].pb(x);
	}

	clr(idx, 0);
	com = 0;

    rep(i, 1, n + 1){
        if(!idx[i]){
            bfs(i);
        }
    }

    queue<int> Q;
    rep(i, 1, n + 1){
        if(deg[i] == 0){
            Q.emplace(i);
        }
    }

    while(!Q.empty()){
        x = Q.front(); Q.pop();
        --sz[idx[x]];
        A[idx[x]].pb(x);
        for(int j : V[x]){
            if(--deg[j] == 0){
                Q.emplace(j);
            }
        }
    }

    rep(i, 1, com + 1){
        if(sz[i] == 0){
            rep(j, 1, A[i].size()){
                ans.pb(A[i][j-1], A[i][j]);
            }
        } else {
            rep(j, 1, P[i].size()){
                ans.pb(P[i][j-1], P[i][j]);
            }
            ans.pb(P[i].back(), P[i][0]);
        }
    }


	printf("%d\n", (int)ans.size());
	for(pii p : ans)	printf("%d %d\n", p.fi, p.se);

	return 0;
}
0