题解:哈希表拉链法| #谁是你的潜在朋友#
谁是你的潜在朋友
https://www.nowcoder.com/practice/0177394fb25b42b48657bc2b1c6f9fcc
#include<iostream> #include<algorithm> #include<cstring> using namespace std; const int N = 210; int h[N],e[N],ne[N],idx; int sum[N]; void add(int a,int b){ e[idx] = b; ne[idx] = h[a]; h[a] = idx++; } int main() { memset(h,-1,sizeof h); int n,m; cin>>n>>m; for(int i =0;i<n;i++){ int num; cin>>num; add(num,i); } for(int i = 0;i<N;i++){ int count = 0; for(int j = h[i];j!=-1;j = ne[j]){ count++; sum[e[j]]++; } if (count>=2){ for(int k = h[i];k!=-1;k = ne[k]){ sum[e[k]]+=(count-1); } } } for(int i =0;i<n;i++){ if (sum[i] == 1){ cout<<"BeiJu"<<endl; }else{ cout<<sum[i]-1<<endl; } } return 0; }