1107 Social Clusters(30 分)

2018-09-03  本文已影响0人  zjh3029
#include <iostream>
#include <algorithm>
#include <vector>
#include<set>
#include<cmath>
#include<iomanip>
using namespace std;
#define STP system("pause")
#pragma warning(disable:4996)
vector<set<int>> v;
vector<int>v_cnt;
int main()
{
    int M,N,index;
    cin >> M;
    vector<int>s_t;

    for (int i = 0; i < M; i++)
    {
        scanf("%d:", &N);
        set<int>s;
        int time = 1;

        for (int j = 0; j < N; j++)//获得所有的元素
        {
            scanf("%d", &index);
            s.insert(index);
        }

        set<int>s_temp;

        for (auto m:s) {
            for (int t = 0; t < v.size(); t++)
            {
                if (find(v[t].begin(),v[t].end(),m)!=v[t].end())//找到了一个值
                {
                    s_temp.insert(v[t].begin(), v[t].end());
                    v[t].clear();
                    s_t.push_back(t);
                    time+=v_cnt[t];
                    break;//在一行找到,就不会在接下来的几行中找到
                }
            }
        }
        v_cnt.push_back(time);
        s.insert(s_temp.begin(), s_temp.end());
        v.push_back(s);
    }

    int cnt = 0;
    for (int i = 0; i < v.size(); i++)
    {
        if (!v[i].empty())
        {
            cnt++;
        }
    }
    cout << cnt << endl;
    
    vector<int> v_sort;
    for (int i= v_cnt.size()-1;i>=0;i--)
    {
        if (find(s_t.begin(), s_t.end(), i) == s_t.end())
        {
            /*cout << v_cnt[i];
            if (i != 0)
                cout << " ";*/
            v_sort.push_back(v_cnt[i]);
        }
    }

    sort(v_sort.begin(), v_sort.end(), greater<int>());
    for (int i = 0; i < v_sort.size(); i++)
    {
        cout << v_sort[i];
        if (i != v_sort.size()-1)
            cout << " ";
    }
    STP;
    return 0;
}

上一篇下一篇

猜你喜欢

热点阅读