并查集 推荐使用 1176 ms
import java.io.*;
import java.util.*;
class Main {
static int idx, n, k;
static int[] p = new int[1010];
static int[] w = new int[1010];
static int[] cnt = new int[1010];
static boolean[] flag = new boolean[1010];
//并查集初始化
static {
for (int i = 0; i < 1010; i++) {
p[i] = i;
cnt[i] = 1;
}
}
static int find(int x) {
if (p[x] != x) p[x] = find(p[x]);
return p[x];
}
static Map<String, Integer> n2i = new HashMap<>();
static Map<Integer, String> i2n = new HashMap<>();
public static void main(String[] args) throws IOException {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(isr);
OutputStreamWriter osw = new OutputStreamWriter(System.out);
BufferedWriter out = new BufferedWriter(osw);
String[] strs = in.readLine().split(" ");
n = Integer.parseInt(strs[0]);
k = Integer.parseInt(strs[1]);
for (int i = 0; i < n; i++) {
strs = in.readLine().split(" ");
String s = strs[0];
String e = strs[1];
int v = Integer.parseInt(strs[2]);
if (!n2i.containsKey(s)) {
n2i.put(s, idx);
i2n.put(idx++, s);
}
if (!n2i.containsKey(e)) {
n2i.put(e, idx);
i2n.put(idx++, e);
}
int si = n2i.get(s);
int ei = n2i.get(e);
w[si] += v;
w[ei] += v;
int fs = find(si);
int fe = find(ei);
if (fs != fe) {
p[fs] = fe;
cnt[fe] += cnt[fs];
}
}
//记录组织头领姓名
List<String> leaderNames = new ArrayList<>();
//遍历每个组织
for (int i = 0; i < idx; i++) {
int father = find(i);
if (cnt[father] < 3 || flag[i]) continue;
flag[i] = true;
int maxW = w[i];
int sum = w[i];
int maxId = i;
for (int j = 0; j < idx; j++) {
if (flag[j] || find(j) != father) continue;
flag[j] = true;
sum += w[j];
if (maxW < w[j]) {
maxW = w[j];
maxId = j;
}
}
sum /= 2;
if (sum > k) leaderNames.add(i2n.get(maxId));
}
int gNum = leaderNames.size();
Collections.sort(leaderNames);
out.write(gNum + "\n");
for (String name : leaderNames)
out.write(name + " " + cnt[find(n2i.get(name))] + "\n");
in.close();
isr.close();
out.flush();
out.close();
osw.close();
}
}
DFS 难写、难以debug,不推荐 1186 ms
import java.io.*;
import java.util.*;
class Main {
static int idx, hidx, n, k;
//稀疏图,邻接表存储
static int[] h = new int[1021];
static int[] e = new int[2021];
static int[] ne = new int[2021];
static {
Arrays.fill(h, -1);
}
//每个点的总权重
static int[] wi = new int[1021];
//标记是否已访问此点
static boolean[] flag = new boolean[1021];
//组织的总权重和最大成员权重
static int sum, maxW, maxId;
//存储组织成员数
static Map<String, Integer> gN = new HashMap<>();
static Map<String, Integer> n2i = new HashMap<>();
static Map<Integer, String> i2n = new HashMap<>();
static void insert(String start, String end, int v) {
if (!n2i.containsKey(start)) n2i.put(start, idx++);
if (!n2i.containsKey(end)) n2i.put(end, idx++);
int si = n2i.get(start);
int ei = n2i.get(end);
i2n.put(si, start);
i2n.put(ei, end);
e[hidx] = ei;
ne[hidx] = h[si];
h[si] = hidx++;
e[hidx] = si;
ne[hidx] = h[ei];
h[ei] = hidx++;
wi[si] += v;
wi[ei] += v;
}
//返回此组织的成员数量
static int dfs(int x) {
flag[x] = true;
if (wi[x] > maxW) {
maxW = wi[x];
maxId = x;
}
sum += wi[x];
int mem = 1;
for (int i = h[x]; i != -1; i = ne[i]) {
int nx = e[i];
if (!flag[nx]) {
mem += dfs(nx);
}
}
return mem;
}
public static void main(String[] args) throws IOException {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(isr);
OutputStreamWriter osw = new OutputStreamWriter(System.out);
BufferedWriter out = new BufferedWriter(osw);
String[] strs = in.readLine().split(" ");
n = Integer.parseInt(strs[0]);
k = Integer.parseInt(strs[1]);
for (int i = 0; i < n; i++) {
strs = in.readLine().split(" ");
String s = strs[0];
String e = strs[1];
int v = Integer.parseInt(strs[2]);
insert(s, e, v);
}
//记录组织头领姓名
List<String> leaderNames = new ArrayList<>();
int members = 0, gNum = 0;
for (int i = 0; i < idx; i++) {
if (!flag[i]) {
sum = 0;
maxW = -1;
members = dfs(i);
sum /= 2;
if (members > 2 && sum > k) {
gNum++;
String name = i2n.get(maxId);
leaderNames.add(name);
gN.put(name, members);
}
}
}
out.write(gNum + "\n");
Collections.sort(leaderNames);
for (String name : leaderNames) {
int mem = gN.get(name);
out.write(name + " " + mem + "\n");
}
in.close();
isr.close();
out.flush();
out.close();
osw.close();
}
}