結果

問題 No.5007 Steiner Space Travel
ユーザー FplusFplusFFplusFplusF
提出日時 2022-07-30 17:42:43
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 4 ms / 1,000 ms
コード長 5,463 bytes
コンパイル時間 2,728 ms
実行使用メモリ 5,164 KB
スコア 4,559,831
最終ジャッジ日時 2022-07-30 17:43:01
合計ジャッジ時間 4,410 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
5,156 KB
testcase_01 AC 4 ms
5,160 KB
testcase_02 AC 4 ms
4,904 KB
testcase_03 AC 4 ms
4,904 KB
testcase_04 AC 4 ms
4,904 KB
testcase_05 AC 4 ms
3,996 KB
testcase_06 AC 4 ms
4,904 KB
testcase_07 AC 3 ms
4,904 KB
testcase_08 AC 3 ms
4,904 KB
testcase_09 AC 4 ms
4,908 KB
testcase_10 AC 4 ms
5,164 KB
testcase_11 AC 4 ms
4,904 KB
testcase_12 AC 3 ms
4,904 KB
testcase_13 AC 4 ms
4,900 KB
testcase_14 AC 4 ms
4,908 KB
testcase_15 AC 4 ms
4,904 KB
testcase_16 AC 4 ms
4,900 KB
testcase_17 AC 4 ms
4,900 KB
testcase_18 AC 4 ms
4,900 KB
testcase_19 AC 4 ms
4,904 KB
testcase_20 AC 4 ms
4,900 KB
testcase_21 AC 3 ms
4,900 KB
testcase_22 AC 4 ms
4,904 KB
testcase_23 AC 3 ms
4,908 KB
testcase_24 AC 4 ms
5,160 KB
testcase_25 AC 4 ms
4,900 KB
testcase_26 AC 4 ms
4,900 KB
testcase_27 AC 4 ms
5,160 KB
testcase_28 AC 4 ms
4,904 KB
testcase_29 AC 4 ms
5,160 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for (long long i=0;i<(long long)(n);i++)
#define all(v) v.begin(),v.end()
using ll=long long;
using pll=pair<ll,ll>;
using tll=tuple<ll,ll,ll>;
const ll inf=-(1ll<<60),INF=(1ll<<60);
template<class T> void chmin(T &a,T b){
    if(a>b){
        a=b;
    }
}
template<class T> void chmax(T &a,T b){
    if(a<b){
        a=b;
    }
}
struct union_find{
    vector<ll> par,siz;
    union_find(ll n):par(n),siz(n,1){
        rep(i,n){
            par[i]=i;
        }
    }
    ll root(ll x){
        if(par[x]==x) return x;
        return par[x]=root(par[x]);
    }
    void unite(ll x,ll y){
        ll rx=root(x);
        ll ry=root(y);
        if(rx==ry) return;
        if(siz[rx]<siz[ry]) swap(rx,ry);
        par[ry]=rx;
        siz[rx]+=siz[ry];
    }
    bool same(ll x,ll y){
        ll rx=root(x);
        ll ry=root(y);
        return rx==ry;
    }
    ll size(ll x){
        return siz[root(x)];
    }
};
struct edge{
    ll to;
	ll cost;
	edge(ll to,ll cost) : to(to),cost(cost) {};
};
struct graph{
    ll v;
    vector<vector<edge>> g;
    vector<ll> d,prev;
    graph(ll n){
        v=n;
        g.resize(v);
        d.resize(v,INF);
        prev.resize(v,-1);
    }
    void add_edge(ll s,ll t,ll cost){
        g[s].push_back({t,cost});
    }
    void dijkstra(ll s){
        rep(i,d.size()){
            d[i]=INF;
        }
        priority_queue<pll,vector<pll>,greater<pll>> pq;
        d[s]=0;
        pq.push({0,s});
        while(!pq.empty()){
            ll x_dist,x_vertex;
            tie(x_dist,x_vertex)=pq.top();
            pq.pop();
            if(d[x_vertex]<x_dist) continue;
            for(auto edge:g[x_vertex]){
                if(d[x_vertex]+edge.cost<d[edge.to]){
                    d[edge.to]=d[x_vertex]+edge.cost;
                    prev[edge.to]=x_vertex;
                    pq.push({d[edge.to],edge.to});
                }
            }
        }
    }
    vector<pll> vp;
    ll kruskal(){
        priority_queue<tll,vector<tll>,greater<tll>> pq;
        rep(i,g.size()){
            for(auto edge:g[i]){
                pq.push({edge.cost,i,edge.to});
            }
        }
        ll min_cost=0;
        union_find uf(g.size());
        while(!pq.empty()){
            ll x,x_to,x_cost;
            tie(x_cost,x,x_to)=pq.top();
            pq.pop();
            if(!uf.same(x,x_to)){
                min_cost+=x_cost;
                uf.unite(x,x_to);
                vp.emplace_back(x,x_to);
            }
        }
        return min_cost;
    }
};
ll dfss(ll x,ll p,vector<ll> &vvd,vector<vector<ll>> &v){
    ll sum=0;
    for(auto &i:v[x]){
        if(i==p) continue;
        sum+=dfss(i,x,vvd,v);
    }
    vvd[x]=sum+1;
    return vvd[x];
}
void dfs(ll x,vector<bool> &vis,vector<vector<ll>> &v,vector<ll> &vvd,vector<ll> &ans){
    ans.push_back(x);
    vis[x]=true;
    bool t=true;
    rep(j,vis.size()){
        if(!vis[j]) t=false;
    }
    if(t) return;
    vector<pll> vv;
    for(auto &i:v[x]){
        if(vis[i]) continue;
        vv.emplace_back(vvd[i],i);
    }
    sort(all(vv));
    for(auto &i:vv){
        dfs(i.second,vis,v,vvd,ans);
    }
    if(ans.back()!=x) ans.push_back(x);
}

int main(){
    ll n,m;
    cin >> n >> m;
    vector<ll> a(n),b(n);
    rep(i,n){
        cin >> a[i] >> b[i];
    }
    graph g(n);
    rep(i,n){
        for(int j=i+1;j<n;j++){
            ll dist=(a[i]-a[j])*(a[i]-a[j])+(b[i]-b[j])*(b[i]-b[j]);
            g.add_edge(i,j,dist);
            g.add_edge(j,i,dist);
        }
    }
    g.kruskal();
    vector<vector<ll>> v(n);
    rep(i,g.vp.size()){
        v[g.vp[i].first].push_back(g.vp[i].second);
        v[g.vp[i].second].push_back(g.vp[i].first);
    }
    vector<ll> vvd(n);
    dfss(0,-1,vvd,v);
    vector<ll> d(n,-1);
    queue<ll> q;
    q.push(0);
    d[0]=0;
    while(!q.empty()){
        ll x=q.front();
        q.pop();
        for(auto &i:v[x]){
            if(d[i]==-1){
                d[i]=d[x]+1;
                q.push(i);
            }
        }
    }
    vector<pll> vd(n);
    rep(i,n){
        vd[i]={d[i],i};
    }
    sort(all(vd));
    ll xxx=vd.back().second;
    d.assign(n,-1);
    q.push(xxx);
    d[xxx]=0;
    while(!q.empty()){
        ll x=q.front();
        q.pop();
        for(auto &i:v[x]){
            if(d[i]==-1){
                d[i]=d[x]+1;
                q.push(i);
            }
        }
    }
    vd.assign(n,{0,0});
    rep(i,n){
        vd[i]={d[i],i};
    }
    sort(all(vd));
    ll yyy=vd.back().second;
    v[xxx].push_back(yyy);
    v[yyy].push_back(xxx);
    vector<bool> vis(n,false);
    vector<ll> ans;
    dfs(0,vis,v,vvd,ans);
    if(ans.back()!=0) ans.push_back(0);
    vector<pll> cv;
    rep(i,n){
        cv.emplace_back(v[i].size(),i);
    }
    sort(all(cv));
    reverse(all(cv));
    vector<pll> pans;
    rep(i,ans.size()){
        pans.emplace_back(1,ans[i]+1);
    }
    rep(i,m){
        ll x=cv[i].second;
        cout << a[x] << " " << b[x] << endl;
        ll j=0;
        while(j<pans.size()){
            ll f,s;
            tie(f,s)=pans[j];
            if(f==1&&s==x+1){
                pans[j]={2,i+1};
                pans.insert(pans.begin()+j+1,{1,s});
                pans.insert(pans.begin()+j+2,{2,i+1});
                j+=2;
            }
            j++;
        }
    }
    cout << pans.size() << endl;
    rep(i,pans.size()){
        cout << pans[i].first << " " << pans[i].second << endl;
    }
}
0