結果

問題 No.1553 Lovely City
ユーザー mugen_1337mugen_1337
提出日時 2021-06-18 22:15:10
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 893 ms / 2,000 ms
コード長 6,346 bytes
コンパイル時間 2,984 ms
コンパイル使用メモリ 235,868 KB
実行使用メモリ 82,272 KB
最終ジャッジ日時 2023-09-04 23:21:02
合計ジャッジ時間 23,060 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 23 ms
20,252 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 3 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 564 ms
54,528 KB
testcase_09 AC 618 ms
58,020 KB
testcase_10 AC 615 ms
61,316 KB
testcase_11 AC 444 ms
48,136 KB
testcase_12 AC 580 ms
59,304 KB
testcase_13 AC 508 ms
48,872 KB
testcase_14 AC 558 ms
55,360 KB
testcase_15 AC 498 ms
51,580 KB
testcase_16 AC 661 ms
60,988 KB
testcase_17 AC 460 ms
47,496 KB
testcase_18 AC 868 ms
82,248 KB
testcase_19 AC 851 ms
81,960 KB
testcase_20 AC 867 ms
82,100 KB
testcase_21 AC 880 ms
82,188 KB
testcase_22 AC 840 ms
82,004 KB
testcase_23 AC 853 ms
81,924 KB
testcase_24 AC 893 ms
82,000 KB
testcase_25 AC 892 ms
82,028 KB
testcase_26 AC 854 ms
82,052 KB
testcase_27 AC 870 ms
82,272 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0