Submission #1235591

#TimeUsernameProblemLanguageResultExecution timeMemory
1235591gry3125Monthly railway pass (LMIO18_menesinis_bilietas)C++20
100 / 100
814 ms81872 KiB
#include <bits/stdc++.h>
#define ll long long int
#define pb push_back
#define fi first
#define se second
using namespace std;

ll par[500005], sz[500005];
set<ll> adj[500005];

ll find(ll a) {
    if (par[a] == a) return a;
    return par[a] = find(par[a]);
}
void merge(ll a, ll b) {
    a = find(a); b = find(b);
    if (a == b) return;
    if (sz[a] < sz[b]) swap(a, b);
    par[b] = a; sz[a] += sz[b]; sz[b] = 0;
}

int main() {
    ll n, m; cin >> n >> m;
    vector<pair<ll,ll>> bus;
    for (ll i = 1; i <= n; i++) {
        par[i] = i; sz[i] = 1;
    }
    for (ll i = 0; i < m; i++) {
        ll a, b; char c; 
        cin >> a >> b >> c;
        if (c == 'A') bus.pb({a, b});
        else merge(a, b);
    }
    for (ll i = 0; i < bus.size(); i++) {
        ll a = bus[i].fi, b = bus[i].se;
        a = find(a); b = find(b);
        if (a == b) continue;
        adj[a].insert(b); adj[b].insert(a);
    }
    ll cc = n; vector<ll> ld;
    for (ll i = 1; i <= n; i++) {
        if (sz[i] == 0) cc--; 
        else ld.pb(i);
    }
    ll cnt = 0; 
    for (auto x : ld) {
        if (adj[x].size() == cc-1) {
            cnt += sz[x];
        }
    }
    cout << cnt;
    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...