Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- using namespace std;
- using namespace std;
- const int maxn=1500;
- 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;
- }
- void union_set(int A,int B){
- int rootA=find_root(A);
- int rootB=find_root(B);
- if(rootA!=rootB){
- if(sz[rootA]<sz[rootB]){
- idx[rootA]=idx[rootB];
- sz[rootB]+=sz[rootA];
- }else{
- idx[rootB]=idx[rootA];
- sz[rootA]+=sz[rootB];
- }
- }
- }
- bool check_set(int A,int B){
- return (find_root(A)==find_root(B));
- }
- vector<int>graph[maxn];
- int main()
- {
- init();
- int n;
- cin>>n;
- vector<pair<int ,pair<int,int>>>v;
- for(int i=1;i<n;i++){
- for(int j=i+1;j<=n;j++){
- int x;
- cin>>x;
- v.push_back({x,{i,j}});
- }
- }
- sort(v.begin(),v.end());
- for(int i=0;i<v.size();i++){
- int A=v[i].second.first;
- int B=v[i].second.second;
- int w=v[i].first;
- bool c=check_set(A,B);
- if(c==true){
- continue;
- }else{
- union_set(A,B);
- graph[A].push_back(B);
- graph[B].push_back(A);
- }
- }
- for(int i=1;i<=n;i++){
- cout << graph[i].size() << " ";
- sort(graph[i].begin(), graph[i].end());
- for(int j=0;j<graph[i].size();j++){
- cout<<graph[i][j]<<" ";
- }
- cout<<endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment