Skip to content

Commit 46b250b

Browse files
committed
modify code
1 parent ff99ad1 commit 46b250b

File tree

3 files changed

+219
-2
lines changed

3 files changed

+219
-2
lines changed

src/class166/Code01_SegmentTreeDivideAndConquer1.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public static void dfs(int l, int r, int i) {
113113
}
114114
}
115115

116-
public static void main(String[] args) throws IOException {
116+
public static void main(String[] args) {
117117
FastIO io = new FastIO(System.in, System.out);
118118
n = io.nextInt();
119119
m = io.nextInt();

src/class166/Code02_MinimumMexTree1.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public static int dfs(int l, int r, int i) {
109109
return ans;
110110
}
111111

112-
public static void main(String[] args) throws IOException {
112+
public static void main(String[] args) {
113113
FastIO io = new FastIO(System.in, System.out);
114114
n = io.nextInt();
115115
m = io.nextInt();
Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
package class166;
2+
3+
// 独特事件,java版
4+
// 测试链接 : https://www.luogu.com.cn/problem/CF1681F
5+
// 测试链接 : https://codeforces.com/problemset/problem/1681/F
6+
// 提交以下的code,提交时请把类名改成"Main",可以通过所有测试用例
7+
8+
import java.io.IOException;
9+
import java.io.InputStream;
10+
import java.io.OutputStream;
11+
12+
public class Code03_UniqueOccurrences1 {
13+
14+
public static int MAXN = 500001;
15+
public static int MAXT = 10000001;
16+
public static int n, m;
17+
18+
public static int[] x = new int[MAXN];
19+
public static int[] y = new int[MAXN];
20+
public static int[] c = new int[MAXN];
21+
22+
public static int[] father = new int[MAXN];
23+
public static int[] siz = new int[MAXN];
24+
public static int[][] rollback = new int[MAXN][2];
25+
public static int opsize;
26+
27+
public static int[] headc = new int[MAXN];
28+
public static int[] nextc = new int[MAXN];
29+
public static int[] toc = new int[MAXN];
30+
public static int cntc = 0;
31+
32+
public static int[] heads = new int[MAXT];
33+
public static int[] nexts = new int[MAXT];
34+
public static int[] tos = new int[MAXT];
35+
public static int cnts = 0;
36+
37+
public static long ans = 0;
38+
39+
public static void addEdgeC(int u, int v) {
40+
nextc[++cntc] = headc[u];
41+
toc[cntc] = v;
42+
headc[u] = cntc;
43+
}
44+
45+
public static void addEdgeS(int u, int v) {
46+
nexts[++cnts] = heads[u];
47+
tos[cnts] = v;
48+
heads[u] = cnts;
49+
}
50+
51+
public static int find(int i) {
52+
while (i != father[i]) {
53+
i = father[i];
54+
}
55+
return i;
56+
}
57+
58+
public static void union(int x, int y) {
59+
int fx = find(x);
60+
int fy = find(y);
61+
if (siz[fx] < siz[fy]) {
62+
int tmp = fx;
63+
fx = fy;
64+
fy = tmp;
65+
}
66+
father[fy] = fx;
67+
siz[fx] += siz[fy];
68+
rollback[++opsize][0] = fx;
69+
rollback[opsize][1] = fy;
70+
}
71+
72+
public static void undo() {
73+
int fx = rollback[opsize][0];
74+
int fy = rollback[opsize--][1];
75+
father[fy] = fy;
76+
siz[fx] -= siz[fy];
77+
}
78+
79+
public static void add(int jobl, int jobr, int jobv, int l, int r, int i) {
80+
if (jobl <= l && r <= jobr) {
81+
addEdgeS(i, jobv);
82+
} else {
83+
int mid = (l + r) >> 1;
84+
if (jobl <= mid) {
85+
add(jobl, jobr, jobv, l, mid, i << 1);
86+
}
87+
if (jobr > mid) {
88+
add(jobl, jobr, jobv, mid + 1, r, i << 1 | 1);
89+
}
90+
}
91+
}
92+
93+
public static void dfs(int l, int r, int i) {
94+
int unionCnt = 0;
95+
for (int ei = heads[i], fx, fy; ei > 0; ei = nexts[ei]) {
96+
fx = find(x[tos[ei]]);
97+
fy = find(y[tos[ei]]);
98+
if (fx != fy) {
99+
union(fx, fy);
100+
unionCnt++;
101+
}
102+
}
103+
if (l == r) {
104+
for (int ei = headc[l], fx, fy; ei > 0; ei = nextc[ei]) {
105+
fx = find(x[toc[ei]]);
106+
fy = find(y[toc[ei]]);
107+
ans += (long) siz[fx] * siz[fy];
108+
}
109+
} else {
110+
int mid = (l + r) >> 1;
111+
dfs(l, mid, i << 1);
112+
dfs(mid + 1, r, i << 1 | 1);
113+
}
114+
for (int k = 1; k <= unionCnt; k++) {
115+
undo();
116+
}
117+
}
118+
119+
public static void main(String[] args) {
120+
FastIO io = new FastIO(System.in, System.out);
121+
n = io.nextInt();
122+
for (int i = 1; i < n; i++) {
123+
x[i] = io.nextInt();
124+
y[i] = io.nextInt();
125+
c[i] = io.nextInt();
126+
}
127+
for (int i = 1; i < n; i++) {
128+
addEdgeC(c[i], i);
129+
if (c[i] > 1) {
130+
add(1, c[i] - 1, i, 1, n, 1);
131+
}
132+
if (c[i] < n) {
133+
add(c[i] + 1, n, i, 1, n, 1);
134+
}
135+
}
136+
for (int i = 1; i <= n; i++) {
137+
father[i] = i;
138+
siz[i] = 1;
139+
}
140+
dfs(1, n, 1);
141+
io.writelnLong(ans);
142+
io.flush();
143+
}
144+
145+
// 读写工具类
146+
static class FastIO {
147+
private final InputStream is;
148+
private final OutputStream os;
149+
private final byte[] inbuf = new byte[1 << 16];
150+
private int lenbuf = 0;
151+
private int ptrbuf = 0;
152+
private final StringBuilder outBuf = new StringBuilder();
153+
154+
public FastIO(InputStream is, OutputStream os) {
155+
this.is = is;
156+
this.os = os;
157+
}
158+
159+
private int readByte() {
160+
if (ptrbuf >= lenbuf) {
161+
ptrbuf = 0;
162+
try {
163+
lenbuf = is.read(inbuf);
164+
} catch (IOException e) {
165+
throw new RuntimeException(e);
166+
}
167+
if (lenbuf == -1) {
168+
return -1;
169+
}
170+
}
171+
return inbuf[ptrbuf++] & 0xff;
172+
}
173+
174+
private int skip() {
175+
int b;
176+
while ((b = readByte()) != -1) {
177+
if (b > ' ') {
178+
return b;
179+
}
180+
}
181+
return -1;
182+
}
183+
184+
public int nextInt() {
185+
int b = skip();
186+
if (b == -1) {
187+
throw new RuntimeException("No more integers (EOF)");
188+
}
189+
boolean negative = false;
190+
if (b == '-') {
191+
negative = true;
192+
b = readByte();
193+
}
194+
int val = 0;
195+
while (b >= '0' && b <= '9') {
196+
val = val * 10 + (b - '0');
197+
b = readByte();
198+
}
199+
return negative ? -val : val;
200+
}
201+
202+
public void writelnLong(long x) {
203+
outBuf.append(x).append('\n');
204+
}
205+
206+
public void flush() {
207+
try {
208+
os.write(outBuf.toString().getBytes());
209+
os.flush();
210+
outBuf.setLength(0);
211+
} catch (IOException e) {
212+
throw new RuntimeException(e);
213+
}
214+
}
215+
}
216+
217+
}

0 commit comments

Comments
 (0)