标签:sort排序 i++ spec clu ace print freopen stream main
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.
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≤K≤N), 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.
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.
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
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\n", v[v.size()-1], v[v.size()-2], v[v.size()-3]);
}
return 0;
}
浙大保研2019年上机题 7-4 Index of Popularity (30分)
标签:sort排序 i++ spec clu ace print freopen stream main
原文地址:https://www.cnblogs.com/littlepage/p/13194717.html