Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const int maxn = 2005;
- int idx[maxn];
- int sz[maxn];
- void init() {
- for(int i = 0; i < maxn; i++) {
- idx[i] = i;
- sz[i] = 1;
- }
- }
- int find_root(int x) {
- while(x != idx[x]) {
- idx[x] = idx[idx[x]];
- x = idx[x];
- }
- return x;
- }
- bool check(int A, int B) {
- return find_root(A) == find_root(B);
- }
- void unite(int A, int B) {
- int rootA = find_root(A);
- int rootB = find_root(B);
- if(rootA != rootB) {
- if(sz[rootA] < sz[rootB]) {
- sz[rootB] += sz[rootA];
- idx[rootA] = idx[rootB];
- }
- else {
- sz[rootA] += sz[rootB];
- idx[rootB] = idx[rootA];
- }
- }
- }
- int main() {
- int n;
- cin >> n;
- init();
- vector<pair<int, pair<int, int>>> graph;
- for(int i = 0; i < n - 1; i++) {
- int node = i + 1;
- for(int j = 0; j < n - i - 1; j++) {
- int cost;
- cin >> cost;
- graph.push_back({cost, {i, node}});
- node++;
- }
- }
- sort(graph.begin(), graph.end());
- vector<pair<int, int>> g[maxn];
- for(int i = 0; i < (int) graph.size(); i++) {
- int A = graph[i].second.first;
- int B = graph[i].second.second;
- int C = graph[i].first;
- if(!check(A, B)) {
- unite(A, B);
- g[A].push_back({B, C});
- g[B].push_back({A, C});
- }
- }
- for(int i = 0; i < n; i++) {
- sort(g[i].begin(), g[i].end());
- cout << (int) g[i].size() << " ";
- for(int j = 0; j < (int) g[i].size(); j++) {
- cout << g[i][j].first + 1 << " ";
- }
- cout << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment