Skip to content

Commit 6c31a60

Browse files
committed
modify code
1 parent 1952d87 commit 6c31a60

File tree

3 files changed

+241
-63
lines changed

3 files changed

+241
-63
lines changed

src/class183/Code01_Ratio1.java

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
package class183;
2+
3+
// 聪聪可可,java版
4+
// 一共有n个节点,给定n-1条边,每条边有边权,所有节点组成一棵树
5+
// 如果点对(u, v)的简单路径权值和能被3整除,则称这个点对是合法的
6+
// 认为点对(x, x)的简单路径权值和是0,并且认为(x, y)和(y, x)是不同的点对
7+
// 打印(合法点对的数量 / 点对的总数量)的最简分数形式
8+
// 1 <= n、边权 <= 2 * 10^4
9+
// 测试链接 : https://www.luogu.com.cn/problem/P2634
10+
// 提交以下的code,提交时请把类名改成"Main",可以通过所有测试用例
11+
12+
import java.io.IOException;
13+
import java.io.InputStream;
14+
import java.io.OutputStreamWriter;
15+
import java.io.PrintWriter;
16+
17+
public class Code01_Ratio1 {
18+
19+
public static int MAXN = 20001;
20+
public static int n;
21+
22+
public static int[] head = new int[MAXN];
23+
public static int[] nxt = new int[MAXN << 1];
24+
public static int[] to = new int[MAXN << 1];
25+
public static int[] weight = new int[MAXN << 1];
26+
public static int cntg;
27+
28+
public static boolean[] vis = new boolean[MAXN];
29+
public static int[] siz = new int[MAXN];
30+
31+
public static int[] cur = new int[3];
32+
public static int[] all = new int[3];
33+
34+
public static int gcd(int a, int b) {
35+
return b == 0 ? a : gcd(b, a % b);
36+
}
37+
38+
public static void addEdge(int u, int v, int w) {
39+
nxt[++cntg] = head[u];
40+
to[cntg] = v;
41+
weight[cntg] = w;
42+
head[u] = cntg;
43+
}
44+
45+
public static int getSize(int u, int fa) {
46+
siz[u] = 1;
47+
for (int e = head[u]; e > 0; e = nxt[e]) {
48+
int v = to[e];
49+
if (v != fa && !vis[v]) {
50+
siz[u] += getSize(v, u);
51+
}
52+
}
53+
return siz[u];
54+
}
55+
56+
public static int getCentroid(int u, int fa) {
57+
int half = getSize(u, fa) >> 1;
58+
boolean find = false;
59+
while (!find) {
60+
find = true;
61+
for (int e = head[u]; e > 0; e = nxt[e]) {
62+
int v = to[e];
63+
if (v != fa && !vis[v] && siz[v] > half) {
64+
fa = u;
65+
u = v;
66+
find = false;
67+
break;
68+
}
69+
}
70+
}
71+
return u;
72+
}
73+
74+
public static void dfs(int u, int fa, int dis) {
75+
cur[dis % 3]++;
76+
for (int e = head[u]; e > 0; e = nxt[e]) {
77+
int v = to[e];
78+
if (v != fa && !vis[v]) {
79+
dfs(v, u, dis + weight[e]);
80+
}
81+
}
82+
}
83+
84+
public static int calc(int u) {
85+
int ans = 0;
86+
all[0] = all[1] = all[2] = 0;
87+
for (int e = head[u]; e > 0; e = nxt[e]) {
88+
int v = to[e];
89+
int w = weight[e];
90+
if (!vis[v]) {
91+
cur[0] = cur[1] = cur[2] = 0;
92+
dfs(v, u, w);
93+
ans += all[0] * cur[0] * 2 + all[1] * cur[2] * 2 + all[2] * cur[1] * 2 + cur[0] * 2;
94+
all[0] += cur[0];
95+
all[1] += cur[1];
96+
all[2] += cur[2];
97+
}
98+
}
99+
return ans;
100+
}
101+
102+
public static int solve(int u) {
103+
vis[u] = true;
104+
int ans = calc(u);
105+
for (int e = head[u]; e > 0; e = nxt[e]) {
106+
int v = to[e];
107+
if (!vis[v]) {
108+
ans += solve(getCentroid(v, u));
109+
}
110+
}
111+
return ans;
112+
}
113+
114+
public static void main(String[] args) throws Exception {
115+
FastReader in = new FastReader(System.in);
116+
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
117+
n = in.nextInt();
118+
for (int i = 1, u, v, w; i < n; i++) {
119+
u = in.nextInt();
120+
v = in.nextInt();
121+
w = in.nextInt();
122+
addEdge(u, v, w);
123+
addEdge(v, u, w);
124+
}
125+
int a = solve(getCentroid(1, 0)) + n;
126+
int b = n * n;
127+
int c = gcd(a, b);
128+
a /= c;
129+
b /= c;
130+
out.println(a + "/" + b);
131+
out.flush();
132+
out.close();
133+
}
134+
135+
// 读写工具类
136+
static class FastReader {
137+
private final byte[] buffer = new byte[1 << 16];
138+
private int ptr = 0, len = 0;
139+
private final InputStream in;
140+
141+
FastReader(InputStream in) {
142+
this.in = in;
143+
}
144+
145+
private int readByte() throws IOException {
146+
if (ptr >= len) {
147+
len = in.read(buffer);
148+
ptr = 0;
149+
if (len <= 0)
150+
return -1;
151+
}
152+
return buffer[ptr++];
153+
}
154+
155+
int nextInt() throws IOException {
156+
int c;
157+
do {
158+
c = readByte();
159+
} while (c <= ' ' && c != -1);
160+
boolean neg = false;
161+
if (c == '-') {
162+
neg = true;
163+
c = readByte();
164+
}
165+
int val = 0;
166+
while (c > ' ' && c != -1) {
167+
val = val * 10 + (c - '0');
168+
c = readByte();
169+
}
170+
return neg ? -val : val;
171+
}
172+
}
173+
174+
}

src/class183/Code02_Template1.java

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package class183;
22

3-
// 点分治,也叫重心分解,java版
3+
// 点分治模版题,java版
44
// 一共有n个节点,给定n-1条边,每条边有边权,所有节点组成一棵树
55
// 一共有m条查询,每条查询给定数字k,打印树上距离为k的点对是否存在
66
// 1 <= n <= 10^4 1 <= 边权 <= 10^4
@@ -31,13 +31,10 @@ public class Code02_Template1 {
3131

3232
public static boolean[] vis = new boolean[MAXN];
3333
public static int[] siz = new int[MAXN];
34-
public static int[] maxPart = new int[MAXN];
35-
public static int total;
36-
public static int centroid;
3734

38-
public static int[] curDis = new int[MAXN];
35+
public static int[] cur = new int[MAXN];
3936
public static int cntc;
40-
public static int[] allDis = new int[MAXN];
37+
public static int[] all = new int[MAXN];
4138
public static int cnta;
4239
public static boolean[] check = new boolean[MAXV];
4340

@@ -50,28 +47,40 @@ public static void addEdge(int u, int v, int w) {
5047
head[u] = cntg;
5148
}
5249

53-
public static void getCentroid(int u, int fa) {
50+
public static int getSize(int u, int fa) {
5451
siz[u] = 1;
55-
maxPart[u] = 0;
5652
for (int e = head[u]; e > 0; e = nxt[e]) {
5753
int v = to[e];
5854
if (v != fa && !vis[v]) {
59-
getCentroid(v, u);
60-
siz[u] += siz[v];
61-
maxPart[u] = Math.max(siz[v], maxPart[u]);
55+
siz[u] += getSize(v, u);
6256
}
6357
}
64-
maxPart[u] = Math.max(maxPart[u], total - siz[u]);
65-
if (centroid == 0 || maxPart[u] < maxPart[centroid]) {
66-
centroid = u;
58+
return siz[u];
59+
}
60+
61+
public static int getCentroid(int u, int fa) {
62+
int half = getSize(u, fa) >> 1;
63+
boolean find = false;
64+
while (!find) {
65+
find = true;
66+
for (int e = head[u]; e > 0; e = nxt[e]) {
67+
int v = to[e];
68+
if (v != fa && !vis[v] && siz[v] > half) {
69+
fa = u;
70+
u = v;
71+
find = false;
72+
break;
73+
}
74+
}
6775
}
76+
return u;
6877
}
6978

7079
public static void dfs(int u, int fa, int dis) {
7180
if (dis > maxq) {
7281
return;
7382
}
74-
curDis[++cntc] = dis;
83+
cur[++cntc] = dis;
7584
for (int e = head[u]; e > 0; e = nxt[e]) {
7685
int v = to[e];
7786
if (v != fa && !vis[v]) {
@@ -82,41 +91,37 @@ public static void dfs(int u, int fa, int dis) {
8291

8392
public static void calc(int u) {
8493
cnta = 0;
94+
check[0] = true;
8595
for (int e = head[u]; e > 0; e = nxt[e]) {
8696
int v = to[e];
87-
int w = weight[e];
8897
if (!vis[v]) {
8998
cntc = 0;
90-
dfs(v, u, w);
99+
dfs(v, u, weight[e]);
91100
for (int i = 1; i <= m; i++) {
92101
for (int j = 1; j <= cntc; j++) {
93-
if (query[i] - curDis[j] >= 0) {
94-
ans[i] |= check[query[i] - curDis[j]];
102+
if (query[i] - cur[j] >= 0) {
103+
ans[i] |= check[query[i] - cur[j]];
95104
}
96105
}
97106
}
98107
for (int i = 1; i <= cntc; i++) {
99-
allDis[++cnta] = curDis[i];
100-
check[curDis[i]] = true;
108+
all[++cnta] = cur[i];
109+
check[cur[i]] = true;
101110
}
102111
}
103112
}
104113
for (int i = 1; i <= cnta; i++) {
105-
check[allDis[i]] = false;
114+
check[all[i]] = false;
106115
}
107116
}
108117

109118
public static void solve(int u) {
110119
vis[u] = true;
111-
check[0] = true;
112120
calc(u);
113121
for (int e = head[u]; e > 0; e = nxt[e]) {
114122
int v = to[e];
115123
if (!vis[v]) {
116-
total = siz[v];
117-
centroid = 0;
118-
getCentroid(v, u);
119-
solve(centroid);
124+
solve(getCentroid(v, u));
120125
}
121126
}
122127
}
@@ -137,10 +142,7 @@ public static void main(String[] args) throws Exception {
137142
query[i] = in.nextInt();
138143
maxq = Math.max(maxq, query[i]);
139144
}
140-
total = n;
141-
centroid = 0;
142-
getCentroid(1, 0);
143-
solve(centroid);
145+
solve(getCentroid(1, 0));
144146
for (int i = 1; i <= m; i++) {
145147
if (ans[i]) {
146148
out.println("AYE");
@@ -154,7 +156,7 @@ public static void main(String[] args) throws Exception {
154156

155157
// 读写工具类
156158
static class FastReader {
157-
private final byte[] buffer = new byte[1 << 20];
159+
private final byte[] buffer = new byte[1 << 16];
158160
private int ptr = 0, len = 0;
159161
private final InputStream in;
160162

0 commit comments

Comments
 (0)