Submission #920871

# Submission time Handle Problem Language Result Execution time Memory
920871 2024-02-03T06:50:03 Z itslq Monthly railway pass (LMIO18_menesinis_bilietas) C++17
6 / 100
639 ms 159820 KB
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
using namespace std;

vector<int> pa, rk;

int getParent(int n) {
    if (pa[n] == n) return n;
    return pa[n] = getParent(pa[n]);
}

void mge(int x, int y) {
    if (x == y) return;

    if (rk[x] > rk[y]) {
        pa[y] = x;
    } else if (rk[x] == rk[y]) {
        pa[y] = x;
        rk[x]++;
    } else {
        pa[x] = y;
    }
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int N, M, A, B, x, y;
    char T;
    cin >> N >> M;

    pa.resize(N);
    rk.resize(N);
    for (int i = 0; i < N; i++) pa[i] = i;
    fill(rk.begin(), rk.end(), 0);

    unordered_map<int, unordered_set<int>> busadj;
    vector<pair<int, int>> bus;
    vector<int> ch[N];

    while (M--) {
        cin >> A >> B >> T;
        A--; B--;

        if (T == 'T') {
            mge(getParent(A), getParent(B));
        } else {
            bus.push_back(make_pair(A, B));
        }
    }

    for (int i = 0; i < N; i++) {
        ch[getParent(i)].push_back(i);
    }

    for (pair<int, int> route: bus) {
        x = getParent(route.first);
        y = getParent(route.second);

        //cout << x << " bus " << y << endl;

        if (x == y) continue;

        busadj[x].insert(y);
        busadj[y].insert(x);
    }

    int C = 0;
/*
    for (int i = 0; i < N; i++) {
        cout << i << ": ";
        for (int c: ch[i]) cout << c << " ";
        cout << endl;
    }*/

    for (pair<int, unordered_set<int>> station: busadj) {
        //cout << station.first << " " << station.second << endl;
        if (station.second.size() + 1 == busadj.size()) {
            C += ch[station.first].size();
        }
    }

    cout << C;
    return 0;



    /*
    while (M--) {
        cin >> A >> B >> T;
        if (T == 'T') {
            adjList[A].push_back(make_pair(B, 0));
            adjList[B].push_back(make_pair(A, 0));
        } else {
            adjList[A].push_back(make_pair(B, 1));
            adjList[B].push_back(make_pair(A, 1));
        }
    }*/
}
# Verdict Execution time Memory Grader output
1 Incorrect 84 ms 11728 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 26 ms 29784 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 3 ms 1116 KB Output is correct
5 Correct 1 ms 1116 KB Output is correct
6 Correct 443 ms 42764 KB Output is correct
7 Correct 639 ms 159820 KB Output is correct
8 Correct 13 ms 6236 KB Output is correct
9 Correct 20 ms 7332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 3 ms 1116 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 2 ms 604 KB Output is correct
9 Correct 2 ms 604 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 544 KB Output is correct
12 Correct 3 ms 604 KB Output is correct
13 Incorrect 0 ms 348 KB Output isn't correct
14 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 2 ms 604 KB Output is correct
6 Correct 2 ms 604 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 544 KB Output is correct
9 Correct 3 ms 604 KB Output is correct
10 Incorrect 0 ms 348 KB Output isn't correct
11 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 2 ms 604 KB Output is correct
6 Correct 2 ms 604 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 544 KB Output is correct
9 Correct 3 ms 604 KB Output is correct
10 Incorrect 0 ms 348 KB Output isn't correct
11 Halted 0 ms 0 KB -