Head of a Gang
题目描述
One way that the police finds the head of a gang is to check people’s phone calls. If there is a phone call between A and B, we say that A and B is related. The weight of a relation is defined to be the total time length of all the phone calls made between the two persons. A “Gang” is a cluster of more than 2 persons who are related to each other with total relation weight being greater than a given threshold K. In each gang, the one with maximum total weight is the head. Now given a list of phone calls, you are supposed to find the gangs and the heads.
Input Specification:
Each input file contains one test case. For each case, the first line contains two positive numbers N and K (both less than or equal to 1000), the number of phone calls and the weight threshold, respectively. Then N lines follow, each in the following format:
Name1 Name2 Time
where Name1 and Name2 are the names of people at the two ends of the call, and Time is the length of the call. A name is a string of three capital letters chosen from A-Z. A time length is a positive integer which is no more than 1000 minutes.
Output Specification:
For each test case, first print in a line the total number of gangs. Then for each gang, print in a line the name of the head and the total number of the members. It is guaranteed that the head is unique for each gang. The output must be sorted according to the alphabetical order of the names of the heads.
Sample Input 1:
8 59
AAA BBB 10
BBB AAA 20
AAA CCC 40
DDD EEE 5
EEE DDD 70
FFF GGG 30
GGG HHH 20
HHH FFF 10Sample Output 1:
2
AAA 3
GGG 3Sample Input 2:
8 70
AAA BBB 10
BBB AAA 20
AAA CCC 40
DDD EEE 5
EEE DDD 70
FFF GGG 30
GGG HHH 20
HHH FFF 10
Sample Output 2:
0
题目分析
题目可以用并查集算法来做,也可以用深搜来解决。先上并查集的算法把,虽然在官网上超时,但也得到了25分,但在牛客网上却返回非零….。深搜算法在牛客网上是通过了,但是在官网上却只得到了22分,参考自以一位神秘大神的博客,PAT如果单纯要刷分,不要选用Java。
- 并查集算法版本
代码如下:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.TreeMap;
public class Main {
static int[] arr;
public static void main(String[] args) throws IOException {
R.init(System.in);
int M = R.nextInt(), K = R.nextInt();
arr = new int[2*M];
int count = 0;
int[] weigh = new int[2*M];
int sum = 0;
Map<String, Integer> map = new HashMap<>();
Map<Integer, String> map2 = new HashMap<>();
for (int i=0; i<M; i++) {
String n1 = R.next(), n2 = R.next();
int t = R.nextInt();
if (!map.containsKey(n1)) {
arr[sum] = sum;
map.put(n1, sum);
map2.put(sum ++, n1);
}
if (!map.containsKey(n2)) {
arr[sum] = sum;
map.put(n2, sum);
map2.put(sum ++, n2);
}
int m = map.get(n1), n = map.get(n2);
weigh[m] += t;
weigh[n] += t;
union(m, n);
}
R.close();
TreeMap<String, Integer> result = new TreeMap<>();
boolean[] b = new boolean[sum];
for (int i=0; i<sum; i++) {
if (!b[i]) {
b[i] = true;
int w = weigh[i], max = i, num = 1;
for (int j=i+1; j<sum; j++) {
if (find(arr[j]) == find(arr[i])) {
b[j] = true;
num ++;
w += weigh[j];
max = weigh[j] > weigh[max] ? j : max;
}
}
if (w / 2 > K && num > 2) {
count ++;
result.put(map2.get(max), num);
}
}
}
System.out.println(count);
for (int i=0; i<count; i++) {
Map.Entry<String, Integer> e = result.pollFirstEntry();
System.out.println(e.getKey() + " " + e.getValue());
}
}
private static void union(int m, int n) {
int p = find(m), q = find(n);
if (p != q) {
arr[q] = p;
}
}
private static int find(int q) {
while (q != arr[q]) q = arr[q];
return q;
}
}
class R {
static BufferedReader br;
static StringTokenizer st;
static void init(InputStream in) {
br = new BufferedReader(new InputStreamReader(in));
st = new StringTokenizer("");
}
static String next() throws IOException {
while (!st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt(next());
}
static void close() {
if (br != null)
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
- 深搜版本
代码如下:
package PAT;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.TreeMap;
public class Main {
static int count;
static Map<String, Integer> m1 = new HashMap<>();
static Map<Integer, String> m2 = new HashMap<>();
static int[][] G = new int[2010][2010];
static int[] W = new int[2010];
static int K;
static boolean[] b = new boolean[2010];
static TreeMap<String, Integer> result = new TreeMap<>();
public static void main(String[] args) throws IOException {
R.init(System.in);
int M = R.nextInt();
K = R.nextInt();
for (int i=0; i<M; i++) {
String s1 = R.next(), s2 = R.next();
int temp = R.nextInt();
int id1 = getId(s1);
int id2 = getId(s2);
W[id1] += temp;
W[id2] += temp;
G[id1][id2] += temp;
G[id2][id1] += temp;
}
for (int i=0; i<count; i++) {
if (!b[i]) {
int[] a = {i, 0, 0};
dfs(i, a);
if (a[1] > 2 && a[2] > K) {
result.put(m2.get(a[0]), a[1]);
}
}
}
int total = result.size();
System.out.println(total);
for (int i=0; i<total; i++) {
Map.Entry<String, Integer> temp = result.pollFirstEntry();
System.out.println(temp.getKey() + " " + temp.getValue());
}
}
private static void dfs(int i, int[] a) {
b[i] = true;
a[1] ++;
if (W[i] > W[a[0]]) a[0] = i;
for (int j=0; j<count; j++) {
if (G[i][j] > 0) {
a[2] += G[i][j];
G[i][j] = G[j][i] = 0;
if (!b[j]) dfs(j, a);
}
}
}
private static int getId(String s1) {
if (!m1.containsKey(s1)) {
m1.put(s1, count);
m2.put(count, s1);
return count ++;
} else return m1.get(s1);
}
}
class R {
static BufferedReader br;
static StringTokenizer st;
static void init(InputStream in) {
br = new BufferedReader(new InputStreamReader(in));
st = new StringTokenizer("");
}
static String next() throws IOException {
while (!st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt(next());
}
static void close() {
if (br != null)
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}