219662026-01-14 11:39:10FriTáblás játékcpp17Runtime error 0/5097ms65536 KiB
#include <iostream>
#include <fstream>
#include <queue>

using namespace std;

int main()
{
    int n, m, x, y, v;
    queue <int> q, qq; // for finding the longest paths
    cin >> n >> m;
    int t[n][n], tt[n][n]; // t[][] changes on second route to remove all items from the first route, but tt[][] doesn't to prevent any loops
    int s[n]; // 1 if the cell doesn'z have any arrows pointing to it
    bool e[n]; // 1 if the cell doesn't have any arrows pointing from it
    for (int i = 0; i < n * n; i++) {
        t[i / n][i % n] = 0;
        tt[i / n][i % n] = 0;
    }
    for (int i = 0; i < m; i++) {
        cin >> x >> y;
        t[x - 1][y - 1] = 1;
        tt[x - 1][y - 1] = t[x - 1][y - 1]; // to back the shit up; probably  not optimal, but oh well
    }

    // fill up s[] and e[]
    for (int i = 0; i < n; i++) {
        x = 0;
        y = 0;
        for (int ii = 0; ii < n; ii++) {
            x += t[ii][i];
            y += t[i][ii];
        }
        if (x == 0) s[i] = true;
        else s[i] = false;
        if (y == 0) e[i] = true;
        else e[i] = false;
    }

    /*

    // TEMP: output all of t[][], s[] and e[]
    for (int i = 0; i < n; i++) {
        for (int ii = 0; ii < n; ii++) {
            cout << t[i][ii] << " ";
        }
        cout << "\n";
    }
    cout << "\n";
    for (int i = 0; i < n; i++) {
        cout << s[i] << " ";
    }
    cout << "\n\n";
    for (int i = 0; i < n; i++) {
        cout << e[i] << " ";
    }

    */

    // first walk
    for (int i = 0; i < n; i++) {
        if (s[i] > 0) {
            q.push(i);
            v = 1;
            while (e[q.back()] != 1) {
                x = q.back();
                for (int ii = 0; ii < n; ii++) {
                    if (t[x][ii] > 0) {
                        q.push(ii);
                        v++;
                        for (int iii = 0; iii < n; iii++) {
                            t[iii][ii] = 0; // don't worry, I still have a backup in tt[][]
                        }
                        break;
                    }
                }
            }
            break;
        }
    }

    // second walk but with avoiding the previous path as much as ever physically possible this time around
    for (int i = n - 1; i >= 0; i--) {
        if (s[i] > 0) {
            qq.push(i);
            v++;
            while (e[qq.back()] != 1) {
                x = qq.back();
                y = 0;
                for (int ii = 0; ii < n; ii++) {
                    if (t[x][ii] > 0) {
                        qq.push(ii);
                        v++;
                        y = 1;
                        break;
                    }
                }
                if (y == 0) { // in case there is no valid path
                    for (int ii = 0; ii < n; ii++) { 
                        if (tt[x][ii] > 0) {
                            qq.push(ii);
                            y = 1;
                            break;
                        }
                    }
                }
            }
            break;
        }
    }

    // output and clear the queues, all in the dumbest way imaginable
    cout << v << "\n";
    while (!q.empty()) {
        cout << q.front() + 1 << " ";
        q.pop();
    }
    cout << 0 << "\n";
    while (!qq.empty()) {
        cout << qq.front() + 1 << " ";
        qq.pop();
    }
    cout << "0";

    return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted1ms500 KiB
2Runtime error94ms65536 KiB
subtask20/5
3Accepted1ms316 KiB
4Accepted1ms316 KiB
5Wrong answer1ms316 KiB
6Wrong answer1ms316 KiB
7Wrong answer1ms316 KiB
subtask30/5
8Wrong answer1ms328 KiB
9Wrong answer1ms316 KiB
10Accepted1ms500 KiB
11Wrong answer1ms316 KiB
12Wrong answer1ms316 KiB
subtask40/5
13Accepted1ms564 KiB
14Accepted1ms568 KiB
15Accepted39ms18048 KiB
16Wrong answer78ms31856 KiB
17Runtime error83ms65536 KiB
subtask50/10
18Accepted1ms316 KiB
19Accepted2ms316 KiB
20Accepted2ms316 KiB
21Accepted2ms316 KiB
22Accepted1ms316 KiB
23Wrong answer1ms520 KiB
24Wrong answer2ms820 KiB
25Accepted4ms2364 KiB
26Wrong answer14ms8596 KiB
27Runtime error83ms65536 KiB
subtask60/10
28Wrong answer2ms556 KiB
29Wrong answer1ms316 KiB
30Wrong answer1ms316 KiB
31Wrong answer1ms316 KiB
32Wrong answer1ms316 KiB
33Wrong answer1ms316 KiB
34Wrong answer2ms820 KiB
35Wrong answer4ms2296 KiB
36Wrong answer16ms8608 KiB
37Runtime error83ms65536 KiB
subtask70/15
38Wrong answer28ms13208 KiB
39Runtime error96ms65536 KiB
40Runtime error85ms65536 KiB
41Runtime error83ms65536 KiB
42Runtime error97ms65536 KiB
43Runtime error96ms65536 KiB
44Runtime error86ms65536 KiB
45Runtime error93ms65536 KiB
46Runtime error82ms65536 KiB
47Runtime error94ms65536 KiB
48Runtime error93ms65536 KiB
49Wrong answer12ms6196 KiB
50Runtime error94ms65536 KiB
51Runtime error83ms65536 KiB
52Runtime error85ms65536 KiB