AtCoder ABC 016 A&B&C AtCoder - 016
private void solveA() {
int m = nextInt();
int d = nextInt();
out.print(m % d == 0 ? "YES" : "NO");
}
B - A±B Problem
private void solveB() {
int a = nextInt();
int b = nextInt();
int c = nextInt();
boolean p = a + b == c;
boolean m = a - b == c;
if (p && m) {
out.println("?");
} else if (!p && !m) {
out.println("!");
} else if (p && !m) {
out.println("+");
} else if (!p && m) {
out.println("-");
}
}
--Find the distance between the vertices of the undirected graph --With friends at the top --The distance to friends is 1 (the distance between friends is the same) --Friends of friends have a distance of 2 between vertices ――Friend of a friend's friend has a distance of 3 between vertices
It's time to learn how to solve it other than the Worshall Floyd method. .. ..
private void solveC() {
int n = nextInt();
int m = nextInt();
int[] a = new int[m];
int[] b = new int[m];
int[][] graph = new int[n][n];
for (int i = 0; i < n; i++) {
//Fill with MAX
Arrays.fill(graph[i], Integer.MAX_VALUE / 2);
//The cost of moving to yourself is 0
graph[i][i] = 0;
}
for (int i = 0; i < m; i++) {
a[i] = nextInt() - 1;
b[i] = nextInt() - 1;
//Creating an adjacency matrix
graph[a[i]][b[i]] = 1;
graph[b[i]][a[i]] = 1;
}
//Floyd-Warshall Floyd method
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k < n; k++) {
graph[j][k] = Integer.min(graph[j][k], graph[j][i] + graph[i][k]);
}
}
}
/*
*Travel cost from yourself
* 1=friend
* 2=A friend of a friend
* 3=Friend of a friend friend of a friend
* 4=Friend of a friend Friend of a friend ...
*Continued below
*/
for (int i = 0; i < graph.length; i++) {
int res = 0;
for (int j = 0; j < graph.length; j++) {
res += graph[i][j] == 2 ? 1 : 0;
}
out.println(res);
}
}
Recommended Posts