-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSocialnePossibleFriends.java
More file actions
116 lines (103 loc) · 2.56 KB
/
SocialnePossibleFriends.java
File metadata and controls
116 lines (103 loc) · 2.56 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/** https://www.spoj.com/problems/SOCIALNE/ #floyd-warshall */
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class SocialnePossibleFriends {
public static int INF = 9999;
public static boolean FloydWarShall(int[][] dist) {
int i, j, k;
int V = dist.length;
for (k = 0; k < V; k++) {
for (i = 0; i < V; i++) {
for (j = 0; j < V; j++) {
if (dist[i][j] > dist[i][k] + dist[k][j]) {
dist[i][j] = dist[i][k] + dist[k][j];
}
}
}
}
// check negative-weight cycles
for (i = 0; i < V; i++) {
for (j = 0; j < V; j++) {
if (dist[i][j] < 0) {
return false;
}
}
}
return true;
}
public static void main(String[] args) {
MyScanner sc = new MyScanner(System.in);
int testcases = sc.nextInt();
for (int t = 0; t < testcases; t++) {
String firstString = sc.next();
int m = firstString.length();
char[][] graph = new char[m][m];
graph[0] = firstString.toCharArray();
for (int i = 1; i < m; i++) {
String tmp = sc.next();
graph[i] = tmp.toCharArray();
}
int[][] dist = new int[m][m];
for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
dist[i][j] = graph[i][j] == 'Y' ? 1 : INF;
}
dist[i][i] = 0;
}
FloydWarShall(dist);
int maxFriends = -1;
int id = -1;
for (int i = 0; i < m; i++) {
int count = 0;
for (int j = 0; j < m; j++) {
if (dist[i][j] == 2) {
count++;
}
}
if (count > maxFriends) {
maxFriends = count;
id = i;
}
}
System.out.println(id + " " + maxFriends);
}
}
}
class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner(InputStream is) {
br = new BufferedReader(new InputStreamReader(is));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}