Submission details
Task:Järjestys
Sender:Metabolix
Submission time:2025-09-07 13:31:02 +0300
Language:C++ (C++17)
Status:READY
Result:0
Feedback
groupverdictscore
#10
#20
#30
#40
#50
Test results
testverdicttimegroup
#1ACCEPTED0.00 s1, 4, 5details
#2ACCEPTED0.00 s1, 4, 5details
#30.00 s1, 4, 5details
#40.01 s1, 4, 5details
#50.01 s1, 4, 5details
#6ACCEPTED0.01 s1, 2, 4, 5details
#70.01 s1, 3, 4, 5details
#80.01 s1, 4, 5details
#9--2, 4, 5details
#10--3, 4, 5details
#11--4, 5details
#12--4, 5details
#13--4, 5details
#14--4, 5details
#15--2, 5details
#16--3, 5details
#17--5details
#18--5details
#19--5details
#20--5details
#21--5details
#22--5details

Compiler report

input/code.cpp: In lambda function:
input/code.cpp:163:31: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  163 |             if (result.size() == target_depth && (i == target_node || target_node == -1)) {
      |                 ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~

Code

#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <functional>
#include <unordered_set>

using namespace std;

struct Node {
    int i, x, y;
    unordered_set<int> fwd, bwd;
    unordered_set<int> fwd_outside, fwd_inside;
    unordered_set<int> bwd_inside;
    bool visited = false;
    int component = -1;
    int depth = 1;
};

struct Component {
    vector<int> nodes;
    bool visited = false;
    int depth = 1;
    int first_node = -1;
    int last_node = -1;
};

int main() {
    int t = 0;
    cin >> t;
    while (t--) {
        int n = 0;
        cin >> n;
        vector<Node> arr(n);
        for (int i = 0; i < n; i++) {
            arr[i].i = i;
            cin >> arr[i].x >> arr[i].y;
        }

        sort(arr.begin(), arr.end(), [](const Node &a, const Node &b) {
            return a.x != b.x ? a.x < b.x : a.y < b.y;
        });

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (arr[i].y <= arr[j].x) {
                    arr[i].fwd.insert(j);
                    arr[j].bwd.insert(i);
                }
            }
        }

        // Kosarajun algoritmi
        vector<int> order;
        for (auto &node : arr) {
            node.visited = false;
        }
        function<void(int)> dfs1 = [&](int i) {
            arr[i].visited = true;
            for (auto next : arr[i].fwd) {
                if (!arr[next].visited) {
                    dfs1(next);
                }
            }
            order.push_back(i);
        };
        for (int i = 0; i < n; i++) {
            if (!arr[i].visited) {
                dfs1(i);
            }
        }
        reverse(order.begin(), order.end());
        vector<Component> components;
        for (auto &node : arr) {
            node.visited = false;
        }
        function<void(int, vector<int>&)> dfs2 = [&](int i, vector<int>& comp) {
            arr[i].visited = true;
            comp.push_back(i);
            for (auto next : arr[i].bwd) {
                if (!arr[next].visited) {
                    dfs2(next, comp);
                }
            }
        };
        for (auto i : order) {
            if (!arr[i].visited) {
                vector<int> comp;
                dfs2(i, comp);
                components.push_back({comp});
                for (auto idx : comp) {
                    arr[idx].component = components.size() - 1;
                }
            }
        }
        for (const auto &comp : components) {
            for (auto idx : comp.nodes) {
                for (auto next : arr[idx].fwd) {
                    if (arr[next].component != arr[idx].component) {
                        arr[idx].fwd_outside.insert(next);
                    } else {
                        arr[idx].fwd_inside.insert(next);
                    }
                }
                for (auto prev : arr[idx].bwd) {
                    if (arr[prev].component == arr[idx].component) {
                        arr[idx].bwd_inside.insert(prev);
                    }
                }
            }
        }

        // yhden solmun komponentit
        for (int i = 0; i < n; ++i) {
            if (arr[i].bwd_inside.size() == 0) {
                arr[i].bwd_inside.insert(i);
            }
        }

        // dfs komponenttien välillä
        vector<int> dfs_components;
        vector<int> dfs_nodes;
        function<bool(int,int)> dfs3 = [&](int i, int depth) -> bool {
            int c = arr[i].component;
            components[c].visited = true;
            components[c].depth = depth;
            components[c].first_node = i;
            components[c].last_node = -1;
            dfs_components.push_back(c);
            if (dfs_components.size() == components.size()) {
                return true;
            }
            for (auto j : arr[i].bwd_inside) {
                components[c].last_node = j;
                for (auto k : arr[j].fwd_outside) {
                    if (!components[arr[k].component].visited) {
                        if (dfs3(k, depth + 1)) {
                            dfs_nodes.push_back(k);
                            dfs_nodes.push_back(j);
                            return true;
                        }
                    }
                }
            }
            dfs_components.pop_back();
            components[c].visited = false;
            return false;
        };

        for (int i = 0; i < n; ++i) {
            if (dfs3(i, 1)) {
                dfs_nodes.push_back(i);
                break;
            }
        }

        // dfs komponenttien sisällä
        vector<int> result;
        function<bool(int,int,int)> dfs4 = [&](int i, int target_depth, int target_node) {
            arr[i].visited = true;
            result.push_back(i);
            if (result.size() == target_depth && (i == target_node || target_node == -1)) {
                return true;
            }
            for (auto j : arr[i].fwd_inside) {
                if (arr[j].component == arr[i].component && !arr[j].visited) {
                    if (dfs4(j, target_depth, target_node)) {
                        return true;
                    }
                }
            }
            result.pop_back();
            arr[i].visited = false;
            return false;
        };
        for (auto &node : arr) {
            node.visited = false;
        }
        while (dfs_nodes.size()) {
            int first_node = dfs_nodes.back();
            dfs_nodes.pop_back();
            int target_node = dfs_nodes.size() ? dfs_nodes.back() : -1;
            if (dfs_nodes.size()) {
                dfs_nodes.pop_back();
            }
            int target_depth = result.size() + components[arr[first_node].component].nodes.size();
            if (!dfs4(first_node, target_depth, target_node)) {
                cout << "FAILED to solve component\n";
            }
        }

        if (!result.size()) {
            cout << "NO\n";
        } else {
            cout << "YES\n";
            for (int i : result) {
                cout << arr[i].x << ' ' << arr[i].y << '\n';
            }
        }
    }
    return 0;
}

Test details

Test 1

Group: 1, 4, 5

Verdict: ACCEPTED

input
100
1
74 75
1
100 43
...

correct output
YES
74 75
YES
100 43
YES
...

user output
YES
74 75
YES
100 43
YES
...
Truncated

Test 2

Group: 1, 4, 5

Verdict: ACCEPTED

input
100
2
80 54
51 61
2
...

correct output
YES
51 61
80 54
YES
2 64
...

user output
YES
51 61
80 54
YES
2 64
...
Truncated

Test 3

Group: 1, 4, 5

Verdict:

input
100
3
3 74
91 45
100 24
...

correct output
YES
3 74
100 24
91 45
YES
...

user output
YES
3 74
91 45
100 24
FAILED to solve component
...
Truncated

Test 4

Group: 1, 4, 5

Verdict:

input
100
4
88 50
62 41
12 86
...

correct output
YES
12 86
88 50
62 41
66 93
...

user output
YES
12 86
88 50
62 41
66 93
...
Truncated

Test 5

Group: 1, 4, 5

Verdict:

input
100
5
82 80
80 92
5 22
...

correct output
YES
5 22
94 13
82 80
80 92
...

user output
YES
5 22
80 92
93 91
94 13
...
Truncated

Test 6

Group: 1, 2, 4, 5

Verdict: ACCEPTED

input
100
5
34 38
26 30
1 6
...

correct output
YES
1 6
12 22
26 30
34 38
...

user output
YES
1 6
12 22
26 30
34 38
...
Truncated

Test 7

Group: 1, 3, 4, 5

Verdict:

input
100
5
50 40
28 25
51 7
...

correct output
YES
51 7
50 40
47 1
17 11
...

user output
YES
17 11
28 25
47 1
50 40
...
Truncated

Test 8

Group: 1, 4, 5

Verdict:

input
100
5
2 2
2 1
1 1
...

correct output
YES
1 2
2 1
2 1
1 1
...

user output
YES
1 1
1 2
2 1
2 1
...
Truncated

Test 9

Group: 2, 4, 5

Verdict:

input
100
100
175870020 296379324
248160539 883842002
21934885 781732852
...

correct output
NO
YES
4976156 6890135
10553287 11923223
14617057 17728163
...

user output
(empty)

Test 10

Group: 3, 4, 5

Verdict:

input
100
100
447597377 314433951
700232436 691277009
937268439 708165426
...

correct output
YES
998963839 391778929
995772196 257222033
995754704 553123757
994629465 247775824
...

user output
(empty)

Test 11

Group: 4, 5

Verdict:

input
100
100
1 1
1 2
2 1
...

correct output
YES
1 2
2 1
1 2
2 2
...

user output
(empty)

Test 12

Group: 4, 5

Verdict:

input
100
100
7 1
6 3
10 9
...

correct output
YES
6 7
7 8
9 10
10 10
...

user output
(empty)

Test 13

Group: 4, 5

Verdict:

input
100
100
51 5
85 77
91 84
...

correct output
YES
100 24
100 25
100 3
100 6
...

user output
(empty)

Test 14

Group: 4, 5

Verdict:

input
100
100
823828194 863717310
593641073 340054211
420481158 965069109
...

correct output
YES
999289319 634855378
996775156 433726648
983657502 55234695
981890636 112877413
...

user output
(empty)

Test 15

Group: 2, 5

Verdict:

input
100
500
88724450 89315226
266915464 267648621
189301651 189661541
...

correct output
YES
764920 1459946
1936195 2832987
3691481 4085931
4991808 5840928
...

user output
(empty)

Test 16

Group: 3, 5

Verdict:

input
100
500
763682761 317584504
756010800 260162861
435911339 78070399
...

correct output
YES
998768285 3307355
998714926 628486754
997115613 820932481
993320616 554600893
...

user output
(empty)

Test 17

Group: 5

Verdict:

input
100
500
2 2
2 1
1 2
...

correct output
YES
1 2
2 2
2 1
1 2
...

user output
(empty)

Test 18

Group: 5

Verdict:

input
100
500
10 6
10 10
9 10
...

correct output
YES
2 3
3 4
4 5
5 6
...

user output
(empty)

Test 19

Group: 5

Verdict:

input
100
500
85 87
89 70
70 92
...

correct output
YES
96 97
100 67
100 10
100 97
...

user output
(empty)

Test 20

Group: 5

Verdict:

input
100
500
861154169 119512584
569086662 606567153
288230434 322196278
...

correct output
YES
999945324 969534372
999738857 240617694
999244114 722161553
999207839 557351400
...

user output
(empty)

Test 21

Group: 5

Verdict:

input
100
500
116439250 401518028
280329609 193466222
674040956 209050570
...

correct output
NO
YES
773701149 773852119
987509190 315670966
977413249 510418200
...

user output
(empty)

Test 22

Group: 5

Verdict:

input
100
500
934181189 942499518
684836806 395802802
957884803 570946201
...

correct output
YES
999772640 505132174
999111650 140844643
999028633 888134186
999020109 291046771
...

user output
(empty)