• 浙大保研2019年上机题 7-4 Index of Popularity (30分)


    7-4 Index of Popularity (30分)

    The index of popularity (IP) of someone in his/her circle of friends is defined to be the number of friends he/she has in that circle. Now you are supposed to list the members in any given friend circle with top 3 IP's.

    Input Specification:

    Each input file contains one test case. Each case starts with a line containing 2 positive integers N and M (both no more than 105), which are the total number of people and the number of friend relations, respectively. Hence the people here are numbered from 1 to N.

    Then M lines follow, each contains the indices of a pair of friends, separated by a space. It is assumed that if A is a friend of B, then B is a friend of A.

    Then several queries follow, each occupies a line. For each line of query, K (3≤KN), the total number of members in this friend circle is given first, with K indices of members follow. It is guaranteed that all the indices in a circle are distinct.

    The input ends when K is zero, and this case must NOT be processed.

    Output Specification:

    For each query, print in a line the members with top 3 indices of popularity in descending order of their IP's. If there is a tie, output the one with the smaller number. The numbers must be separated by exactly 1 space, and there must be no extra space at the beginning or the end of the line.

    Sample Input:

    8 10
    2 1
    1 3
    1 4
    1 5
    5 8
    3 5
    2 3
    6 3
    4 6
    3 4
    7 8 1 2 3 4 6 5
    4 1 3 5 2
    4 8 7 4 2
    0
    

    Sample Output:

    3 1 4
    1 3 2
    2 4 7
    

    这题考察人际关系网络,给出一个人际关系的网,给出一组人,求出每个人与其他人关系最密切的3个,如果有多个,则输出最小的那个。

    我们这题可以巧妙采用sort排序,c++无敌(嘻嘻)。

    #include <iostream>
    #include <vector>
    #include <unordered_map>
    #include <algorithm>
    using namespace std;
    int N, M, K, a, b;
    vector<pair<int, int>> e;
    unordered_map<int, int> degree;
    bool cmp(int& m, int& n) {
       if(degree[m] != degree[n]) return degree[m] < degree[n];
       return m > n;
    }
    int main() {
       // freopen("in.txt", "r", stdin);
       // freopen("out.txt", "w", stdout);
       scanf("%d%d", &N, &M);
       while(M--) {
           scanf("%d%d", &a, &b);
           e.push_back({a, b});
       }
       while(1) {
           scanf("%d", &K);
           if(K == 0) break;
           degree.clear();
           vector<int> v(K);
           unordered_map<int, int> exist;
           for(int i = 0; i < K; i++) {
               scanf("%d", &v[i]);
               exist[v[i]] = 1;
           }
           for(auto x: e) {
               if(exist[x.first] && exist[x.second]) {
                   degree[x.first]++;
                   degree[x.second]++;
               }
           }
           sort(v.begin(), v.end(), cmp);
           printf("%d %d %d
    ", v[v.size()-1], v[v.size()-2], v[v.size()-3]);
       }
       return 0;
    }
    
  • 相关阅读:
    (转) mysql的分区技术 .
    (转) MySQL中索引的限制
    单键索引还是组合索引
    什么是挂载?mount的用处在哪?
    java中的try-catch-finally中的return的执行顺序
    eclipse中spring配置文件的自动提示和namespace的添加
    try catch 语句中有return 的各类情况
    IOC与DI的理解
    对已经存在的没有唯一标识的表添加一个自增的id字段(利用序列sequence)操作过程
    mysql5.7导出数据提示--secure-file-priv选项问题的解决方法
  • 原文地址:https://www.cnblogs.com/littlepage/p/13194717.html
Copyright © 2020-2023  润新知