1107 Social Clusters (30point (s)) Easy only once * and look into the problem set, we need to pay attention to the next

The basic idea:

Mainly on the investigation and the use of sets, which pay attention to the idea of ​​merging the nodes;

 

key point:

map Sort not seem to be, it can turn into a Vector <pair <string, int >> were sort sort condition rewriting on the line;

#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<vector> 
#include<string>
#include<math.h>
#include<algorithm>
#include<cstring>
#include<map>
#include<queue>
using namespace std;

const int maxn = 1010;
vector<int>father;
vector<int>fin;
int course[maxn];
int n;

void init() {
    father.resize(n+1);
    fill(course, course + maxn, 0);
    for (int i = 0; i < father.size(); i++) {
        father[i] = i;
    }
}

bool cmp(int a, int b) {
    return a > b;
}

int findfather(int n) {
    int temp = n;
    while (father[n] != n) {
        n = father[n];
    }
    /*
    while (father[temp] != n) {
        int a = father[temp];
        father[temp] = n;
        temp = a;
    }
    */
    return n;
}

void contain(int a, int b) {
    int aa = findfather(a);
    int bb = findfather(b);
    if (aa != bb) {
        father[aa] = bb;
    }
}

void cnt() {
    map<int, int>m;
    for (int i = 1; i <= n; i++) {
        int f = findfather(i);
        m[f]++;
    }
    cout << m.size() << endl;
    bool flag = true;
    for (auto it = m.begin(); it != m.end();it++) {
        fin.push_back(it->second);
    }
    sort(fin.begin(), fin.end(), cmp);
    for (int i = 0; i < fin.size(); i++) {
        if (i == 0)
            cout << fin[i];
        else
            cout << " " << fin[i];
    }
}

int main() {
    cin >> n;
    int a,b;
    init();
    for (int i = 1; i <= n; i++) {
        scanf("%d: ", &a);
        //cout << a << endl;
        for (int j = 0; j < a; j++) {
            cin >> b;
            if(Course [B] == 0 ) {
                 // if the first visit to add this program; 
                Course [B] = I;
                 // Added in course; 
            }
             the else {
                 // If this is not the first time; 
                Contain ( I, Course [B]);
                 // the same two persons are combined elective; 
            } 
        } 
    } 
    // constructed, merge; 
    CNT ();
     return  0 ; 
}

 

Guess you like

Origin www.cnblogs.com/songlinxuan/p/12313070.html