voidkruskal(vector<node> &arr, vector<bool> &visited){ for(int i = 0; i < arr.size(); i++) cin >> arr[i].head >> arr[i].tail >> arr[i].len; sort(arr.begin(), arr.end(), cmp); for(int i = 0; i < visited.size(); i++) visited[i] = false; int ans = 0; for(int i = 0; i < arr.size(); i++){ if((!visited[arr[i].head]) || (!visited[arr[i].tail])){ visited[arr[i].head] = visited[arr[i].tail] = true; ans += arr[i].len; cout << arr[i].head << "-->" << arr[i].tail << ": " << arr[i].len << endl; } } cout << ans << endl; }
intmain(){ int N, M; //N is the number of edges, M is the number of nodes cin >> N >> M; vector<node> arr(N); vector<bool> visited(M); kruskal(arr, visited); return0; } /* Input: 8 6 0 1 2 0 2 1 1 3 5 2 3 4 1 2 3 2 4 1 4 5 2 3 5 3 Output: 0-->2: 1 2-->4: 1 0-->1: 2 4-->5: 2 3-->5: 3 9 */