-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMost Popular Friend
62 lines (52 loc) Β· 1.31 KB
/
Most Popular Friend
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <bits/stdc++.h>
using namespace std;
int main() {
// your code goes here
int t;
cin>>t;
while(t--)
{
int n,m_popu=INT_MIN;
cin>>n;
vector<int> size;
vector<vector<int>> g(n,vector<int>(n,101));
for(int i=0;i<n;i++)
g[i][i]=0;
string s;
getline(cin,s); //removeing '\n'
for(int i=0;i<n;i++){
getline(cin,s);
int val=0;
for(int j=0;j<s.length();j++){
if(s[j]==' ' || j==s.length()-1){
if(j==s.length()-1)
val=(val*10)+(s[j]-'0');
g[i][val-1]=1;
val=0;
}
else
val=(val*10)+(s[j]-'0'); //converting string to int
}
}
for(int k=0;k<n;k++){
for(int i=0;i<n;i++){
for(int j=0;j<n;j++)
g[i][j]=min(g[i][j],g[i][k]+g[k][j]);
}
}
int ans;
double avg=1000000.0;
for(int i=0;i<n;i++){
double sum=0;
for(auto& x:g[i])
sum+=x;
double z=(double)sum/n;
if(z < avg){
avg=z;
ans=i;
}
}
cout<<ans+1<<" "<<fixed<<setprecision(6)<<avg<<endl;
}
return 0;
}