답안 #648369

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
648369 2022-10-06T09:00:09 Z inksamurai 시간이 돈 (balkan11_timeismoney) C++17
100 / 100
1788 ms 600 KB
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define rng(i,c,n) for(int i=c;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define vec(...) vector<__VA_ARGS__>
#define _3Rz7lEu ios::sync_with_stdio(0),cin.tie(0)
typedef long long ll;
using pii=pair<int,int>;
using vi=vector<int>;
void print(){cout<<'\n';}
template<class h,class...t>
void print(const h&v,const t&...u){cout<<v<<' ',print(u...);}
// e

struct dsu{
	int _n;
	vi si,par,leb;
	dsu(int n=0){
		init(n);
	}
	void init(int n=0){
		_n=n;
		par=vi(_n,0);
		si=vi(_n,0);
		leb=vi(_n,-1);
		rep(i,n){
			si[i]=1;
			par[i]=i;
		}	
	}
	int parent(int u){return par[u]=(par[u]==u?u:parent(par[u]));}
	bool same(int u,int v){return parent(u)==parent(v);}
	void merge(int u,int v){
		u=parent(u),v=parent(v);
		if(!same(u,v)){
			if(si[u]<si[v]) swap(u,v);
			leb[u]=max(leb[u],leb[v]);
			_n-=1;
			si[u]+=si[v];
			par[v]=u;
		}
	}
	int size(int v=-1){return v==-1?_n:si[parent(v)];}
};

ll ccw(pii a,pii b,pii c){
	return a.fi*(b.se-c.se)+b.fi*(c.se-a.se)+c.fi*(a.se-b.se);
}

signed main(){
_3Rz7lEu;
	int n,m;
	cin>>n>>m;
	using E=pair<pii,pii>;
	vec(E) es;
	rep(i,m){
		int u,v,w,z;
		cin>>u>>v>>w>>z;
		es.pb(E(pii(w,z),pii(u,v)));
	}
	dsu uf(n);
	vec(pii) opte;
	auto mst=[&](int x,int y,int t=0)->pii{
		sort(es.begin(),es.end(),[&](const E&l,const E&r){
			return pair<ll,pii>(1ll*l.fi.se*x-1ll*l.fi.fi*y,l.se)<pair<ll,pii>(1ll*r.fi.se*x-1ll*r.fi.fi*y,r.se);
		});
		rep(i,n){
			uf.par[i]=i;
			uf.si[i]=1;
		}
		pii p={0,0};
		for(auto e:es){
			auto [u,v]=e.se;
			if(uf.same(u,v)) continue;
			uf.merge(u,v);
			p.fi+=e.fi.fi;
			p.se+=e.fi.se;
			if(t){
				opte.pb(e.se);
			}
		}
		return p;
	};
	ll mi=1e18;
	int dx=-1,dy=-1;
	auto add=[&](int x,int y,ll val)->void{
		if(val<mi){
			mi=val;
			dx=x,dy=y;
		}
	};
	auto dnc=[&](auto&self,pii l,pii r)->void{
		int x=r.fi-l.fi,y=r.se-l.se;
		pii p=mst(x,y);
		// print("left point is = ",l.fi,l.se);
		// print("mid point is = ",p.fi,p.se);
		// print("right point is = ",r.fi,r.se);
		// print(ccw(l,p,r));
		if(ccw(l,p,r)<=0) return;
		add(x,y,1ll*p.fi*p.se);
		self(self,l,p);
		self(self,p,r);
	};
	pii l=mst(-1,0);
	add(1,0,l.fi*l.se);
	pii r=mst(0,1);
	add(0,1,r.fi*r.se);
	dnc(dnc,l,r);
	pii p=mst(dx,dy,1);
	cout<<p.fi<<" "<<p.se<<"\n";
	for(auto p:opte){
		cout<<p.fi<<" "<<p.se<<"\n";
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 2 ms 340 KB Output is correct
8 Correct 7 ms 600 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 15 ms 336 KB Output is correct
15 Correct 8 ms 212 KB Output is correct
16 Correct 208 ms 368 KB Output is correct
17 Correct 191 ms 460 KB Output is correct
18 Correct 205 ms 368 KB Output is correct
19 Correct 1722 ms 600 KB Output is correct
20 Correct 1788 ms 600 KB Output is correct