-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJ.java
More file actions
147 lines (119 loc) · 4.11 KB
/
Copy pathJ.java
File metadata and controls
147 lines (119 loc) · 4.11 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.StringTokenizer;
public class J {
static ArrayList<Integer>[] adj;
static boolean[] visited;
static int[] subtreeSize;
static long globalAnswer = 0;
static int n, k, K_prime;
static long[] allCounts;
static long[] subtreeCounts;
static void dfsSize(int u, int p) {
subtreeSize[u] = 1;
for (int v : adj[u]) {
if (v != p && !visited[v]) {
dfsSize(v, u);
subtreeSize[u] += subtreeSize[v];
}
}
}
static int getCentroid(int u, int p, int totalSize) {
for (int v : adj[u]) {
if (v != p && !visited[v] && subtreeSize[v] > totalSize / 2) {
return getCentroid(v, u, totalSize);
}
}
return u;
}
static int dfsCollect(int u, int p, int dist, long[] counts) {
counts[dist]++;
int max_d = dist;
for (int v : adj[u]) {
if (v != p && !visited[v]) {
max_d = Math.max(max_d, dfsCollect(v, u, dist + 1, counts));
}
}
return max_d;
}
static long countValidPairs(long[] counts, int max_d, int K_prime) {
long ans = 0;
long[] suffix = new long[max_d + 2];
for (int i = max_d; i >= 0; i--) {
suffix[i] = suffix[i + 1] + counts[i];
}
for (int d1 = 0; d1 <= max_d; d1++) {
if (counts[d1] == 0) continue;
int d2_min = Math.max(0, K_prime - d1);
if (d2_min <= max_d) {
ans += counts[d1] * suffix[d2_min];
}
}
long self_pairs = 0;
for (int d = 0; d <= max_d; d++) {
if (2 * d >= K_prime) {
self_pairs += counts[d];
}
}
return (ans - self_pairs) / 2;
}
static void decompose(int u) {
dfsSize(u, -1);
int totalSize = subtreeSize[u];
int c = getCentroid(u, -1, totalSize);
visited[c] = true;
allCounts = new long[totalSize + 1];
int maxDepthAll = 0;
allCounts[0] = 1;
for (int v : adj[c]) {
if (!visited[v]) {
subtreeCounts = new long[totalSize + 1];
int maxDepthSub = 0;
maxDepthSub = dfsCollect(v, c, 1, subtreeCounts);
globalAnswer -= countValidPairs(subtreeCounts, maxDepthSub, K_prime);
for (int d = 1; d <= maxDepthSub; d++) {
allCounts[d] += subtreeCounts[d];
}
maxDepthAll = Math.max(maxDepthAll, maxDepthSub);
}
}
globalAnswer += countValidPairs(allCounts, maxDepthAll, K_prime);
for (int v : adj[c]) {
if (!visited[v]) {
decompose(v);
}
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
StringTokenizer st = new StringTokenizer(br.readLine());
n = Integer.parseInt(st.nextToken());
k = Integer.parseInt(st.nextToken());
K_prime = n - 1 - k;
if (K_prime <= 0) {
out.println((long)n * (n - 1) / 2);
out.flush();
return;
}
adj = new ArrayList[n];
for (int i = 0; i < n; i++) {
adj[i] = new ArrayList<>();
}
for (int i = 0; i < n - 1; i++) {
st = new StringTokenizer(br.readLine());
int u = Integer.parseInt(st.nextToken()) - 1;
int v = Integer.parseInt(st.nextToken()) - 1;
adj[u].add(v);
adj[v].add(u);
}
visited = new boolean[n];
subtreeSize = new int[n];
decompose(0);
out.println(globalAnswer);
out.flush();
}
}