Submission #373771

# Submission time Handle Problem Language Result Execution time Memory
373771 2021-03-05T17:45:13 Z gustason Monthly railway pass (LMIO18_menesinis_bilietas) C++11
0 / 100
3 ms 2284 KB
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
//const int maxN = 5e5 + 5;
const int maxN = 2e4 + 5;
vector<int> adj[maxN];
bool ok[maxN];
bool vis[maxN];

int parent[maxN];
int sz[maxN];
int find(int v) {
    if (v == parent[v])
        return v;
    return parent[v] = find(parent[v]);
}
void unite(int a, int b) {
    a = find(a), b = find(b);

    if (a == b)
        return;
    if (sz[a] < sz[b])
        swap(a, b);

    parent[b] = a;
    sz[a] += sz[b];
}
void dfs(int v) {
    vis[v] = true;
    for(int u : adj[v]) {
        ok[v] |= ok[u];
        if (!vis[u]) {
            dfs(u);
        }
    }
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, m;
    cin >> n >> m;
    for(int i = 1; i <= n; i++) {
        parent[i] = i;
        sz[i] = 1;
    }

    for(int i = 0; i < m; i++) {
        char c;
        int a, b;
        cin >> a >> b >> c;
        if (c == 'T') {
            unite(a, b);
        } else {
            adj[a].push_back(b);
            adj[b].push_back(a);
        }
    }

    set<int> s;
    for(int i = 1; i <= n; i++) {
        s.insert(parent[i]);
    }


    int comp = s.size();
    for(int i = 1; i <= n; i++) {
        s.clear();
        s.insert(parent[i]);
        for(int j : adj[i]) {
            s.insert(parent[j]);
        }

        int sum = s.size();
        if (sum == comp) {
            ok[i] = true;
        }
    }

    for(int i = 1; i <= n; i++) {
        ok[parent[i]] |= ok[i];
    }
    for(int i = 1; i <= n; i++) {
        ok[i] |= ok[parent[i]];
    }

    int ans = 0;
    for(int i = 0; i <= n; i++) {
        ans += ok[i];
    }
    cout << ans << "\n";
    return 0;
}
# Verdict Execution time Memory Grader output
1 Runtime error 3 ms 2284 KB Execution killed with signal 11
# Verdict Execution time Memory Grader output
1 Runtime error 3 ms 2284 KB Execution killed with signal 11
# Verdict Execution time Memory Grader output
1 Correct 1 ms 748 KB Output is correct
2 Correct 1 ms 748 KB Output is correct
3 Incorrect 2 ms 876 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 748 KB Output is correct
2 Correct 1 ms 748 KB Output is correct
3 Incorrect 2 ms 876 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 748 KB Output is correct
2 Correct 1 ms 748 KB Output is correct
3 Incorrect 2 ms 876 KB Output isn't correct
4 Halted 0 ms 0 KB -