zoukankan      html  css  js  c++  java
  • 浙大保研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;
    }
    
  • 相关阅读:
    366. Find Leaves of Binary Tree输出层数相同的叶子节点
    716. Max Stack实现一个最大stack
    515. Find Largest Value in Each Tree Row查找一行中的最大值
    364. Nested List Weight Sum II 大小反向的括号加权求和
    156. Binary Tree Upside Down反转二叉树
    698. Partition to K Equal Sum Subsets 数组分成和相同的k组
    244. Shortest Word Distance II 实现数组中的最短距离单词
    187. Repeated DNA Sequences重复的DNA子串序列
    java之hibernate之基于主键的双向一对一关联映射
    java之hibernate之基于主键的单向一对一关联映射
  • 原文地址:https://www.cnblogs.com/littlepage/p/13194717.html
Copyright © 2011-2022 走看看