답안 #920877

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
920877 2024-02-03T07:07:59 Z itslq Monthly railway pass (LMIO18_menesinis_bilietas) C++17
100 / 100
566 ms 159824 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++) {
        x = getParent(i);
        ch[x].push_back(i);
        if (ch[x].size() == N) {
            cout << N;
            return 0;
        }
    }

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

        if (x == y) continue;

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

    int C = 0;

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

    cout << C;
    return 0;
}

Compilation message

menesinis_bilietas.cpp: In function 'int main()':
menesinis_bilietas.cpp:58:26: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   58 |         if (ch[x].size() == N) {
      |             ~~~~~~~~~~~~~^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 85 ms 11728 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 27 ms 29788 KB Output is correct
5 Correct 1 ms 1116 KB Output is correct
6 Correct 24 ms 692 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 29788 KB Output is correct
2 Correct 1 ms 1116 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 3 ms 1116 KB Output is correct
6 Correct 468 ms 42808 KB Output is correct
7 Correct 566 ms 159824 KB Output is correct
8 Correct 10 ms 6236 KB Output is correct
9 Correct 17 ms 7128 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 3 ms 1116 KB Output is correct
6 Correct 1 ms 600 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 2 ms 604 KB Output is correct
11 Correct 2 ms 604 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 3 ms 604 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 600 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 1 ms 344 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 3 ms 604 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 344 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 0 ms 344 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 3 ms 1116 KB Output is correct
21 Correct 1 ms 1116 KB Output is correct
22 Correct 10 ms 6236 KB Output is correct
23 Correct 17 ms 7128 KB Output is correct
24 Correct 24 ms 692 KB Output is correct
25 Correct 5 ms 860 KB Output is correct
26 Correct 71 ms 6896 KB Output is correct
27 Correct 27 ms 2516 KB Output is correct
28 Correct 80 ms 11672 KB Output is correct
29 Correct 23 ms 1752 KB Output is correct
30 Correct 73 ms 10432 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 600 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 1 ms 344 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 3 ms 604 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 5 ms 860 KB Output is correct
17 Correct 71 ms 6896 KB Output is correct
18 Correct 27 ms 2516 KB Output is correct
19 Correct 80 ms 11672 KB Output is correct
20 Correct 23 ms 1752 KB Output is correct
21 Correct 73 ms 10432 KB Output is correct
22 Correct 85 ms 11728 KB Output is correct
23 Correct 0 ms 344 KB Output is correct
24 Correct 1 ms 344 KB Output is correct
25 Correct 27 ms 29788 KB Output is correct
26 Correct 0 ms 344 KB Output is correct
27 Correct 1 ms 344 KB Output is correct
28 Correct 3 ms 1116 KB Output is correct
29 Correct 1 ms 1116 KB Output is correct
30 Correct 468 ms 42808 KB Output is correct
31 Correct 566 ms 159824 KB Output is correct
32 Correct 10 ms 6236 KB Output is correct
33 Correct 17 ms 7128 KB Output is correct
34 Correct 24 ms 692 KB Output is correct
35 Correct 18 ms 3028 KB Output is correct
36 Correct 106 ms 11728 KB Output is correct
37 Correct 42 ms 8016 KB Output is correct