답안 #648365

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
648365 2022-10-06T08:55:07 Z inksamurai 시간이 돈 (balkan11_timeismoney) C++17
100 / 100
1720 ms 984 KB
#include <bits/stdc++.h>
#define int ll
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({{w,z},{u,v}});
	}
	dsu uf(n);
	vec(pii) opte;
	auto mst=[&](int x,int y,int t=0)->pii{
		sort(es.begin(),es.end(),[&](E l,E r){
			return pair<int,pii>(l.fi.se*x-l.fi.fi*y,l.se)<pair<int,pii>(r.fi.se*x-r.fi.fi*y,r.se);
		});
		uf.init(n);
		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;
	};
	const int inf=1e18;
	int mi=inf;
	int dx=-1,dy=-1;
	auto add=[&](int x,int y,int 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,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 1 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 2 ms 340 KB Output is correct
8 Correct 7 ms 856 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 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 14 ms 468 KB Output is correct
15 Correct 8 ms 352 KB Output is correct
16 Correct 186 ms 440 KB Output is correct
17 Correct 186 ms 432 KB Output is correct
18 Correct 197 ms 424 KB Output is correct
19 Correct 1704 ms 984 KB Output is correct
20 Correct 1720 ms 984 KB Output is correct