結果
問題 | No.1553 Lovely City |
ユーザー | mugen_1337 |
提出日時 | 2021-06-18 22:15:10 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 742 ms / 2,000 ms |
コード長 | 6,346 bytes |
コンパイル時間 | 2,680 ms |
コンパイル使用メモリ | 237,048 KB |
実行使用メモリ | 82,388 KB |
最終ジャッジ日時 | 2024-06-22 20:36:49 |
合計ジャッジ時間 | 18,614 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,816 KB |
testcase_01 | AC | 2 ms
6,816 KB |
testcase_02 | AC | 21 ms
20,488 KB |
testcase_03 | AC | 2 ms
6,944 KB |
testcase_04 | AC | 2 ms
6,940 KB |
testcase_05 | AC | 3 ms
6,940 KB |
testcase_06 | AC | 2 ms
6,940 KB |
testcase_07 | AC | 3 ms
6,940 KB |
testcase_08 | AC | 482 ms
54,624 KB |
testcase_09 | AC | 513 ms
58,056 KB |
testcase_10 | AC | 524 ms
61,532 KB |
testcase_11 | AC | 380 ms
48,268 KB |
testcase_12 | AC | 485 ms
59,436 KB |
testcase_13 | AC | 415 ms
48,968 KB |
testcase_14 | AC | 473 ms
55,212 KB |
testcase_15 | AC | 407 ms
51,752 KB |
testcase_16 | AC | 536 ms
61,032 KB |
testcase_17 | AC | 374 ms
47,732 KB |
testcase_18 | AC | 720 ms
82,252 KB |
testcase_19 | AC | 711 ms
82,088 KB |
testcase_20 | AC | 731 ms
82,244 KB |
testcase_21 | AC | 726 ms
82,388 KB |
testcase_22 | AC | 711 ms
82,316 KB |
testcase_23 | AC | 742 ms
82,196 KB |
testcase_24 | AC | 719 ms
82,288 KB |
testcase_25 | AC | 710 ms
82,044 KB |
testcase_26 | AC | 723 ms
82,296 KB |
testcase_27 | AC | 706 ms
82,284 KB |
ソースコード
#include<bits/stdc++.h> using namespace std; #define ALL(x) begin(x),end(x) #define rep(i,n) for(int i=0;i<(n);i++) #define debug(v) cout<<#v<<":";for(auto x:v){cout<<x<<' ';}cout<<endl; #define mod 1000000007 using ll=long long; const int INF=1000000000; const ll LINF=1001002003004005006ll; int dx[]={1,0,-1,0},dy[]={0,1,0,-1}; // ll gcd(ll a,ll b){return b?gcd(b,a%b):a;} template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;} template<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return true;}return false;} struct IOSetup{ IOSetup(){ cin.tie(0); ios::sync_with_stdio(0); cout<<fixed<<setprecision(12); } } iosetup; template<typename T> ostream &operator<<(ostream &os,const vector<T>&v){ for(int i=0;i<(int)v.size();i++) os<<v[i]<<(i+1==(int)v.size()?"":" "); return os; } template<typename T> istream &operator>>(istream &is,vector<T>&v){ for(T &x:v)is>>x; return is; } #line 1 "Graph2/GraphTemplate.cpp" // graph template // ref : https://ei1333.github.io/library/graph/graph-template.cpp template<typename T=int> struct Edge{ int from,to; T w; int idx; Edge()=default; Edge(int from,int to,T w=1,int idx=-1):from(from),to(to),w(w),idx(idx){} operator int() const{return to;} }; template<typename T=int> struct Graph{ vector<vector<Edge<T>>> g; int V,E; Graph()=default; Graph(int n):g(n),V(n),E(0){} size_t size(){ return g.size(); } void resize(int k){ g.resize(k); } inline const vector<Edge<T>> &operator[](int k)const{ return (g.at(k)); } inline vector<Edge<T>> &operator[](int k){ return (g.at(k)); } void add_directed_edge(int from,int to,T cost=1){ g[from].emplace_back(from,to,cost,E++); } void add_edge(int from,int to,T cost=1){ g[from].emplace_back(from,to,cost,E); g[to].emplace_back(to,from,cost,E++); } void read(int m,int pad=-1,bool weighted=false,bool directed=false){ for(int i=0;i<m;i++){ int u,v;cin>>u>>v; u+=pad,v+=pad; T w=T(1); if(weighted) cin>>w; if(directed) add_directed_edge(u,v,w); else add_edge(u,v,w); } } }; #line 2 "Graph2/StronglyConnectedComponents.cpp" // scc.belong[i] : strongly connected components i belongs // scc.group[i] : vertice i-th strongly connected component has // scc.compressed : compressed Graph, DAG // Longest Path verified : https://atcoder.jp/contests/abc135/submissions/19684261 template<typename T=int> struct StronglyConnectedComponents{ private: Graph<T> g,rg; vector<int> check; void dfs(int cur,vector<int> &ord){ for(auto &to:g[cur])if(!check[to]){ check[to]=true; dfs(to,ord); } ord.push_back(cur); } void rdfs(int cur,int p){ for(auto &to:rg[cur])if(belong[to]==-1){ belong[to]=p; rdfs(to,p); } } void build(){ vector<int> ord; for(int i=0;i<(int)g.size();i++)if(!check[i]){ check[i]=true; dfs(i,ord); } int ptr=0;; for(int i=(int)ord.size()-1;i>=0;i--)if(belong[ord[i]]==-1){ belong[ord[i]]=ptr; rdfs(ord[i],ptr);ptr++; } compressed.resize(ptr); group.resize(ptr); for(int i=0;i<(int)g.size();i++){ int u=belong[i]; group[u].push_back(i); for(auto &e:g[i]){ int v=belong[e]; if(u!=v) compressed.add_directed_edge(u,v,e.w); } } return ; } public: vector<int> belong; vector<vector<int>> group; Graph<T> compressed; StronglyConnectedComponents(Graph<T> &g):g(g),rg(g.size()),check(g.size()),belong(g.size(),-1){ for(int i=0;i<(int)g.size();i++)for(auto &e:g[i]) rg.add_directed_edge(e.to,e.from,e.w); build(); } // topological sort vector<int> get_DAG_order(){ vector<int> ret; for(int i=0;i<(int)group.size();i++)for(auto &j:group[i]) ret.push_back(j); return ret; } // g is not DAG or contain self-loop, return inf T LongestPath(){ for(int i=0;i<(int)g.size();i++){ for(auto &e:g[i]){ if(belong[i]==belong[e]) return -1; } } vector<int> ord=get_DAG_order(); vector<T> dp(g.size(),0); for(auto i:ord)for(auto &e:g[i]) dp[e]=max(dp[e],dp[i]+e.w); return (*max_element(begin(dp),end(dp))); } }; struct UnionFind{ private: vector<int> par,siz; public: int con; UnionFind(int n):par(n),siz(n,1),con(n){ iota(begin(par),end(par),0); } int root(int x){ return (par[x]==x?x:(par[x]=root(par[x]))); } bool sameroot(int x,int y){ return root(x)==root(y); } bool unite(int x,int y){ x=root(x);y=root(y); if(x==y) return false; if(siz[x]<siz[y])swap(x,y); siz[x]+=siz[y]; par[y]=x; con--; return true; } int size(int x){ return siz[root(x)]; } }; signed main(){ int N,M;cin>>N>>M; using P=pair<int,int>; vector<P> es; UnionFind uf(N); rep(i,M){ int u,v;cin>>u>>v;u--,v--; uf.unite(u,v); es.emplace_back(u,v); } vector<vector<int>> gr(N); vector<vector<P>> gre(N); rep(i,N) gr[uf.root(i)].push_back(i); for(auto &[u,v]:es) gre[uf.root(u)].emplace_back(u,v); vector<P> res; rep(mugen,N){ auto &w=gr[mugen]; auto &e=gre[mugen]; if(w.size()<=1) continue; map<int,int> id,di; rep(i,w.size()) id[w[i]]=i,di[i]=w[i]; int n=(int)w.size(); Graph<int> g(n); for(auto &[u,v]:e) g.add_directed_edge(id[u],id[v]); StronglyConnectedComponents<int> scc(g); if(scc.group.size()==n){ auto ord=scc.get_DAG_order(); rep(i,n-1) res.emplace_back(di[ord[i]],di[ord[i+1]]); }else{ rep(i,n){ res.emplace_back(di[i],di[(i+1)%n]); } } } cout<<res.size()<<endl; for(auto &[u,v]:res) cout<<u+1<<" "<<v+1<<endl; return 0; }