Click here to view the problem
/***
Md. Namzul Hasan
Shahjalal University of Science & Technology,Sylhet.
hasan08sust@gmail.com
***/
#include<bits/stdc++.h>
using namespace std ;
typedef long long ll ;
typedef int in ;
typedef unsigned long long ull ;
const double pi = 2*acos(0) ;
#define maxi 40000
#define pf printf
#define sc scanf
#define pb push_back
#define MEM(x,y) (memset((x),(y),sizeof(x)))
#define MIN(x,y) ((x) < (y) ? (x) : (y))
#define MAX(x,y) ((x) > (y) ? (x) : (y))
#define load(array,size) for(int i=0 ; i<size ; i++) cin>>array[i] ;
#define new_line pf("\n")
#define clear_data(array) memset(array,0,sizeof(array))
#define highest_int 2147483647
#define maxi 101
struct edge
{
int u,v,w ;
};
vector<edge> e ;
int pr[maxi],rank_r[maxi] ;
edge get ;
bool comp(edge a, edge b)
{
if(a.w == b.w)
{
if(a.u == b.u)
{
return a.v < b.v ;
}
else
return a.u <b.u ;
}
else
return a.w < b.w ;
}
int find_r(int r)
{
return pr[r]==r ? r : pr[r]= find_r(pr[r]) ;
}
void doUnion(int a, int b)
{
if(rank_r[a]<rank_r[b])
{
pr[a] =b ;
}
else
{
if(rank_r[a] == rank_r[b])
rank_r[a]++ ;
pr[b] =a ;
}
}
void mst(int n)
{
int i,cnt=0 ,u,v;
sort(e.begin(),e.end(),comp) ;
for(i=0 ; i<=n ; i++)
{
pr[i] =i ;
rank_r[i] =i ;
}
for(i=0 ; i<e.size() ; i++)
{
u = find_r(e[i].u) ;
v = find_r(e[i].v) ;
if(u!=v)
{
doUnion(u,v) ;
pf("%c-%c %d\n",e[i].u+'A' , e[i].v+'A', e[i].w) ;
//cnt++ ;
//if(cnt==n-1)
//break ;
}
}
}
int main()
{
int test,cnt =1,city,i,j,p ;
cin>>test ;
while(test--)
{
e.clear() ;
cin>>city ;
for(i=0 ; i<city ; i++)
{
for(j=0 ; j<city ; j++)
{
sc("%d,",&p) ;
if(p)
{
get.u =i<j ? i :j ;
get.v = j >i ? j:i;
get.w =p ;
e.push_back(get) ;
}
}
}
pf("Case %d:\n",cnt++) ;
mst(city) ;
}
return 0;
}
/***
Md. Namzul Hasan
Shahjalal University of Science & Technology,Sylhet.
hasan08sust@gmail.com
***/
#include<bits/stdc++.h>
using namespace std ;
typedef long long ll ;
typedef int in ;
typedef unsigned long long ull ;
const double pi = 2*acos(0) ;
#define maxi 40000
#define pf printf
#define sc scanf
#define pb push_back
#define MEM(x,y) (memset((x),(y),sizeof(x)))
#define MIN(x,y) ((x) < (y) ? (x) : (y))
#define MAX(x,y) ((x) > (y) ? (x) : (y))
#define load(array,size) for(int i=0 ; i<size ; i++) cin>>array[i] ;
#define new_line pf("\n")
#define clear_data(array) memset(array,0,sizeof(array))
#define highest_int 2147483647
#define maxi 101
struct edge
{
int u,v,w ;
};
vector<edge> e ;
int pr[maxi],rank_r[maxi] ;
edge get ;
bool comp(edge a, edge b)
{
if(a.w == b.w)
{
if(a.u == b.u)
{
return a.v < b.v ;
}
else
return a.u <b.u ;
}
else
return a.w < b.w ;
}
int find_r(int r)
{
return pr[r]==r ? r : pr[r]= find_r(pr[r]) ;
}
void doUnion(int a, int b)
{
if(rank_r[a]<rank_r[b])
{
pr[a] =b ;
}
else
{
if(rank_r[a] == rank_r[b])
rank_r[a]++ ;
pr[b] =a ;
}
}
void mst(int n)
{
int i,cnt=0 ,u,v;
sort(e.begin(),e.end(),comp) ;
for(i=0 ; i<=n ; i++)
{
pr[i] =i ;
rank_r[i] =i ;
}
for(i=0 ; i<e.size() ; i++)
{
u = find_r(e[i].u) ;
v = find_r(e[i].v) ;
if(u!=v)
{
doUnion(u,v) ;
pf("%c-%c %d\n",e[i].u+'A' , e[i].v+'A', e[i].w) ;
//cnt++ ;
//if(cnt==n-1)
//break ;
}
}
}
int main()
{
int test,cnt =1,city,i,j,p ;
cin>>test ;
while(test--)
{
e.clear() ;
cin>>city ;
for(i=0 ; i<city ; i++)
{
for(j=0 ; j<city ; j++)
{
sc("%d,",&p) ;
if(p)
{
get.u =i<j ? i :j ;
get.v = j >i ? j:i;
get.w =p ;
e.push_back(get) ;
}
}
}
pf("Case %d:\n",cnt++) ;
mst(city) ;
}
return 0;
}
No comments:
Post a Comment