Some people are friends with each other (no to the graph with a non-edge), now to add a friend to recommend the function, if for x, there is y,x and y is not a friend, but Y and X have a common friend, and the most common friends, then Y can be recommended to X. Ask for everyone, how many people are worth recommending.
Map map, and then use Set_intersection () to find the intersection
This question learns a bit about the use of set_intersection ():
Set_intersection (Fir.begin (), Fir.end (), Sec.begin (), Sec.end (), Thr.begin ());
Fir, sec, thr are all vector types, and of course int[] types
Fir and SEC are required to be ordered, otherwise errors will occur.
Sort (Fir.begin (), Fir.end ());
The function returns a tail pointer with a value of intersection, which is similar to the concept of thr.end ().
This problem is the intersection of everyone asking for others, calculate how many people in the intersection can be.
#include <bits/stdc++.h>#defineEPS 1e-9#defineAll (x) X.begin (), X.end ()#defineINS (x) Inserter (X,x.begin ())#definefor (i,j,k) for (int i=j;i<=k;i++)#defineMAXN 1005#defineMAXM 40005#defineINF 0X3FFFFFFFusing namespaceStd;typedefLong LongLL;inti,j,k,n,m,x,y,t,ans,big,cas,num,len,an,bn,ans_num,cur;BOOLFlag;map<string,int>Mp;vector<int> st[10002];intvis[10002];inttmp[10002];stringa,b,rev[10002];intMain () {scanf ("%d",&N); for(i=1; i<=n;i++) st[i].clear (); Mp.clear (); memset (TMP,0,sizeof(TMP)); Num=0; an=bn=0; Ans_num=0; Cur=0; Ans=0; for(i=1; i<=n;i++) {cin>>a>>b; if(!Mp.count (a)) {Mp[a]=++num; Rev[num]=A; an=num; }Elsean=Mp[a]; if(!Mp.count (b)) {Mp[b]=++num; Rev[num]=b; Bn=num; }Elsebn=Mp[b]; St[an].push_back (BN); St[bn].push_back (an); } for(i=1; i<=num;i++) {sort (St[i].begin (), St[i].end ()); } printf ("%d\n", num); for(i=1; i<=num;i++) { intPre=0; Cur=0; ans=0; ans_num=0; memset (Vis,0,sizeof(VIS)); for(Vector <int>:: Iterator It=st[i].begin (); It!=st[i].end (); it++) {vis[*it]=1; } Vis[i]=1; for(j=1; j<=num;j++) { if(Vis[j])Continue; Cur=set_intersection (St[i].begin (), St[i].end (), St[j].begin (), St[j].end (), TMP)-tmp; if(cur>ans_num) {Ans_num=cur; Ans=1; }Else if(cur==ans_num) {ans++; }} cout<<rev[i]<<" "<<ans<<Endl; } return 0;}
Codeforces 245G suggested Friends-intersection set_intersection ()