Skip to content

Commit d5e9f8e

Browse files
authored
[New Problem] Graph Isomorphism (180) and Reduced Binary Quadratic Assignment (181) (#68)
* 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
1 parent d1d94f9 commit d5e9f8e

18 files changed

Lines changed: 3096 additions & 0 deletions

File tree

algorithmic/problems/180/chk.cc

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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(2, 2000);
9+
int m = inf.readInt(1, n * (n - 1) / 2);
10+
11+
// Read G1 edges
12+
set<pair<int,int>> E1;
13+
for (int i = 0; i < m; i++) {
14+
int u = inf.readInt(1, n);
15+
int v = inf.readInt(1, n);
16+
if (u == v) quitf(_wa, "self-loop in G1");
17+
if (u > v) swap(u, v);
18+
E1.insert({u, v});
19+
}
20+
21+
// Read G2 edges
22+
vector<pair<int,int>> E2(m);
23+
for (int i = 0; i < m; i++) {
24+
int u = inf.readInt(1, n);
25+
int v = inf.readInt(1, n);
26+
if (u == v) quitf(_wa, "self-loop in G2");
27+
if (u > v) swap(u, v);
28+
E2[i] = {u, v};
29+
}
30+
31+
// Read permutation p (G2 -> G1)
32+
vector<int> p(n + 1), used(n + 1, 0);
33+
for (int i = 1; i <= n; i++) {
34+
p[i] = ouf.readInt(1, n);
35+
if (used[p[i]]) {
36+
quitf(_wa, "not a permutation");
37+
}
38+
used[p[i]] = 1;
39+
}
40+
41+
// Count matched edges
42+
int matched = 0;
43+
for (auto &e : E2) {
44+
int u = p[e.first];
45+
int v = p[e.second];
46+
if (u > v) swap(u, v);
47+
if (E1.count({u, v})) {
48+
matched++;
49+
}
50+
}
51+
52+
double score = 0.0;
53+
if (m > 0) score = (double)matched / m;
54+
score = max(0.0, min(1.0, score));
55+
56+
// EXACT same output format as old checker
57+
quitp(score, "Ratio: %.6f", score);
58+
}
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: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Graph Isomorphism
2+
3+
## Problem
4+
5+
You are given two undirected graphs with the same number of vertices.
6+
Your task is to find a permutation of vertices of the second graph that makes it as isomorphic as possible to the first graph.
7+
8+
This is the Graph Isomorphism problem, evaluated with a soft scoring rule.
9+
10+
## Input Format
11+
12+
- Line 1: two integers n and m
13+
(2 ≤ n ≤ 2000, 1 ≤ m ≤ n*(n-1)/2)
14+
- Next m lines: two integers u and v
15+
(1 ≤ u, v ≤ n, u ≠ v)
16+
These describe edges of the first graph G₁. Note that there are no duplicate edges.
17+
- Next m lines: two integers u and v
18+
(1 ≤ u, v ≤ n, u ≠ v)
19+
These describe edges of the second graph G₂. Note that there are no duplicate edges.
20+
21+
## Output Format
22+
23+
- Output exactly one line:
24+
- n integers p₁, p₂, ..., pₙ
25+
- This is a permutation of {1, 2, ..., n}
26+
- pᵢ = j means vertex i of G₂ is mapped to vertex j of G₁
27+
28+
## Scoring
29+
30+
Let:
31+
32+
- E₁ = set of edges in G₁
33+
- E₂ = set of edges in G₂
34+
35+
Define:
36+
matched_edges = |{(u,v) : (p(u), p(v)) ∈ E₁ and (u,v) ∈ E₂}|
37+
total_edges = m
38+
39+
Your score is:
40+
score = matched_edges / total_edges
41+
42+
Notes:
43+
- The score is always in the range [0, 1]
44+
- Perfect isomorphism yields score = 1
45+
- Any valid permutation is accepted and scored

algorithmic/problems/180/testdata/1.ans

Whitespace-only changes.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
5 7
2+
1 4
3+
1 5
4+
2 4
5+
2 5
6+
3 4
7+
3 5
8+
4 5
9+
1 3
10+
3 5
11+
1 2
12+
2 5
13+
1 4
14+
4 5
15+
1 5

algorithmic/problems/180/testdata/2.ans

Whitespace-only changes.
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
20 48
2+
1 9
3+
1 10
4+
1 11
5+
1 17
6+
2 3
7+
2 10
8+
2 14
9+
2 18
10+
3 5
11+
3 7
12+
3 10
13+
3 16
14+
3 19
15+
3 20
16+
4 5
17+
4 8
18+
5 7
19+
5 12
20+
5 13
21+
5 14
22+
5 19
23+
6 7
24+
6 10
25+
6 11
26+
6 14
27+
7 10
28+
7 11
29+
7 14
30+
8 13
31+
8 14
32+
8 16
33+
9 10
34+
9 13
35+
9 15
36+
10 16
37+
10 19
38+
10 20
39+
11 16
40+
11 17
41+
12 15
42+
12 17
43+
13 18
44+
13 20
45+
14 16
46+
16 19
47+
17 20
48+
18 19
49+
19 20
50+
6 15
51+
15 19
52+
15 18
53+
15 20
54+
5 14
55+
5 19
56+
5 16
57+
1 5
58+
13 14
59+
10 14
60+
14 19
61+
9 14
62+
8 14
63+
4 14
64+
2 13
65+
2 11
66+
10 13
67+
13 17
68+
3 13
69+
13 16
70+
8 13
71+
7 10
72+
7 19
73+
7 18
74+
7 16
75+
10 19
76+
10 18
77+
10 16
78+
3 11
79+
11 16
80+
9 11
81+
6 19
82+
3 6
83+
6 12
84+
9 19
85+
8 19
86+
4 19
87+
9 18
88+
18 20
89+
12 17
90+
17 20
91+
1 3
92+
3 4
93+
9 16
94+
8 9
95+
4 20
96+
1 8
97+
4 8

algorithmic/problems/180/testdata/3.ans

Whitespace-only changes.

0 commit comments

Comments
 (0)