//\\__ hr1212 __//\\
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef map<int,int> mi;
#define si(a) scanf("%d",&a)
#define sii(a,b) scanf("%d %d",&a,&b)
#define siii(a,b,c) scanf("%d %d %d",&a,&b,&c)
#define pi(a) printf("%d\n",a)
#define nl printf("\n");
#define pb push_back
#define mp make_pair
#define all(c) (c).begin(),(c).end()
#define f(i,a,b) for(i=a;i<b;i++)
#define rf(i,a,b) for(i=a;i>=b;i--)
#define clr(x,a) memset(x,a,sizeof(x))
#define MAX 1000100
#define MOD 1000000007
int n,m,parent[MAX];
vi w;
vector<pair<int,pair<int,int> > > v;
int findp(int x){
if(parent[x]==x)
return x;
return parent[x]=findp(parent[x]);
}
int sameset(int x,int y){
return findp(x)==findp(y);
}
void unionset(int x,int y){
if(!sameset(x,y))
parent[findp(x)]=findp(y);
}
int main(){
int r,k,i,c=0,x=0,y=0,j,t,l,z,x1=0,y1=0;
ll ans=0,xx,cst,cst1;string p;
si(t);
while(t--){
sii(n,m);
v.clear();
w.clear();
f(i,0,m){
siii(x,y,z);
v.pb(mp(z,mp(x,y)));
}
f(i,0,n+1)
parent[i]=i;
cst=0;
sort(all(v));
f(i,0,v.size()){
pair<int,pair<int,int> > pp=v[i];
z=pp.first;x=pp.second.first;y=pp.second.second;
if(!sameset(x,y)){
w.pb(i);
unionset(x,y);
cst+=z;
}
}
cout<<cst<<" ";
cst1=1e9;
f(j,0,w.size()){
f(i,0,n+1)
parent[i]=i;
xx=0;
f(i,0,v.size()){
if(i==w[j])
continue;
pair<int,pair<int,int> > pp=v[i];
z=pp.first;x=pp.second.first;y=pp.second.second;
if(!sameset(x,y)){
unionset(x,y);
xx+=z;
}
}
c=0;
f(i,1,n+1)
if(parent[i]==i)
c++;
if(c>1)
continue;
cst1=min(cst1,xx);
}
if(cst1==1e9 || cst1<cst)
cst1=cst;
cout<<cst1<<endl;
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef map<int,int> mi;
#define si(a) scanf("%d",&a)
#define sii(a,b) scanf("%d %d",&a,&b)
#define siii(a,b,c) scanf("%d %d %d",&a,&b,&c)
#define pi(a) printf("%d\n",a)
#define nl printf("\n");
#define pb push_back
#define mp make_pair
#define all(c) (c).begin(),(c).end()
#define f(i,a,b) for(i=a;i<b;i++)
#define rf(i,a,b) for(i=a;i>=b;i--)
#define clr(x,a) memset(x,a,sizeof(x))
#define MAX 1000100
#define MOD 1000000007
int n,m,parent[MAX];
vi w;
vector<pair<int,pair<int,int> > > v;
int findp(int x){
if(parent[x]==x)
return x;
return parent[x]=findp(parent[x]);
}
int sameset(int x,int y){
return findp(x)==findp(y);
}
void unionset(int x,int y){
if(!sameset(x,y))
parent[findp(x)]=findp(y);
}
int main(){
int r,k,i,c=0,x=0,y=0,j,t,l,z,x1=0,y1=0;
ll ans=0,xx,cst,cst1;string p;
si(t);
while(t--){
sii(n,m);
v.clear();
w.clear();
f(i,0,m){
siii(x,y,z);
v.pb(mp(z,mp(x,y)));
}
f(i,0,n+1)
parent[i]=i;
cst=0;
sort(all(v));
f(i,0,v.size()){
pair<int,pair<int,int> > pp=v[i];
z=pp.first;x=pp.second.first;y=pp.second.second;
if(!sameset(x,y)){
w.pb(i);
unionset(x,y);
cst+=z;
}
}
cout<<cst<<" ";
cst1=1e9;
f(j,0,w.size()){
f(i,0,n+1)
parent[i]=i;
xx=0;
f(i,0,v.size()){
if(i==w[j])
continue;
pair<int,pair<int,int> > pp=v[i];
z=pp.first;x=pp.second.first;y=pp.second.second;
if(!sameset(x,y)){
unionset(x,y);
xx+=z;
}
}
c=0;
f(i,1,n+1)
if(parent[i]==i)
c++;
if(c>1)
continue;
cst1=min(cst1,xx);
}
if(cst1==1e9 || cst1<cst)
cst1=cst;
cout<<cst1<<endl;
}
return 0;
}
No comments:
Post a Comment