给定一个只包含小写字母的字符串,请你找到第一个仅出现一次的字符。如果没有,输出no。c语言
时间: 2023-10-30 20:45:38 浏览: 81
import java.util.*;
public class Main {
static int n;
static int[] head, nxt, to, w;
static int cnt = 0;
public static void add(int u, int v, int weight) {
nxt[cnt] = head[u];
to[cnt] = v;
w[cnt] = weight;
head[u] = cnt++;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
head = new int[n + 1];
nxt = new int[(n - 1) * 2];
to = new int[(n - 1) * 2];
w = new int[(n - 1) * 2];
Arrays.fill(head, -1);
for (int i = 1; i < n; i++) {
int u = sc.nextInt();
int v = sc.nextInt();
int weight = sc.nextInt();
add(u, v, weight);
add(v, u, weight);
}
int ans = dfs(1, 0, 0);
System.out.println(ans);
}
public static int dfs(int u, int fa, int color) {
int ans = 0;
for (int i = head[u]; i != -1; i = nxt[i]) {
int v = to[i];
int weight = w[i];
if (v == fa) {
continue;
}
int tmp = dfs(v, u, 0);
if (color == 0) {
tmp = Math.max(tmp, dfs(v, u, 1) + weight);
}
ans += tmp;
}
return ans;
}
}
阅读全文