Skip to content

Commit e5f979e

Browse files
authored
[New Problem] Max-Cut (192) and Reduced Max-2-SAT (193) (#73)
* Fix: Support unbounded ratio in scoring formula for p36, p119, p120 * Fix: Support unbounded ratio in scoring formula for p125 * Add Problem 174, 175 * Fix: problem 174, 175 larger public dataset * Fix: Problem 175 Data Range * Add Problem 176, 177 * Add Problem 178, 179 * Add Problem 180, 181 * Revert "Add Problem 180, 181" This reverts commit 94de9f3. * Add Problem 180, 181 * Add Problem 192, 193
1 parent 232380b commit e5f979e

18 files changed

Lines changed: 620 additions & 0 deletions

File tree

algorithmic/problems/192/chk.cc

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include "testlib.h"
2+
#include <bits/stdc++.h>
3+
using namespace std;
4+
5+
int main(int argc, char* argv[]) {
6+
registerTestlibCmd(argc, argv);
7+
8+
int n = inf.readInt(1, 1000);
9+
int m = inf.readInt(0, 20000);
10+
11+
vector<pair<int,int>> edges(m);
12+
for (int i = 0; i < m; i++) {
13+
int u = inf.readInt(1, n);
14+
int v = inf.readInt(1, n);
15+
if (u == v) {
16+
quitf(_wa, "self-loop on node %d", u);
17+
}
18+
edges[i] = {u - 1, v - 1};
19+
}
20+
21+
vector<int> side(n);
22+
for (int i = 0; i < n; i++) {
23+
side[i] = ouf.readInt(0, 1);
24+
}
25+
26+
int cut = 0;
27+
for (auto &[u, v] : edges) {
28+
if (side[u] != side[v]) cut++;
29+
}
30+
31+
double ratio;
32+
if (m == 0) {
33+
ratio = 1.0;
34+
} else {
35+
ratio = (double)cut / (double)m;
36+
}
37+
38+
ratio = max(0.0, min(1.0, ratio));
39+
quitp(ratio, "Ratio: %.6f", ratio);
40+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
type: default
2+
time: 1s
3+
memory: 1024m
4+
subtasks:
5+
- score: 100
6+
n_cases: 3
7+
checker: chk.cc
8+
checker_type: testlib
9+
filename: std.cc
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Max-Cut
2+
3+
## Problem
4+
5+
You are given an undirected graph with n vertices and m edges.
6+
Your task is to partition the vertices into two sets to maximize the number of edges crossing the partition.
7+
8+
An edge is a cut edge if its two endpoints are in different sets.
9+
10+
## Input Format
11+
12+
- Line 1: two integers n and m (1 ≤ n ≤ 1000, 0 ≤ m ≤ 20000)
13+
- Next m lines: two integers u v (1 ≤ u, v ≤ n, u ≠ v)
14+
15+
The graph may be disconnected.
16+
There are no multiple edges or self-loops.
17+
18+
## Output Format
19+
20+
- Output exactly one line:
21+
- n integers s₁ s₂ … sₙ
22+
- each sᵢ ∈ {0, 1}
23+
- sᵢ = 0 means vertex i is in set S
24+
- sᵢ = 1 means vertex i is in set T
25+
26+
## Scoring
27+
28+
Let:
29+
- m be the number of edges
30+
- c be the number of cut edges
31+
32+
Score is defined as:
33+
- If m > 0: score = c / m
34+
- If m = 0: score = 1
35+
36+
The score is clamped to [0, 1].

algorithmic/problems/192/testdata/1.ans

Whitespace-only changes.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
10 1
2+
4 10

algorithmic/problems/192/testdata/2.ans

Whitespace-only changes.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
50 9
2+
5 23
3+
6 12
4+
9 28
5+
10 17
6+
15 49
7+
18 32
8+
19 46
9+
24 29
10+
32 40

algorithmic/problems/192/testdata/3.ans

Whitespace-only changes.
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
200 135
2+
1 135
3+
2 46
4+
2 135
5+
4 38
6+
4 141
7+
4 200
8+
5 121
9+
5 197
10+
6 22
11+
6 100
12+
8 55
13+
8 120
14+
9 135
15+
10 75
16+
12 16
17+
14 84
18+
14 186
19+
15 84
20+
15 169
21+
15 180
22+
16 134
23+
18 194
24+
19 40
25+
20 42
26+
20 139
27+
20 186
28+
21 118
29+
22 148
30+
23 69
31+
23 193
32+
25 70
33+
26 165
34+
27 157
35+
28 156
36+
29 128
37+
32 145
38+
34 65
39+
35 114
40+
35 198
41+
36 56
42+
36 93
43+
36 121
44+
36 170
45+
37 41
46+
37 158
47+
38 117
48+
39 123
49+
41 63
50+
41 75
51+
41 78
52+
41 151
53+
41 189
54+
43 121
55+
44 60
56+
44 112
57+
47 129
58+
47 196
59+
49 106
60+
51 114
61+
51 156
62+
51 177
63+
52 189
64+
59 101
65+
60 91
66+
60 135
67+
61 104
68+
61 114
69+
61 118
70+
61 133
71+
63 140
72+
63 144
73+
64 70
74+
66 95
75+
67 194
76+
70 107
77+
70 175
78+
71 158
79+
78 148
80+
79 169
81+
82 95
82+
87 194
83+
89 122
84+
89 137
85+
90 158
86+
92 145
87+
92 168
88+
92 177
89+
94 154
90+
95 102
91+
95 143
92+
96 171
93+
101 167
94+
102 123
95+
102 187
96+
108 131
97+
108 169
98+
108 195
99+
110 137
100+
110 168
101+
111 152
102+
112 118
103+
112 142
104+
112 168
105+
112 175
106+
113 135
107+
113 194
108+
115 152
109+
116 134
110+
118 137
111+
119 140
112+
119 159
113+
120 122
114+
120 146
115+
120 169
116+
123 174
117+
124 132
118+
124 189
119+
126 181
120+
132 135
121+
135 189
122+
137 157
123+
137 173
124+
145 151
125+
153 169
126+
154 189
127+
156 165
128+
158 193
129+
164 191
130+
166 186
131+
167 180
132+
168 196
133+
180 199
134+
183 198
135+
189 190
136+
192 194

algorithmic/problems/193/chk.cc

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include "testlib.h"
2+
#include <bits/stdc++.h>
3+
using namespace std;
4+
5+
int main(int argc, char* argv[]) {
6+
registerTestlibCmd(argc, argv);
7+
8+
int n = inf.readInt(1, 1000);
9+
int m = inf.readInt(0, 40000);
10+
11+
vector<array<int,2>> clauses(m);
12+
for (int i = 0; i < m; i++) {
13+
int a = inf.readInt(-n, n);
14+
int b = inf.readInt(-n, n);
15+
if (a == 0 || b == 0) {
16+
quitf(_wa, "literal cannot be zero");
17+
}
18+
clauses[i] = {a, b};
19+
}
20+
21+
vector<int> val(n + 1);
22+
for (int i = 1; i <= n; i++) {
23+
val[i] = ouf.readInt(0, 1);
24+
}
25+
26+
int sat = 0;
27+
for (auto &cl : clauses) {
28+
bool ok = false;
29+
for (int k = 0; k < 2; k++) {
30+
int x = cl[k];
31+
int v = abs(x);
32+
bool lit = (x > 0 ? val[v] == 1 : val[v] == 0);
33+
if (lit) {
34+
ok = true;
35+
break;
36+
}
37+
}
38+
if (ok) sat++;
39+
}
40+
41+
double score = 1.0;
42+
if (m > 0) score = (double)sat / m;
43+
score = max(0.0, min(1.0, score));
44+
45+
quitp(score, "Ratio: %.6f", score);
46+
}

0 commit comments

Comments
 (0)