Submission #373787

#TimeUsernameProblemLanguageResultExecution timeMemory
373787gustasonMonthly railway pass (LMIO18_menesinis_bilietas)C++11
100 / 100
773 ms125004 KiB
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int maxN = 5e5 + 5;
vector<int> adj[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];
}
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++) {
        parent[i] = find(i);
    }
    for(int i = 1; i <= n; i++) {
        s.insert(parent[i]);
    }


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


    int ans = 0;
    for(int i = 0; i <= n; i++) {
        ans += ((int) ls[parent[i]].size() >= comp);
    }
    cout << ans << "\n";
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...