57222023-09-09 22:20:07kukkermanElágazás nélküli úton levő települések (50 pont)cpp14Runtime error 31/5029ms64648 KiB
#include <iostream>
#include <vector>
#include <deque>
#include <set>
#include <algorithm>

std::vector<std::vector<size_t>> beolvas(std::istream &in) {
    size_t t, u;
    in >> t >> u;

    std::vector<std::vector<size_t>> sz(t);
    for (auto i = 0u; i < u; i++) {
        size_t honnan, hova;
        in >> honnan >> hova;
        honnan--;
        hova--;

        sz[honnan].push_back(hova);
        sz[hova].push_back(honnan);
    }

    return sz;
}

void feldolgoz(const std::vector<std::vector<size_t>> &sz) {
    const auto n = sz.size();

    std::deque<size_t> q;
    for (auto i = 0u; i < n; i++) {
        if (sz[i].size() == 1) {
            q.push_back(i);
        }
    }

    std::vector<size_t> varosok;
    std::vector<bool> lattam(n, false);
    while (!q.empty()) {
        const auto v = q.front();
        q.pop_front();

        if (!lattam[v]) {
            lattam[v] = true;
            
            if (sz[v].size() != 1) {
                varosok.push_back(v);
            }

            if (sz[v].size() <= 2) {
                for (const auto u : sz[v]) {
                    q.push_back(u);
                }
            }
        }
    }

    std::sort(varosok.begin(), varosok.end());

    std::cout << varosok.size() << std::endl;
    for (const auto v : varosok) {
        std::cout << v + 1 << ' ';
    }
    std::cout << std::endl;
}

void dfs(size_t elozo, size_t akt, const std::vector<std::vector<size_t>> &sz, std::set<size_t> &varosok) {
    if (sz[akt].size() > 1) {
        varosok.insert(akt);
    }

    if (sz[akt].size() <= 2) {
        dfs(akt, sz[akt][0] == elozo ? sz[akt][1] : sz[akt][0], sz, varosok);
    } 
}

void feldolgoz_dfs(const std::vector<std::vector<size_t>> &sz) {
    const auto n = sz.size();

    std::set<size_t> varosok;
    for (auto i = 0u; i < n; i++) {
        if (sz[i].size() == 1) {
            dfs(n, i, sz, varosok);
        }
    }

    std::cout << varosok.size() << std::endl;
    if (!varosok.empty()) {
        for (const auto v : varosok) {
            std::cout << v + 1 << ' ';
        }
        std::cout << std::endl;
    }
}

int main() {
    const auto sz = beolvas(std::cin);
    feldolgoz_dfs(sz);

    return 0;
}
SubtaskSumTestVerdictTimeMemory
base31/50
1Accepted0/03ms1812 KiB
2Accepted0/029ms4560 KiB
3Runtime error0/226ms64648 KiB
4Accepted2/22ms2444 KiB
5Accepted2/22ms2540 KiB
6Wrong answer0/23ms2668 KiB
7Accepted2/22ms2788 KiB
8Accepted2/24ms3032 KiB
9Accepted2/26ms3268 KiB
10Accepted2/28ms3628 KiB
11Accepted2/214ms4316 KiB
12Accepted2/216ms4304 KiB
13Accepted3/34ms3300 KiB
14Wrong answer0/34ms3548 KiB
15Wrong answer0/36ms3876 KiB
16Runtime error0/36ms4108 KiB
17Wrong answer0/314ms5032 KiB
18Wrong answer0/314ms5252 KiB
19Accepted3/317ms5784 KiB
20Accepted3/328ms6548 KiB
21Accepted3/328ms6556 KiB
22Accepted3/328ms6552 KiB