#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
using namespace std;
// using namespace __gnu_pbds;
#define SPEED \
ios_base::sync_with_stdio(0); \
cin.tie(NULL); \
cout.tie(NULL);
#define pb push_back
#define ins insert
#define fi first
#define se second
#define endl "\n"
#define ALL(x) x.begin(), x.end()
#define sz(x) x.size()
#define intt long long
const intt mod = 1e9 + 7;
const intt base = 31;
const intt inf = 1e9;
const intt mxN = 500000 + 5;
const intt L = 21;
vector<vector<intt>> graph;
set<intt> st[mxN];
vector<intt> roots(mxN), color(mxN), xalq(mxN);
vector<intt> comp;
void dfs(intt node) {
color[node] = 1;
comp.pb(node);
for(auto u : graph[node]) {
if(!color[u]) {
dfs(u);
}
}
}
void solve() {
intt n, m;
cin >> n >> m;
graph.assign(n + 1, vector<intt>{});
vector<pair<intt,intt>> buses;
for(intt i = 0; i < m; i++) {
char c;
intt a, b;
cin >> a >> b >> c;
if(c == 'T') {
graph[a].pb(b);
graph[b].pb(a);
} else {
buses.pb({a, b});
}
}
vector<intt> r;
intt cnt = 0;
for(intt i = 1; i <= n; i++) {
if(!color[i]) {
dfs(i);
intt represent = *min_element(ALL(comp));
for(auto u : comp) roots[u] = represent;
r.pb(represent);
xalq[represent] = comp.size();
cnt++;
comp.clear();
}
}
// cout << cnt << endl;
for(intt i = 0; i < buses.size(); i++) {
intt a = roots[buses[i].fi], b = roots[buses[i].se];
if(a != b) {
st[a].insert(b);
st[b].insert(a);
}
}
intt ans = 0;
for(auto root : r) {
if(st[root].size() >= cnt-1) {
ans += xalq[root];
}
}
cout << ans << endl;
}
signed main() {
SPEED;
intt tst = 1;
// cin >> tst;
while (tst--) {
solve();
}
}
# | 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... |