Submission #1139333

#TimeUsernameProblemLanguageResultExecution timeMemory
1139333seby1305Monthly railway pass (LMIO18_menesinis_bilietas)C++20
100 / 100
437 ms97444 KiB
#include <bits/stdc++.h>
#define ll long long
#define pi pair<int, char>
#define pint pair<int, int>
#define ff first
#define ss second
#define pb push_back
#define all(v) v.begin(), v.end()
#define allsir(v) v+1, v+n+1
#define inf 1e9
using namespace std;

const string file = "";
ifstream fin(file+".in");
ofstream fout(file+".out");

const int dim = 500001, mod = 1e9+7;
int n, m, a, b, nod, i, drum[dim], comp[dim], sz[dim];
char c;
vector<pi> g[dim];
set<int> gc[dim];

struct muchie {
    int a, b;
    char c;
};

vector<muchie> lm;

void bfs(int nod)
{
    int i;
    for (i = 1; i <= n; i++)
        drum[i] = inf;
    drum[nod] = 0;
    queue<pint> q;
    q.push({0, nod});
    while (!q.empty())
    {
        auto top = q.front();
        q.pop();
        int dist = top.ff, a = top.ss;
        for (auto x : g[a])
        {
            int b = x.ff, cost = 0;
            char tip = x.ss;
            if (tip == 'A')
                cost = 1;
            if (drum[b] > drum[a]+cost)
                drum[b] = drum[a]+cost, q.push({drum[b], b});
        }
    }
}

void dfs(int nod, int nrc)
{
    comp[nod] = nrc;
    sz[nrc]++;
    for (auto x : g[nod])
    {
        if (comp[x.ff])
            continue;
        if (x.ss == 'T')
            dfs(x.ff, nrc);
    }
}

void solve()
{
    cin >> n >> m;
    //bool allA = 1, allT = 1;
    for (i = 1; i <= m; i++)
    {
        cin >> a >> b >> c;
        lm.pb({a, b, c});
//        if (c == 'A')
//            allT = 0;
//        else
//            allA = 0;
        g[a].pb({b, c});
        g[b].pb({a, c});
    }
    int nrc = 0;
    for (i = 1; i <= n; i++)
    {
        if (!comp[i])
        {
            ++nrc;
            dfs(i, nrc);
        }
    }
    for (i = 0; i < m; i++)
    {
        int a = lm[i].a, b = lm[i].b, c = lm[i].c;
        if (c == 'A')
        {
            if (comp[a] == comp[b])
                continue;
            gc[comp[a]].insert(comp[b]);
            gc[comp[b]].insert(comp[a]);
        }
    }
    //cout << "nrc = " << nrc << endl;
    int rez = 0;
    for (i = 1; i <= nrc; i++)
    {
        //cout << "gc["<<i<<"].size() = " << gc[i].size() << endl;
        if (gc[i].size() == nrc-1)
            rez += sz[i];
    }
    cout << rez;
//    if (allT || allA)
//    {
//
//        if (allT)
//        {
//            if (nrc == 1)
//                cout << n;
//            else
//                cout << 0;
//        }
//        else
//        {
//
//        }
//        return;
//    }
//    int rez = 0;
//    for (nod = 1; nod <= n; nod++)
//    {
//        memset(drum, 0, sizeof drum);
//        bfs(nod);
//        bool ok = 1;
//        for (i = 1; i <= n && ok; i++)
//            if (drum[i] > 1)
//                ok = 0;
//        if (ok)
//            rez++;
//    }
//    cout << rez;
}

int main()
{
    int t = 1;
    ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
    //cin >> t;
    while (t--)
        solve();
    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...