Language: C++


// Problem : D - Built?
// Contest : AtCoder - AtCoder Beginner Contest 065
// URL : https://atcoder.jp/contests/abc065/tasks/arc076_b
// Memory Limit : 256 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

//#pragma GCC optimize("Ofast")
//#pragma GCC optimize ("unroll-loops")
//#pragma GCC target("avx,avx2,fma")
#include<bits/stdc++.h>
#define eb emplace_back
#define pb push_back
#define size(s) (int)s.size()
#define int long long
#define vi vector<int>
#define vs vector<string>
#define vv vector<vector<int>>
#define pii pair<int,int>
#define m_p(x,y) make_pair(x,y)
#define vp vector<pair<int,int>>
#define setbits(x) __builtin_popcountll(x)
#define f first
#define se second
#define inc(v,n,x) v.assign(n,x)
#define incd(v,n) v.resize(n)
#define iniz(n) memset(n,0,sizeof(n))
#define inin(n) memset(n,-1,sizeof(n))
#define inimi(n) memset(n,0xc0,sizeof(n))
#define inima(n) memset(n,0x3f,sizeof(n))
#define all(v) (v).begin(),(v).end()

using namespace std;

template<typename T1,typename T2>istream &operator>>(istream &is, vector<pair<T1,T2>> &v) { for (pair<T1,T2> &t : v) is >> t.f>>t.se; return is; }
template<typename T>istream &operator>>(istream &is, vector<T> &v) { for (T &t : v) is >> t; return is; }
template<typename T>ostream &operator<<(ostream &os, const vector<T> &v) { for (const T &t : v) {os << t <<" ";} os << '\n'; return os; }

const int N=200001;
double pi=acos(-1.0);
int md=1e9+7;
const int INF=1e15;
int dx1[]={0,0,-1,1};
int dy1[]={1,-1,0,0};

template<class T>
T abst(T a)
{return a<0?-a:a;}

template<class T>
T max2(T a,T b){return a>b?a:b;}
template<class T>
T min2(T a,T b){return a<b?a:b;}

vi dsu,size;
void init_dsu(int n)
{
	incd(dsu,n+1);
	incd(size,n+1);
	for(int i=0;i<=n;i++)
	dsu[i]=i,size[i]=1;
}

int get_par(int x)
{
	if(dsu[x]==x) return x;
	return dsu[x]=get_par(dsu[x]);
}

void unite(int x,int y)
{
	int p1=get_par(x);
	int p2=get_par(y);
	if(p1!=p2)
	{
		size[p1]+=size[p2];
		dsu[p2]=p1;
	}
}

int MST(vector<pair<int,pii>> edges,int n)
{
	init_dsu(n+1);
	int cost=0;
	sort(all(edges));
	for(auto i:edges)
	{
		pii p=i.se;
		int x=get_par(p.f);
		int y=get_par(p.se);
		if(x!=y)
		{
			//cout<<x<<" "<<y<<" "<<i.f<<"\n";
			unite(x,y);
			cost+=i.f;
		}
	}
	return cost;
}

void solve()
{
int n;cin>>n;
vector<pair<int,pii>> edges;
vp v1,v2;
int point=1;
map<pii,int> mp;
for(int i=0;i<n;i++)
{
	int x,y;
	cin>>x>>y;
	if(mp.count({x,y}))continue;
	else
	{
		mp[{x,y}]=point++;
		v1.pb({x,point-1});
		v2.pb({y,point-1});
	}
}
sort(all(v1));
sort(all(v2));
for(int i=0;i<n-1;i++)
{
	edges.pb({v1[i+1].f-v1[i].f,{v1[i].se,v1[i+1].se}});
	edges.pb({v1[i+1].f-v1[i].f,{v1[i+1].se,v1[i].se}});
}
for(int i=0;i<n-1;i++)
{
	edges.pb({v2[i+1].f-v2[i].f,{v2[i].se,v2[i+1].se}});
	edges.pb({v2[i+1].f-v2[i].f,{v2[i+1].se,v2[i].se}});
}
cout<<MST(edges,point);
}

int32_t main(){
ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
int t=1;
//cin>>t;
for(int i=1;i<=t;i++)
{
    solve();
}
return 0;
}


Compiler output:
a.cc: In function 'void init_dsu(long long int)':
a.cc:60:14: error: reference to 'size' is ambiguous
   60 |         incd(size,n+1);
      |              ^~~~
a.cc:27:19: note: in definition of macro 'incd'
   27 | #define incd(v,n) v.resize(n)
      |                   ^
In file included from /usr/include/c++/14/string:53,
                 from /usr/include/c++/14/bitset:52,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:52,
                 from a.cc:12:
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:56:8: note:                 'std::vector<long long int> size'
   56 | vi dsu,size;
      |        ^~~~
a.cc:62:18: error: reference to 'size' is ambiguous
   62 |         dsu[i]=i,size[i]=1;
      |                  ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:56:8: note:                 'std::vector<long long int> size'
   56 | vi dsu,size;
      |        ^~~~
a.cc: In function 'void unite(long long int, long long int)':
a.cc:77:17: error: reference to 'size' is ambiguous
   77 |                 size[p1]+=size[p2];
      |                 ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:56:8: note:                 'std::vector<long long int> size'
   56 | vi dsu,size;
      |        ^~~~
a.cc:77:27: error: reference to 'size' is ambiguous
   77 |                 size[p1]+=size[p2];
      |                           ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:56:8: note:                 'std::vector<long long int> size'
   56 | vi dsu,size;
      |        ^~~~