53222023-04-25 22:01:22CattVázsony vonatjegyet vásárolcpp17Runtime error 20/1001.587s175920 KiB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll maxN = 2e5 + 5;


vector<vector<pair<ll, ll> > > g;
vector<ll> dist;
vector<ll> p(maxN), s(maxN, 1);
vector<set<ll> > to(maxN);
vector<bool> volt(maxN, 0);

ll root(ll x) {
    if(p[x] == x) return x;

    return p[x] = root(p[x]);
}

void connect(ll x, ll y) {
    x = root(x);
    y = root(y);

    if(x == y) return;
    if(to[x].size() < to[y].size()) swap(x, y);

    p[y] = x;
    for(ll sz : to[y]) {
        to[x].insert(sz);
    }
    s[x] += s[y];
}

void dijkstra(ll start) {
    priority_queue<pair<ll, ll> > pq;
    pq.push({0, start});

    while (!pq.empty()) {
        auto curr = pq.top();
        pq.pop();

        ll w = -curr.first, x = curr.second;
        if(dist[x] != -1) continue;
        dist[x] = w;
        for(pair<ll, ll> sz : g[x]) {
            if(dist[sz.first] != -1) continue;
            pq.push({-w-sz.second, sz.first});
        }
    }
}

void dfs(int x) {
    volt[x] = 1;
    for(int sz : to[x]) {
        if(volt[root(sz)]) continue;
        dfs(root(sz));
    }
}

int main() {

    ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);
    
    ll n,m,a,b;
    cin >> n >> m >> a >> b;
    g.resize(n+1);
    dist.resize(n+1, -1);

    for(ll i = 0; i < m; i++) {
        ll x,y,z;
        cin >> x >> y >> z;

        g[x].push_back({y, z});
        g[y].push_back({x, z});
    }

    dijkstra(b);
    for(ll i = 1; i <= n; i++) {
        p[i] = i;
        for(auto sz : g[i]) {
            if(dist[sz.first] < dist[i]) {
                to[i].insert(sz.first);
            }
            else if(dist[sz.first] > dist[i]) {
                to[sz.first].insert(i);
            }
        }
    }
    for(ll i = 1; i <= n; i++) {
        for(auto sz : g[i]) {
            if(dist[sz.first] == dist[i]) {
                connect(sz.first, i);
            }
        }
    }
    dfs(root(a));
    vector<ll> be(n+1, 0);
    for(ll i = 1; i <= n; i++) {

        if(!volt[i]) continue;
        for(ll sz : to[i]) be[root(sz)]++;
    }

    vector<ll> tav(n+1, -1), parent(n+1, -1);
    volt.assign(n+1, 0);
    
    tav[root(a)] = 0;
    queue<ll> sor;
    sor.push(root(a));
    while(!sor.empty()) {
        ll cur = sor.front();
        sor.pop();

        volt[cur] = 1;

        for(ll sz : to[cur]) {
            sz = root(sz);
            if(volt[sz]) continue;
            be[sz]--;
            if((tav[sz] == tav[cur] + s[cur] && s[cur] > s[parent[sz]]) || tav[sz] < tav[cur] + s[cur]) {
                tav[sz] = tav[cur] + s[cur];
                parent[sz] = cur;
            }
            if(be[sz] == 0) {
                sor.push(sz);
            }
        }
    }

    set<ll> mo = {b};
    ll cur = b;
    while(cur != a) {
        cur = parent[cur];
        mo.insert(cur);
    }
    
    vector<ll> ki;
    for(ll i = 1; i <= n; i++) {
        if(mo.find(root(i)) != mo.end()) ki.push_back(i);
    }

    cout << ki.size() << "\n";
    for(ll sz : ki) cout << sz << ' ';
    
    return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted10ms26868 KiB
2Accepted212ms60204 KiB
subtask220/20
3Accepted92ms43248 KiB
4Accepted734ms119820 KiB
5Accepted474ms99640 KiB
6Accepted323ms82576 KiB
7Accepted507ms98100 KiB
8Accepted361ms81112 KiB
9Accepted977ms145688 KiB
subtask30/15
10Runtime error326ms81536 KiB
11Accepted28ms35664 KiB
12Time limit exceeded1.557s21024 KiB
13Time limit exceeded1.565s26288 KiB
14Time limit exceeded1.575s35692 KiB
subtask40/20
15Runtime error76ms57112 KiB
16Runtime error305ms93500 KiB
17Accepted57ms56084 KiB
18Accepted104ms66480 KiB
19Accepted54ms51296 KiB
20Accepted94ms61556 KiB
subtask50/45
21Time limit exceeded1.582s26436 KiB
22Time limit exceeded1.542s113336 KiB
23Accepted90ms55256 KiB
24Accepted400ms97108 KiB
25Accepted368ms95304 KiB
26Accepted615ms121156 KiB
27Accepted298ms86144 KiB
28Accepted992ms175920 KiB
29Accepted197ms71924 KiB
30Accepted1.136s167832 KiB
31Runtime error280ms96856 KiB
32Time limit exceeded1.587s37356 KiB
33Runtime error504ms119432 KiB
34Accepted486ms96260 KiB
35Accepted397ms94308 KiB
36Accepted507ms93260 KiB
37Accepted513ms92740 KiB