#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)->bool{
if(val<mi){
mi=val;
dx=x,dy=y;
return 1;
}
return 0;
};
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 or !add(x,y,1ll*p.fi*p.se)) return;
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";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 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 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
10 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
11 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
12 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
15 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
16 |
Incorrect |
2 ms |
340 KB |
Output isn't correct |
17 |
Incorrect |
2 ms |
340 KB |
Output isn't correct |
18 |
Incorrect |
2 ms |
340 KB |
Output isn't correct |
19 |
Incorrect |
9 ms |
600 KB |
Output isn't correct |
20 |
Incorrect |
10 ms |
660 KB |
Output isn't correct |