QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#181954#6414. Classical Maximization ProblemalexzhanUSTL 201ms3636kbC++174.1kb2023-09-17 06:10:472023-09-17 06:10:47

Judging History

你现在查看的是最新测评结果

  • [2023-09-17 06:10:47]
  • 评测
  • 测评结果:TL
  • 用时:201ms
  • 内存:3636kb
  • [2023-09-17 06:10:47]
  • 提交

answer

#include <iostream>
#include <map>
#include <vector>
#include <set>

using namespace std;

class Neighbor {
public:
    long long axisValue;
    int edgeIndex;
    bool marked = false;
    Neighbor(long long axisValue, int edgeIndex): axisValue(axisValue), edgeIndex(edgeIndex) {}
};

void buildDFSTree(map<long long, vector<Neighbor>>& adjacencyMatrix, set<long long>& visited, long long node) {
    // *NOTE: the edge from a node to its parent is marked (false) as a back edge.
    visited.insert(node);
    for (Neighbor& edge : adjacencyMatrix[node]) {
        if (visited.find(edge.axisValue) == visited.end()) { // not visited before
            edge.marked = true;
            buildDFSTree(adjacencyMatrix, visited, edge.axisValue);
        }
        // if it is already visited, it is a back edge, so you don't really need to do anything
    }
}

bool pairEdges(map<long long, vector<Neighbor>>& adjacencyMatrix, vector<pair<int, int>>& pairs, long long node, long long parent) {
    // return true if you used the edge from the node to its parent.
    // return false otherwise.
    set<int> notUsedEdges;
    int parentEdge = 0;
    for (Neighbor& neighbor : adjacencyMatrix[node]) {
        if (neighbor.marked) { // it is a spanning neighbor
            if (!pairEdges(adjacencyMatrix, pairs, neighbor.axisValue, node)) { // if neighbor is not used
                notUsedEdges.insert(neighbor.edgeIndex);
            }
        } else { // it is a back neighbor.
            if (neighbor.axisValue != parent) {
                notUsedEdges.insert(neighbor.edgeIndex);
            } else {
                parentEdge = neighbor.edgeIndex;
            }
        }
    }
    while (notUsedEdges.size() >= 2) {
        int a = *notUsedEdges.begin();
        notUsedEdges.erase(notUsedEdges.begin());
        int b = *notUsedEdges.begin();
        notUsedEdges.erase(notUsedEdges.begin());
        pairs.emplace_back(a, b);
    }
    if (notUsedEdges.size() == 1) {
        if (parentEdge != 0) {
            pairs.emplace_back(parentEdge, *notUsedEdges.begin());
        }
        return true;
    } else {
        return false;
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int numOfTestCases;
    cin >> numOfTestCases;

    for (int i = 0; i < numOfTestCases; i++) {
        int n;
        cin >> n;
        map<long long, vector<Neighbor>> adjacencyMatrix;
        set<long long> allVertices;
        for (int j = 0; j < (2 * n); j++) {
            long long x, y;
            cin >> x >> y;
            // TODO: why do we need this?
            y += 2e9 + 1;
            adjacencyMatrix[x].emplace_back(y, j + 1);
            adjacencyMatrix[y].emplace_back(x, j + 1);
            // Bug: what if x and y are the same? //// I literally did y += 2e9 + 1 in order to make sure that x and y are not the same
            allVertices.insert(x);
            allVertices.insert(y);
        }
        set<long long> visited;
        vector<pair<int, int>> pairs;
        for (auto& vertex : allVertices) {
            if (visited.find(vertex) == visited.end()) {
                buildDFSTree(adjacencyMatrix, visited, vertex);
                pairEdges(adjacencyMatrix, pairs, vertex, -1e9 - 1);
            }
        }

        cout << pairs.size() << "\n";
        set<int> points;
        for (int j = 1; j <= 2 * n; j++) {
            points.insert(j);
        }
        for (auto& pair : pairs) {
            cout << pair.first << " " << pair.second << "\n";
            points.erase(pair.first);
            points.erase(pair.second);
        }
        // There is a more efficient way of doing it.
        // One option is to use vector<boolean> to track what points are used already, and then insert all unused
        // points to a separate vector. Then scan through the vector once to print out pairs.
        // Another option is to reuse set<int> to keep track of unused points, and then use iterator arithmetics (iterator++).
        auto iterator = points.begin();
        while (iterator != points.end()) {
            cout << *iterator << " " << *(++iterator) << "\n";
            iterator++;
        }
    }
    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 3556kb

input:

3
2
0 0
0 1
1 0
1 1
2
0 0
0 1
0 2
0 3
2
0 0
1 1
2 2
3 3

output:

2
4 2
1 3
2
1 2
3 4
0
1 2
3 4

result:

ok ok (3 test cases)

Test #2:

score: 0
Accepted
time: 195ms
memory: 3560kb

input:

10000
2
-107276936 -310501829
419434212 585811870
-65754386 -491212232
381152038 897148193
3
-474045168 493506332
299114415 540203303
165808153 983551
-506936261 -694189769
766718170 -725540031
975267148 -593051087
1
-818952276 -762387923
584023914 -612401389
6
-77701228 -266484128
659434465 6322062...

output:

0
1 2
3 4
0
1 2
3 4
5 6
0
1 2
0
1 2
3 4
5 6
7 8
9 10
11 12
0
1 2
3 4
5 6
7 8
9 10
11 12
13 14
0
1 2
0
1 2
3 4
5 6
7 8
9 10
11 12
13 14
15 16
17 18
19 20
21 22
23 24
25 26
27 28
29 30
31 32
33 34
35 36
37 38
39 40
41 42
43 44
45 46
47 48
49 50
51 52
53 54
55 56
57 58
59 60
61 62
63 64
65 66
0
1 2
3 4...

result:

ok ok (10000 test cases)

Test #3:

score: 0
Accepted
time: 187ms
memory: 3556kb

input:

10000
1
999855386 999580905
999342928 999615227
21
999601032 999015398
999155628 999176944
999309856 999524434
999121011 999509537
999323572 999685730
999272272 999769606
999450559 999390758
999632027 999178534
999024993 999463838
999784856 999374197
999980525 999366771
999241260 999516879
999599548...

output:

0
1 2
0
1 2
3 4
5 6
7 8
9 10
11 12
13 14
15 16
17 18
19 20
21 22
23 24
25 26
27 28
29 30
31 32
33 34
35 36
37 38
39 40
41 42
0
1 2
3 4
5 6
7 8
9 10
11 12
13 14
15 16
17 18
19 20
21 22
23 24
25 26
27 28
29 30
0
1 2
3 4
5 6
7 8
9 10
11 12
13 14
15 16
17 18
19 20
21 22
23 24
25 26
27 28
0
1 2
3 4
5 6
7...

result:

ok ok (10000 test cases)

Test #4:

score: 0
Accepted
time: 196ms
memory: 3576kb

input:

10000
5
999984799 999981445
999958394 999984217
999994978 999981258
999955539 999938710
999936554 999963561
999907222 999907508
999938166 999941959
999910567 999986887
999901446 999961092
999994730 999963038
5
999916115 999962400
999948250 999940355
999954204 999920844
999928148 999990369
999978118 ...

output:

0
1 2
3 4
5 6
7 8
9 10
0
1 2
3 4
5 6
7 8
9 10
0
1 2
3 4
5 6
7 8
9 10
11 12
13 14
15 16
17 18
19 20
21 22
0
1 2
3 4
5 6
7 8
0
1 2
3 4
5 6
7 8
9 10
11 12
13 14
15 16
17 18
19 20
21 22
23 24
25 26
27 28
29 30
31 32
0
1 2
3 4
5 6
7 8
9 10
0
1 2
3 4
5 6
7 8
9 10
11 12
13 14
15 16
0
1 2
3 4
5 6
7 8
9 10
1...

result:

ok ok (10000 test cases)

Test #5:

score: 0
Accepted
time: 201ms
memory: 3636kb

input:

10000
1
999990146 999993828
999995909 999996353
56
999999851 999991179
999997250 999997987
999990590 999997316
999997350 999996856
999997034 999996236
999999396 999996897
999991180 999993309
999991265 999995185
999993952 999994054
999990210 999994471
999993201 999995893
999997170 999998971
999998201...

output:

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

result:

ok ok (10000 test cases)

Test #6:

score: 0
Accepted
time: 189ms
memory: 3544kb

input:

10000
5
999999432 999999813
999999271 999999233
999999043 999999606
999999523 999999406
999999564 999999274
999999641 999999102
999999903 999999858
999999058 999999098
999999974 999999119
999999643 999999620
5
999999370 999999738
999999181 999999907
999999163 999999783
999999393 999999086
999999661 ...

output:

0
1 2
3 4
5 6
7 8
9 10
0
1 2
3 4
5 6
7 8
9 10
0
1 2
3 4
5 6
0
1 2
3 4
5 6
7 8
9 10
1
9 10
1 2
3 4
5 6
7 8
11 12
13 14
15 16
17 18
19 20
21 22
23 24
25 26
27 28
29 30
31 32
33 34
1
13 12
1 2
3 4
5 6
7 8
9 10
11 14
15 16
0
1 2
3 4
0
1 2
3 4
1
10 44
1 2
3 4
5 6
7 8
9 11
12 13
14 15
16 17
18 19
20 21
22...

result:

ok ok (10000 test cases)

Test #7:

score: -100
Time Limit Exceeded

input:

10000
14
-369804569 -904204119
526374829 -824374353
-127549933 -904204119
-68608787 929413707
-68608787 -363454459
526374829 929413707
693313139 -824374353
-127549933 -726843762
526374829 -904204119
526374829 -363454459
526374829 -409731440
693313139 -726843762
693313139 929413707
-68608787 -8243743...

output:

22
10 19
5 28
11 25
16 26
14 23
4 13
6 18
17 20
15 24
9 10
2 11
14 21
7 22
13 24
27 28
8 12
17 18
19 22
3 26
9 23
1 27
20 21
31
21 12
9 34
25 36
13 24
27 29
30 40
15 33
26 35
22 39
7 32
4 38
17 23
5 29
16 20
36 40
6 12
15 24
28 32
10 31
8 19
3 33
1 18
10 13
16 35
37 38
14 17
19 25
2 39
18 20
31 34
1...

result: