#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
typedef long long ll;
const ll INF = 1e9;
using namespace std;
int link[500005], sz[500005];
set<int>adj[500005];
int find(int x) {
    while (x != link[x]) x = link[x];
    return x;
}
void unite(int a, int b) {
    a = find(a);
    b = find(b);
    if (a == b) return;
    if (a != b) {
        if (sz[a] < sz[b]) swap(a, b);
        sz[a] += sz[b];
        link[b] = a;
    }
}
void solve()
{
    int n, m; cin >> n >> m;
    for (int i = 1; i <= n; i++) { link[i] = i; sz[i] = 1; }
    vector<pair<int, int>>a;
    for (int i = 1; i <= m; i++) {
        int u, v; cin >> u >> v; char c; cin >> c;
        if (find(u) != find(v) && c == 'T') {
            unite(u, v);
        }
        else {
            a.push_back({ u, v });
        }
    }
    for (auto x : a) {
        int u = find(x.first); int v = find(x.second);
        if (u != v) {
            adj[u].insert(v);
            adj[v].insert(u);
        }
    }
    int cc = 0;
    for (int i = 1; i <= n; i++) {
        if (find(i) == i)++cc;
    }
    int ans = 0;
    for (int i = 1; i <= n; i++) {
        if (find(i) == i && adj[i].size() == cc - 1)ans += sz[i];
    }
    cout << ans << "\n";
}
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int t = 1;// cin >> t;
    while (t--) solve();
    return 0;
}
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |