AtCoder Regular Contest 076

Submission #1379815

Source codeソースコード

#include<iostream>
using namespace std;
#include<vector>
#include<map>
#include<set>
#include<algorithm>
#include<cstdio>
#define MAX_N 100000

int parent[MAX_N];
int ranka[MAX_N];
void init(int n){
	for(int i=0;i<n;i++){
		parent[i]=i;
		ranka[i]=0;
	}
}
int Find(int x){
	if(parent[x]==x){
    return x;
  }else{
		parent[x]=Find(parent[x]);
		return parent[x];
	}
}
void Union(int x,int y){
	x=Find(x);
	y=Find(y);
	if(x==y)return;
	if(ranka[x]<ranka[y]){
    parent[x]=y;
  }else{
		parent[y]=x;
		if(ranka[x]==ranka[y])ranka[x]++;
	}
}
bool same(int x,int y){
	return Find(x)==Find(y);
}

int main(){
  int n;
  cin>>n;
  vector< pair<int,int> > x(n);
  vector< pair<int,int> > y(n);

  for(int i=0;i<n;i++){
    int a,b;
    cin>>a>>b;
    x[i].first=a;
    x[i].second=i;
    y[i].first=b;
    y[i].second=i;
  }

  sort(x.begin(),x.end());
  sort(y.begin(),y.end());

  //cout<<x[0].first<<x[0].second<<endl;
  //vector< vector<int> > d(n,vector<int>(n,1000000001));
  vector< pair< int,pair<int,int> > > cost((n-1)*2);
  int j=0;
  for(int i=0;i<n-1;i++){
    int d2=x[i+1].first-x[i].first;
    //d[x[i].second][x[i+1].second]=min(d[x[i].second][x[i+1].second],d2);
    cost[j].first=d2;
    cost[j].second.first=x[i].second;
    cost[j].second.second=x[i+1].second;
    j++;
    d2=y[i+1].first-y[i].first;
    //d[y[i].second][y[i+1].second]=min(d[y[i].second][y[i+1].second],d2);
    cost[j].first=d2;
    cost[j].second.first=y[i].second;
    cost[j].second.second=y[i+1].second;
    j++;
    //cout<<d[x[i].second][x[i+1].second]<<endl;
    //cout<<d[y[i].second][y[i+1].second]<<endl;
  }
  sort(cost.begin(),cost.end());
  //for(int i=0;i<(n-1)*2;i++)cout<<cost[i].first<<" "<<cost[i].second.first<<" "<<cost[i].second.second<<endl;
  init(n);
  int ans=0;
  for(int i=0;i<(n-1)*2;i++){
    if(!same(cost[i].second.first,cost[i].second.second)){
      Union(cost[i].second.first,cost[i].second.second);
      ans+=cost[i].first;
    }
  }
  cout<<ans<<endl;
  return 0;
}

Submission

Task問題 D - Built?
User nameユーザ名 ratea
Created time投稿日時
Language言語 C++14 (GCC 5.4.1)
Status状態 AC
Score得点 500
Source lengthソースコード長 2080 Byte
File nameファイル名
Exec time実行時間 123 ms
Memory usageメモリ使用量 4992 KB

Test case

Set

Set name Score得点 / Max score Cases
Sample - s1.txt,s2.txt
All 500 / 500 01.txt,02.txt,03.txt,04.txt,05.txt,06.txt,07.txt,08.txt,09.txt,10.txt,11.txt,12.txt,13.txt,14.txt,15.txt,16.txt,17.txt,18.txt,19.txt,20.txt,21.txt,22.txt,23.txt,24.txt,25.txt,26.txt,s1.txt,s2.txt

Test case

Case name Status状態 Exec time実行時間 Memory usageメモリ使用量
01.txt AC 117 ms 4992 KB
02.txt AC 117 ms 4992 KB
03.txt AC 118 ms 4992 KB
04.txt AC 119 ms 4992 KB
05.txt AC 117 ms 4992 KB
06.txt AC 118 ms 4992 KB
07.txt AC 123 ms 4992 KB
08.txt AC 118 ms 4992 KB
09.txt AC 115 ms 4992 KB
10.txt AC 116 ms 4992 KB
11.txt AC 115 ms 4992 KB
12.txt AC 115 ms 4992 KB
13.txt AC 83 ms 4992 KB
14.txt AC 84 ms 4992 KB
15.txt AC 93 ms 4992 KB
16.txt AC 108 ms 4992 KB
17.txt AC 116 ms 4992 KB
18.txt AC 111 ms 4992 KB
19.txt AC 91 ms 4992 KB
20.txt AC 94 ms 4992 KB
21.txt AC 93 ms 4992 KB
22.txt AC 100 ms 4992 KB
23.txt AC 49 ms 4992 KB
24.txt AC 82 ms 4992 KB
25.txt AC 1 ms 256 KB
26.txt AC 1 ms 256 KB
s1.txt AC 1 ms 256 KB
s2.txt AC 1 ms 256 KB