Skip to content

Commit af22f51

Browse files
committed
modify code
1 parent 1066bd6 commit af22f51

File tree

2 files changed

+39
-30
lines changed

2 files changed

+39
-30
lines changed

src/class165/Code03_Envy1.java

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class Code03_Envy1 {
2525
// 节点u、节点v、边权w
2626
public static int[][] edge = new int[MAXN][3];
2727
// 节点u、节点v、边权w、问题编号i
28-
public static int[][] queryEdge = new int[MAXN][4];
28+
public static int[][] queries = new int[MAXN][4];
2929

3030
// 可撤销并查集
3131
public static int[] father = new int[MAXN];
@@ -70,28 +70,33 @@ public static void prepare() {
7070
siz[i] = 1;
7171
}
7272
Arrays.sort(edge, 1, m + 1, (a, b) -> a[2] - b[2]);
73-
Arrays.sort(queryEdge, 1, k + 1, (a, b) -> a[2] != b[2] ? (a[2] - b[2]) : (a[3] - b[3]));
73+
Arrays.sort(queries, 1, k + 1, (a, b) -> a[2] != b[2] ? (a[2] - b[2]) : (a[3] - b[3]));
7474
Arrays.fill(ans, 1, q + 1, true);
7575
}
7676

7777
public static void compute() {
78-
int ei = 1;
78+
int ei = 1, queryId, unionCnt;
7979
for (int l = 1, r = 1; l <= k; l = ++r) {
80-
for (; ei <= m && edge[ei][2] < queryEdge[l][2]; ei++) {
80+
while (r + 1 <= k && queries[l][2] == queries[r + 1][2] && queries[l][3] == queries[r + 1][3]) {
81+
r++;
82+
}
83+
// 添加小于当前边权的边,利用Kruskal算法增加连通性,ei是不回退的
84+
for (; ei <= m && edge[ei][2] < queries[l][2]; ei++) {
8185
if (find(edge[ei][0]) != find(edge[ei][1])) {
8286
union(edge[ei][0], edge[ei][1]);
8387
}
8488
}
85-
while (r + 1 <= k && queryEdge[l][2] == queryEdge[r + 1][2] && queryEdge[l][3] == queryEdge[r + 1][3]) {
86-
r++;
89+
queryId = queries[l][3];
90+
if (!ans[queryId]) {
91+
continue;
8792
}
88-
int unionCnt = 0;
93+
unionCnt = 0;
8994
for (int i = l; i <= r; i++) {
90-
if (find(queryEdge[i][0]) == find(queryEdge[i][1])) {
91-
ans[queryEdge[i][3]] = false;
95+
if (find(queries[i][0]) == find(queries[i][1])) {
96+
ans[queryId] = false;
9297
break;
9398
} else {
94-
union(queryEdge[i][0], queryEdge[i][1]);
99+
union(queries[i][0], queries[i][1]);
95100
unionCnt++;
96101
}
97102
}
@@ -126,10 +131,10 @@ public static void main(String[] args) throws IOException {
126131
for (int j = 1, ei; j <= s; j++) {
127132
in.nextToken();
128133
ei = (int) in.nval;
129-
queryEdge[++k][0] = edge[ei][0];
130-
queryEdge[k][1] = edge[ei][1];
131-
queryEdge[k][2] = edge[ei][2];
132-
queryEdge[k][3] = i;
134+
queries[++k][0] = edge[ei][0];
135+
queries[k][1] = edge[ei][1];
136+
queries[k][2] = edge[ei][2];
137+
queries[k][3] = i;
133138
}
134139
}
135140
prepare();

src/class165/Code03_Envy2.java

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@
2222
// return x.w < y.w;
2323
//}
2424
//
25-
//struct QueryEdge {
25+
//struct Query {
2626
// int u, v, w, i;
2727
//};
2828
//
29-
//bool QueryEdgeCmp(QueryEdge x, QueryEdge y) {
29+
//bool QueryCmp(Query x, Query y) {
3030
// if(x.w != y.w) {
3131
// return x.w < y.w;
3232
// } else {
@@ -38,7 +38,7 @@
3838
//int n, m, q, k;
3939
//
4040
//Edge edge[MAXN];
41-
//QueryEdge queryEdge[MAXN];
41+
//Query queries[MAXN];
4242
//
4343
//int father[MAXN];
4444
//int siz[MAXN];
@@ -81,30 +81,34 @@
8181
// siz[i] = 1;
8282
// }
8383
// sort(edge + 1, edge + m + 1, EdgeCmp);
84-
// sort(queryEdge + 1, queryEdge + k + 1, QueryEdgeCmp);
84+
// sort(queries + 1, queries + k + 1, QueryCmp);
8585
// for (int i = 1; i <= q; i++) {
8686
// ans[i] = true;
8787
// }
8888
//}
8989
//
9090
//void compute() {
91-
// int ei = 1;
91+
// int ei = 1, queryId, unionCnt;
9292
// for (int l = 1, r = 1; l <= k; l = ++r) {
93-
// for (; ei <= m && edge[ei].w < queryEdge[l].w; ei++) {
93+
// while (r + 1 <= k && queries[l].w == queries[r + 1].w && queries[l].i == queries[r + 1].i) {
94+
// r++;
95+
// }
96+
// for (; ei <= m && edge[ei].w < queries[l].w; ei++) {
9497
// if (find(edge[ei].u) != find(edge[ei].v)) {
9598
// Union(edge[ei].u, edge[ei].v);
9699
// }
97100
// }
98-
// while (r + 1 <= k && queryEdge[l].w == queryEdge[r + 1].w && queryEdge[l].i == queryEdge[r + 1].i) {
99-
// r++;
101+
// queryId = queries[l].i;
102+
// if (!ans[queryId]) {
103+
// continue;
100104
// }
101-
// int unionCnt = 0;
105+
// unionCnt = 0;
102106
// for (int i = l; i <= r; i++) {
103-
// if (find(queryEdge[i].u) == find(queryEdge[i].v)) {
104-
// ans[queryEdge[i].i] = false;
107+
// if (find(queries[i].u) == find(queries[i].v)) {
108+
// ans[queryId] = false;
105109
// break;
106110
// } else {
107-
// Union(queryEdge[i].u, queryEdge[i].v);
111+
// Union(queries[i].u, queries[i].v);
108112
// unionCnt++;
109113
// }
110114
// }
@@ -127,10 +131,10 @@
127131
// cin >> s;
128132
// for (int j = 1, ei; j <= s; j++) {
129133
// cin >> ei;
130-
// queryEdge[++k].u = edge[ei].u;
131-
// queryEdge[k].v = edge[ei].v;
132-
// queryEdge[k].w = edge[ei].w;
133-
// queryEdge[k].i = i;
134+
// queries[++k].u = edge[ei].u;
135+
// queries[k].v = edge[ei].v;
136+
// queries[k].w = edge[ei].w;
137+
// queries[k].i = i;
134138
// }
135139
// }
136140
// prepare();

0 commit comments

Comments
 (0)