Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- #define MAXN 100
- int leader[MAXN];
- int G[MAXN][3];
- int N, M;
- int find(int u){
- return leader[u];
- }
- void merge(int l1,int l2){
- for(int i=1;i<=N;i++)
- if(leader[i]==l2)
- leader[i]=l1;
- }
- void bubble_sort_level2(){
- for( int i = M-2; i >= 0; i--){
- for( int j = 0; j <= i; j++){
- if( G[j][2] > G[j+1][2] ){
- int swap;
- swap = G[j][0];
- G[j][0] = G[j+1][0];
- G[j+1][0] = swap;
- swap = G[j][1];
- G[j][1] = G[j+1][1];
- G[j+1][1] = swap;
- swap = G[j][2];
- G[j][2] = G[j+1][2];
- G[j+1][2] = swap;
- }
- }
- }
- }
- int main(){
- int l1,l2;
- cin>>N>>M;
- for(int i=1; i<=M; i++ ){
- leader[i] = i;
- cin >> G[i][0] >> G[i][1] >> G[i][2];
- }
- bubble_sort_level2();
- for(int i=1; i<=M; i++ ){
- l1=find(G[i][0]);
- l2=find(G[i][1]);
- if(l1!=l2){
- merge(l1,l2);
- cout << G[i][0] << " " << G[i][1] << endl;
- }
- }
- return EXIT_SUCCESS;
- }
- /*
- Дърво с цени на ребрата Връх->Връх->Ребро.Цена (Input):
- 10 14
- 5 9 1
- 5 10 1
- 9 10 1
- 2 5 2
- 3 5 2
- 1 2 3
- 4 9 3
- 7 10 3
- 6 10 4
- 1 6 5
- 3 7 5
- 1 3 8
- 2 4 10
- 4 8 11
- Минимално Покриващо Дърво (Expected Output)
- 5 9
- 5 10
- 2 5
- 3 5
- 1 2
- 4 9
- 7 10
- 6 10
- 4 8
- */
Advertisement
Add Comment
Please, Sign In to add comment