답안 #706507

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
706507 2023-03-06T20:21:47 Z tamyte Monthly railway pass (LMIO18_menesinis_bilietas) C++14
6 / 100
679 ms 50164 KB
#include <bits/stdc++.h>


using namespace std;

struct DSU {
    vector<int> e;
    DSU(int n) {
        e = vector<int>(n, -1);
    }
    int get(int x) {
        return e[x] < 0 ? x : e[x] = get(e[x]);
    }
    bool same(int x, int y) {
        return get(x) == get(y);
    }
    int size(int x) {
        return -e[get(x)];
    }
    bool unite(int x, int y) {
//        cout << x << " " << y << " -> ";
        x = get(x), y = get(y);
//        cout << x << " " << y << "\n";
        if (x == y) return false;
        if (e[x] > e[y]) swap(x, y);
        e[x] += e[y];
        e[y] = x;
//        cout << x << " " << y << "\n";
        return true;
    }

};

vector<int> adj[(int)5e5 + 1];


int main() {
    int n, m;
    cin >> n >> m;
    DSU dsu(n);
    if (!m) {
        cout << 0;
        return 0;
    }
    vector<pair<int, int>> bus;
    for (int i = 0; i < m; ++i) {
        int a, b;
        char c;
        cin >> a >> b >> c;
        --a; --b;
        if (c == 'T') {
//            cout << "a";
            dsu.unite(a, b);
        } else {
            bus.push_back({a, b});
        }
    }
//    for (auto u : dsu.e) {
//        cout << u << " ";
//    }
//    cout << "\n";
    bool free = true;
    for (auto u : bus) {
        int x = dsu.get(u.first);
        int y = dsu.get(u.second);
        if (x == y) continue;
        free = false;
        adj[u.first].push_back(u.second);
        adj[u.second].push_back(u.first);
    }
    if (free) {
        bool connected = true;
        // cerr << "a";
        for (auto& p : dsu.e) {
            if (dsu.get(p) != dsu.e.front()) {
                connected = false;
                break;
            }
        }   
        if (connected) cout << n;
        else cout << 0;
        return 0;
    }
    // cerr << "a";
    int cnt = 0;
    for (int i = 0; i < n; ++i) {
        auto& curr = adj[i];
        if (curr.size()) {
            set<int> st;
            int x = dsu.get(i);
            st.insert(x);
            int sum = -dsu.e[x];
            for (auto& u : curr) {
                int y = dsu.get(u);
                if (!st.count(y)) {
                    st.insert(y);
                    sum += -dsu.e[y];
                }
            }
            cnt += (sum == n) * (-dsu.e[x]);
        }
    }
    cout << cnt;
}
# 결과 실행 시간 메모리 Grader output
1 Runtime error 335 ms 26512 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 13780 KB Output is correct
2 Correct 6 ms 11988 KB Output is correct
3 Correct 7 ms 12072 KB Output is correct
4 Correct 11 ms 12240 KB Output is correct
5 Correct 6 ms 11988 KB Output is correct
6 Correct 329 ms 21096 KB Output is correct
7 Correct 679 ms 50164 KB Output is correct
8 Correct 20 ms 13396 KB Output is correct
9 Correct 30 ms 13572 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 11988 KB Output is correct
2 Correct 7 ms 12072 KB Output is correct
3 Correct 11 ms 12240 KB Output is correct
4 Correct 6 ms 11988 KB Output is correct
5 Correct 6 ms 11988 KB Output is correct
6 Incorrect 8 ms 12116 KB Output isn't correct
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 11988 KB Output is correct
2 Correct 6 ms 11988 KB Output is correct
3 Incorrect 8 ms 12116 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 11988 KB Output is correct
2 Correct 6 ms 11988 KB Output is correct
3 Incorrect 8 ms 12116 KB Output isn't correct
4 Halted 0 ms 0 KB -