398. Friends of Friends
Memory limit: 65536 kilobytes
output: standard
Social networks are very popular now. They use different types of relationships to organize individual users in a network. In this problem friendship is used as a method to connect users. For each user you are given the list of his friends. Consider friendship as a symmetric relation, so if user a is a friend of user b then b is a friend of a.
A friend of a friend for a is such a user c that c is not a friend of a, but there is such b that b is a friend of a and c is a friend of b. Obviously c ≠ a.
Your task is to find the list of friends of friends for the given user x.
sample input | sample output |
4 2 1 2 2 1 3 2 4 2 1 3 | 1 4 |
sample input | sample output |
4 1 3 4 3 2 3 1 3 4 3 1 2 4 3 1 2 3 | 0 |
#include <bits/stdc++.h>
using namespace std;
set<int> s;
int m[55][55];
int v[55];
void solve(){
int n,x;
scanf("%d %d",&n,&x);
for(int i=1;i<=n;i++){
int a;
scanf("%d",&a);
while(a--){
int b;
scanf("%d",&b);
m[i][b]=1;
if(i==x){
v[b]=1;
}
}
}
for(int i=1;i<=n;i++){
if(m[x][i]==1){
for(int j=1;j<=n;j++){
if(m[i][j]==1&&v[j]!=1&&j!=x){
s.insert(j);
}
}
}
}
printf("%d\n",s.size());
set<int>::iterator ite;
for(ite=s.begin();ite!=s.end();++ite){
printf("%d\n",*ite);
}
}
int main(){
solve();
return 0;
}