結果

問題 No.1553 Lovely City
ユーザー suzuken_wsuzuken_w
提出日時 2021-07-08 20:53:24
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 141 ms / 2,000 ms
コード長 4,042 bytes
コンパイル時間 3,210 ms
コンパイル使用メモリ 230,548 KB
実行使用メモリ 41,448 KB
最終ジャッジ日時 2023-09-14 04:52:22
合計ジャッジ時間 9,554 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 6 ms
10,088 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 90 ms
27,868 KB
testcase_09 AC 96 ms
29,896 KB
testcase_10 AC 98 ms
29,676 KB
testcase_11 AC 72 ms
23,916 KB
testcase_12 AC 99 ms
29,144 KB
testcase_13 AC 80 ms
26,044 KB
testcase_14 AC 89 ms
28,736 KB
testcase_15 AC 78 ms
25,672 KB
testcase_16 AC 103 ms
30,432 KB
testcase_17 AC 73 ms
23,588 KB
testcase_18 AC 136 ms
41,200 KB
testcase_19 AC 135 ms
41,448 KB
testcase_20 AC 141 ms
41,432 KB
testcase_21 AC 137 ms
40,380 KB
testcase_22 AC 137 ms
40,852 KB
testcase_23 AC 139 ms
40,704 KB
testcase_24 AC 138 ms
40,240 KB
testcase_25 AC 139 ms
41,436 KB
testcase_26 AC 136 ms
41,000 KB
testcase_27 AC 136 ms
40,324 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("O3")
#include<bits/stdc++.h> 
using namespace std;
using ll=long long;
using lll=__int128_t;
using P=pair<ll,ll>;
template<class T> using V=vector<T>; 
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
const ll inf=(1e18);
//const ll mod=998244353;
const ll mod=1000000007;
const vector<int> dy={-1,0,1,0},dx={0,-1,0,1};
ll GCD(ll a,ll b) {return b ? GCD(b,a%b):a;}
ll LCM(ll c,ll d){return c/GCD(c,d)*d;}
struct __INIT{__INIT(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);}} __init;
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
template<class T>void debag(const vector<T> &a){cerr<<"debag :";for(auto v:a)cerr<<v<<" ";cerr<<"\n";}
template<class T>void print(const vector<T> &a){for(auto v:a)cout<<v<<" ";cout<<"\n";}
std::ostream &operator<<(std::ostream &dest, __int128_t value) {
  std::ostream::sentry s(dest);
if (s) {__uint128_t tmp = value < 0 ? -value : value;char buffer[128];char *d = std::end(buffer);do {--d;*d = "0123456789"[tmp % 10];tmp /= 10;} while (tmp != 0);
if (value < 0) { --d; *d = '-';}int len = std::end(buffer) - d;if (dest.rdbuf()->sputn(d, len) != len) {dest.setstate(std::ios_base::badbit);}}
  return dest;}
class UF{
public:
	vector<int> par,num;
	int find(int v){
		return (par[v]==v)? v: (par[v]=find(par[v]));
	}
  explicit UF(){}
	explicit UF(int N):par(N),num(N,1){
		iota(all(par),0);
	}
  void init(int N){
     par.assign(N,0);
     num.assign(N,1);
     iota(all(par),0);
  }
	void unite(int u,int v){
		u=find(u),v=find(v);
		if(u==v)return;
		if(num[u]<num[v])swap(u,v);
		num[u]+=num[v];
		par[v]=u;
	}
	bool same(int u,int v){
		return find(u)==find(v);
	}
	bool ispar(int v){
		return v==find(v);
	}
	int size(int v){
		return num[find(v)];
	}
};
struct tposort{
	int n;
	vector<vector<int> > G;
	vector<int> deg,res;
	tposort(int node_size):n(node_size), G(n), deg(n, 0){}
	
    void add_edge(int from,int to){
		G[from].push_back(to);
		deg[to]++;
	}
	bool solve(){
		queue<int> q;
		for(int i = 0; i < n; i++){
			if(deg[i] == 0){
				q.push(i);
			}
		}
		while(!q.empty()){
			int cur = q.front();
			q.pop();
			res.push_back(cur);
			for(int v :G[cur]){
				if(--deg[v]==0){
					q.push(v);
				}
			}
		}
		return (*max_element(deg.begin(),deg.end()) == 0);
	}
};
int main(){
      int n,m;
      cin>>n>>m;
      UF uf(n);
      V<P> a(m);
      for(int i=0;i<m;i++){
            cin>>a[i].fi>>a[i].se;
            a[i].fi--;a[i].se--;
            uf.unite(a[i].fi,a[i].se);
      }
      V<V<P>> d(n);
      for(int i=0;i<m;i++){
             d[uf.find(a[i].fi)].emplace_back(a[i]);
      }
      V<int> id(n);
      V<P> ans;
      V<bool> used(n,false);
      for(int i=0;i<n;i++){
            if(uf.find(i)!=i)continue;
            if(uf.size(i)==1)continue;
            int cnt=0;
            for(auto &p:d[i]){
                  if(!used[p.fi]){
                        id[p.fi]=cnt++;
                        used[p.fi]=true;
                  }
                  if(!used[p.se]){
                        id[p.se]=cnt++;
                        used[p.se]=true;
                  }
            }
            V<V<int>> g(cnt),tmp;
            V<int> rid(cnt);
            for(auto &p:d[i]){
                   rid[id[p.fi]]=p.fi+1;rid[id[p.se]]=p.se+1;
                   g[id[p.fi]].emplace_back(id[p.se]);
            }
            tposort tp(cnt);
            for(int j=0;j<cnt;j++){
                  for(int v:g[j])tp.add_edge(j,v);
            }
            if(!tp.solve()){
                   for(int j=0;j<cnt;j++){
                           ans.emplace_back(rid[j],rid[(j+1)%cnt]);
                   }
            }else{
                  for(int j=0;j<cnt-1;j++){
                         ans.emplace_back(rid[tp.res[j]],rid[tp.res[j+1]]);
                  }
            }
       }
       cout<<ans.size()<<"\n";
       for(auto &p:ans)cout<<p.fi<<" "<<p.se<<"\n";
}
0