85362024-01-21 16:31:45qertendKerékpártúra (50 pont)cpp17Time limit exceeded 24/50467ms16652 KiB
#include <iostream>
#include <list>
using namespace std;

bool hasInt(int target, list<int> field) {
    for (int i : field) {
        if (i == target) return true;
    }
    return false;
}

int main()
{
    int numberOfNodes, routes, startID;
    cin >> numberOfNodes;
    cin >> routes;
    cin >> startID;

    struct Node {
        list<int> outgoing;
        list<int> incoming;
        void connected(list<int> &connectedByIncoming, list<int> &connectedByOutgoing,Node nodesList[]) {
            for (int i : incoming) {
                if (!hasInt(i, connectedByIncoming)) {
                    connectedByIncoming.push_back(i);
                    nodesList[i].connected(connectedByIncoming, connectedByOutgoing, nodesList);
                }
            }
            for (int i : outgoing) {
                if (!hasInt(i, connectedByOutgoing)) connectedByOutgoing.push_back(i);
            }
        }
    };
    Node nodes[numberOfNodes];

    //read all routes from stdin
    for (int i = 0; i < routes; i++) {
        int startPoint, endPoint;
        cin >> startPoint;
        cin >> endPoint;
        Node &startNode = nodes[startPoint-1];
        Node &endNode = nodes[endPoint-1];
        startNode.outgoing.push_back(endPoint-1);
        endNode.incoming.push_back(startPoint-1);
    }

    list<int> connectedByIncoming;
    list<int> connectedByOutgoing;
    nodes[startID-1].connected(connectedByIncoming, connectedByOutgoing, nodes);
    list<int> allConnected;
    allConnected.merge(connectedByIncoming);
    for (int i : connectedByOutgoing) {
        if (!hasInt(i, allConnected)) allConnected.push_back(i);
    }
    int size = allConnected.size();
    if (hasInt(startID-1, allConnected)) size--;
    cout << size << "\n";
    for (int i : allConnected) {
        if (i != startID - 1) cout << i+1 << " ";
    }

    return 0;
}
SubtaskSumTestVerdictTimeMemory
base24/50
1Accepted0/03ms1684 KiB
2Accepted0/061ms6156 KiB
3Accepted2/23ms2408 KiB
4Accepted2/23ms2360 KiB
5Accepted2/23ms2744 KiB
6Accepted2/23ms2720 KiB
7Accepted2/23ms2924 KiB
8Accepted2/26ms3588 KiB
9Accepted2/24ms3976 KiB
10Accepted2/26ms4268 KiB
11Accepted2/28ms4528 KiB
12Accepted2/217ms6308 KiB
13Accepted2/268ms6160 KiB
14Accepted2/2137ms8828 KiB
15Time limit exceeded0/3463ms6848 KiB
16Time limit exceeded0/4446ms7296 KiB
17Time limit exceeded0/4458ms8912 KiB
18Time limit exceeded0/3467ms8524 KiB
19Time limit exceeded0/3467ms8040 KiB
20Time limit exceeded0/3467ms15024 KiB
21Time limit exceeded0/3456ms16652 KiB
22Time limit exceeded0/3451ms16636 KiB