Polycarpus works as a programmer in a start-up social network. His boss gave his a task to develop a mechanism for determining suggested friends. Polycarpus thought much about the task and came to the folowing conclusion.
Let's say that all friendship relationships in a social network are given as m username pairs ai, bi (ai ≠ bi). Each pair ai, bi means that users ai and bi are friends. Friendship is symmetric, that is, if ai is friends with bi, then bi is also friends with ai. User y is a suggested friend for user x, if the following conditions are met:
- x ≠ y;
- x and y aren't friends;
- among all network users who meet the first two conditions, user y has most of all common friends with user x. User z is a common friend of user x and user y (z ≠ x, z ≠ y), if x and z are friends, and y and z are also friends.
Your task is to help Polycarpus to implement a mechanism for determining suggested friends.
The first line contains a single integer m (1 ≤ m ≤ 5000) — the number of pairs of friends in the social network. Next m lines contain pairs of names of the users who are friends with each other. The i-th line contains two space-separated names ai and bi (ai ≠ bi). The users' names are non-empty and consist of at most 20 uppercase and lowercase English letters.
It is guaranteed that each pair of friends occurs only once in the input. For example, the input can't contain x, y and y, x at the same time. It is guaranteed that distinct users have distinct names. It is guaranteed that each social network user has at least one friend. The last thing guarantees that each username occurs at least once in the input.
In the first line print a single integer n — the number of network users. In next n lines print the number of suggested friends for each user. In the i-th line print the name of the user ci and the number of his suggested friends di after a space.
You can print information about the users in any order.
5
Mike Gerald
Kate Mike
Kate Tank
Gerald Tank
Gerald David
5
Mike 1
Gerald 1
Kate 1
Tank 1
David 2
4
valera vanya
valera edik
pasha valera
igor valera
5
valera 0
vanya 3
edik 3
pasha 3
igor 3
In the first test case consider user David. Users Mike and Tank have one common friend (Gerald) with David. User Kate has no common friends with David. That's why David's suggested friends are users Mike and Tank.
分析:主要是map和vector的使用。map建立对应关系,count()判断某串是否已经存在,set_intersection()直接求两人的公共朋友的数量。复杂度为O(n^2),推导源于某大牛,如下:
It seems to be cubic, but its not, for one guy you do at most N*A + M operations (A is the number of his friends), the total complexity will be N*A + M + N*B + M + NC + M + ... = N(A+B+C..) + N*M = N*M + N*M = O(M^2).
#include<cstdio> #include<cstring> #include<queue> #include<vector> #include<algorithm> #include<set> #include<iostream> #include<map> #include<string> using namespace std; #define N 50010 int n; int inter[N]; map <string,int> stoi; map <int,string> itos; vector<int> add[N]; int vis[N]; string s1[N],s2[N]; int main(){ int m,fa,fb,i,j,max,ans,temp; scanf("%d",&m); while(m--){ cin>>s1[m]>>s2[m]; if(stoi.count(s1[m])==0){ stoi[s1[m]]=n; itos[n++]=s1[m]; } if(stoi.count(s2[m])==0){ stoi[s2[m]]=n; itos[n++]=s2[m]; } fa=stoi[s1[m]]; fb=stoi[s2[m]]; add[fa].push_back(fb); add[fb].push_back(fa); } for(i=0;i<n;++i) sort(add[i].begin(),add[i].end()); printf("%d\n",n); for(i=0;i<n;++i){ ans=max=0; cout<<itos[i]<<" "; memset(vis,0,sizeof(vis)); for(j=0;j<add[i].size();++j) vis[add[i][j]]=1; for(j=0;j<n;++j){ if(vis[j] || i==j){ continue; } temp=set_intersection(add[i].begin(),add[i].end(),add[j].begin(),add[j].end(),inter)-inter; // printf("%d %d\n",set_intersection(add[i].begin(),add[i].end(),add[j].begin(),add[j].end(),inter),inter); if(temp>max){ ans=1; max=temp; // printf("max=%d ans=%d\n",max,ans); } else if(temp==max){ ++ans; // printf("j=%d ans=%d\n",j,ans); } } printf("%d\n",ans); } return 0; }