浙江大学 计算机与软件学院 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 10​5), 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.

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

分析:给出一个图的各边,及图中若干顶点,找出其中度最大的三个点。
注意点:
1、这道题侧重点不是图的遍历,而是图的存储(当顶点数>100000时,怎么存储?),邻接矩阵是不行,邻接表也有点大,遍历的话会超时,此时可以采用结构体数组来存储边。
2、找出序列中度最大的点,采用map来记录顶点与度。我用了两种方法:1:遍历序列顶点,判断序列中两个点是否有边,有则度++。复杂度n(v*v)太高(运行超时了)2:遍历所有边,判断边的两个点是否在序列中,复杂度n(e)。

#include 
#include 
#include 
#include 
using namespace std;
struct node {
	int index,deg;
	friend bool operator <(node n1,node n2) {
		if(n1.deg != n2.deg)return n1.deg > n2.deg;
		else return n1.index edge;
int main(){
	int n,m,k,p1,p2,v;
	cin>>n>>m;
	for(int i=0; i>p1>>p2;
		edge.push_back({p1,p2});
	}
	while(scanf("%d",&k)!=EOF,k!=0) {
		set s;
		map M;
		set ans;
		for(int i=0; i>v;
			ans.insert({v,0});
			s.insert(v);
		}
		for(int i=0; iindex;
			count ++ ;
			if(count>3) break;
		}
		cout<

你可能感兴趣的:(图论)