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 10005
struct edge
{
int u,v,w ;
bool operator < (const edge& p) const
{
return w<p.w ;
}
};
int pr[maxi] ;
vector<edge> e ;
int find_r(int r)
{
return (pr[r]==r ? r : find_r(pr[r])) ;
}
void mst(int n)
{
vector<int> data ;
data.clear() ;
int i ,cnt =0,u,v,p;
sort(e.begin(), e.end()) ;
for(i=0 ; i<n ; i++)
pr[i] =i ;
for(i=0 ; i<e.size(); i++)
{
u = find_r(e[i].u) ;
v = find_r(e[i].v) ;
if(u!=v)
{
pr[u] =v ;
//cnt++ ;
//if(cnt==n-1)
//break ;
}
else
{
p = e[i].w ;
data.push_back(p) ;
}
}
if(data.size()==0)
pf("forest\n") ;
else
{
for(i=0 ; i<data.size(); i++)
{
pf("%d",data[i]) ;
if(i<data.size()-1)
pf(" ") ;
}
pf("\n") ;
}
}
int main()
{
int n,m ;
int u,v,w,i ;
while(sc("%d %d",&n,&m)&& (n||m))
{
edge get ;
for(i=1 ; i<=m ; i++)
{
cin>>u>>v>>w ;
get.u =u ;
get.v =v ;
get.w =w ;
e.pb(get) ;
}
mst(n) ;
e.clear() ;
}
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 10005
struct edge
{
int u,v,w ;
bool operator < (const edge& p) const
{
return w<p.w ;
}
};
int pr[maxi] ;
vector<edge> e ;
int find_r(int r)
{
return (pr[r]==r ? r : find_r(pr[r])) ;
}
void mst(int n)
{
vector<int> data ;
data.clear() ;
int i ,cnt =0,u,v,p;
sort(e.begin(), e.end()) ;
for(i=0 ; i<n ; i++)
pr[i] =i ;
for(i=0 ; i<e.size(); i++)
{
u = find_r(e[i].u) ;
v = find_r(e[i].v) ;
if(u!=v)
{
pr[u] =v ;
//cnt++ ;
//if(cnt==n-1)
//break ;
}
else
{
p = e[i].w ;
data.push_back(p) ;
}
}
if(data.size()==0)
pf("forest\n") ;
else
{
for(i=0 ; i<data.size(); i++)
{
pf("%d",data[i]) ;
if(i<data.size()-1)
pf(" ") ;
}
pf("\n") ;
}
}
int main()
{
int n,m ;
int u,v,w,i ;
while(sc("%d %d",&n,&m)&& (n||m))
{
edge get ;
for(i=1 ; i<=m ; i++)
{
cin>>u>>v>>w ;
get.u =u ;
get.v =v ;
get.w =w ;
e.pb(get) ;
}
mst(n) ;
e.clear() ;
}
return 0;
}
No comments:
Post a Comment