結果
| 問題 |
No.5007 Steiner Space Travel
|
| コンテスト | |
| ユーザー |
FplusFplusF
|
| 提出日時 | 2022-07-30 15:25:25 |
| 言語 | C++17(gcc12) (gcc 12.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 4 ms / 1,000 ms |
| コード長 | 3,967 bytes |
| コンパイル時間 | 2,691 ms |
| 実行使用メモリ | 5,164 KB |
| スコア | 5,927,178 |
| 最終ジャッジ日時 | 2022-07-30 15:25:29 |
| 合計ジャッジ時間 | 4,411 ms |
|
ジャッジサーバーID (参考情報) |
judge10 / judge13 |
| 純コード判定しない問題か言語 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 30 |
ソースコード
#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;
}
};
void dfs(ll x,ll p,vector<vector<ll>> &v,vector<ll> &ans){
ans.push_back(x);
for(auto &i:v[x]){
if(i==p) continue;
dfs(i,x,v,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> ans;
dfs(0,-1,v,ans);
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;
rep(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,s});
}
}
}
cout << pans.size() << endl;
rep(i,pans.size()){
cout << pans[i].first << " " << pans[i].second << endl;
}
}
FplusFplusF