//Dedicated to my love, ivaziva
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define int long long
using pii = pair<int, int>;
using ll = int64_t;
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define pb push_back
#define dbg(x) cerr<<#x<<": "<<x<<'\n';
#define dbga(A,l_,r_){for(int i_=l_;i_<=r_;i_++)cerr<<A[i_]<<' ';cerr<<'\n';}
#define dbgv(a_){for(auto x_:a_) cerr<<x_<<' ';cerr<<'\n';}
const int maxn = 5e5 + 20;
int n, m, par[maxn], sz[maxn], comp[maxn], fa[maxn];
vector<int> g[2 * maxn];
bool was[maxn];
int get(int x) {
return x == par[x] ? x : par[x] = get(par[x]);
}
void join(int a, int b) {
a = get(a), b = get(b);
if (a == b) {
return;
}
if (sz[a] < sz[b]) {
swap(a, b);
}
par[b] = a;
sz[a] += sz[b];
}
int32_t main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
cout.tie(nullptr); cerr.tie(nullptr);
cin >> n >> m;
vector<pii> a, t;
for (int i = 0; i < m; i++) {
int u, v;
char c;
cin >> u >> v >> c;
if (c == 'A') {
a.pb({u, v});
} else {
t.pb({u, v});
}
}
for (int i = 1; i <= n; i++) {
par[i] = i, sz[i] = 1;
was[i] = false, comp[i] = 0;
}
for (auto x : t) {
int u = x.first, v = x.second;
join(u, v);
}
int c = n + 1, br = 0;
for (int i = 1; i <= n; i++) {
int x = get(i);
if (!was[x]) {
comp[i] = c;
fa[c] = x;
comp[x] = c++;
br++;
was[x] = true;
} else {
comp[i] = comp[x];
}
}
map<pii, bool> alr;
for (auto x : a) {
int u = x.first, v = x.second;
u = comp[u], v = comp[v];
if (u == v || alr[{u, v}]) {
continue;
}
g[u].pb(v);
g[v].pb(u);
alr[{u, v}] = true, alr[{v, u}] = true;
}
int ans = 0;
dbg(br);
for (int i = n + 1; i <= n + br; i++) {
if ((int)(g[i].size()) == br - 1) {
ans += sz[get(fa[i])];
}
}
cout << ans << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
87 ms |
45256 KB |
Output is correct |
2 |
Correct |
12 ms |
23896 KB |
Output is correct |
3 |
Correct |
15 ms |
23920 KB |
Output is correct |
4 |
Correct |
21 ms |
35524 KB |
Output is correct |
5 |
Correct |
14 ms |
24156 KB |
Output is correct |
6 |
Correct |
36 ms |
29640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
35524 KB |
Output is correct |
2 |
Correct |
14 ms |
24156 KB |
Output is correct |
3 |
Correct |
12 ms |
23900 KB |
Output is correct |
4 |
Correct |
12 ms |
24156 KB |
Output is correct |
5 |
Correct |
18 ms |
25180 KB |
Output is correct |
6 |
Correct |
558 ms |
84644 KB |
Output is correct |
7 |
Correct |
844 ms |
113684 KB |
Output is correct |
8 |
Correct |
22 ms |
27092 KB |
Output is correct |
9 |
Correct |
36 ms |
29404 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23896 KB |
Output is correct |
2 |
Correct |
15 ms |
23920 KB |
Output is correct |
3 |
Correct |
12 ms |
23900 KB |
Output is correct |
4 |
Correct |
12 ms |
24156 KB |
Output is correct |
5 |
Correct |
18 ms |
25180 KB |
Output is correct |
6 |
Correct |
12 ms |
23900 KB |
Output is correct |
7 |
Correct |
13 ms |
23900 KB |
Output is correct |
8 |
Correct |
17 ms |
23968 KB |
Output is correct |
9 |
Correct |
13 ms |
24156 KB |
Output is correct |
10 |
Correct |
14 ms |
24408 KB |
Output is correct |
11 |
Correct |
15 ms |
24408 KB |
Output is correct |
12 |
Correct |
16 ms |
23900 KB |
Output is correct |
13 |
Correct |
14 ms |
23896 KB |
Output is correct |
14 |
Correct |
16 ms |
24644 KB |
Output is correct |
15 |
Correct |
12 ms |
23900 KB |
Output is correct |
16 |
Correct |
13 ms |
23896 KB |
Output is correct |
17 |
Correct |
12 ms |
23896 KB |
Output is correct |
18 |
Correct |
13 ms |
24156 KB |
Output is correct |
19 |
Correct |
13 ms |
23932 KB |
Output is correct |
20 |
Correct |
13 ms |
24156 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23900 KB |
Output is correct |
2 |
Correct |
13 ms |
23900 KB |
Output is correct |
3 |
Correct |
17 ms |
23968 KB |
Output is correct |
4 |
Correct |
13 ms |
24156 KB |
Output is correct |
5 |
Correct |
14 ms |
24408 KB |
Output is correct |
6 |
Correct |
15 ms |
24408 KB |
Output is correct |
7 |
Correct |
16 ms |
23900 KB |
Output is correct |
8 |
Correct |
14 ms |
23896 KB |
Output is correct |
9 |
Correct |
16 ms |
24644 KB |
Output is correct |
10 |
Correct |
12 ms |
23900 KB |
Output is correct |
11 |
Correct |
13 ms |
23896 KB |
Output is correct |
12 |
Correct |
12 ms |
23896 KB |
Output is correct |
13 |
Correct |
13 ms |
24156 KB |
Output is correct |
14 |
Correct |
13 ms |
23932 KB |
Output is correct |
15 |
Correct |
13 ms |
24156 KB |
Output is correct |
16 |
Correct |
12 ms |
23896 KB |
Output is correct |
17 |
Correct |
15 ms |
23920 KB |
Output is correct |
18 |
Correct |
12 ms |
23900 KB |
Output is correct |
19 |
Correct |
12 ms |
24156 KB |
Output is correct |
20 |
Correct |
18 ms |
25180 KB |
Output is correct |
21 |
Correct |
14 ms |
24156 KB |
Output is correct |
22 |
Correct |
22 ms |
27092 KB |
Output is correct |
23 |
Correct |
36 ms |
29404 KB |
Output is correct |
24 |
Correct |
36 ms |
29640 KB |
Output is correct |
25 |
Correct |
18 ms |
24812 KB |
Output is correct |
26 |
Correct |
102 ms |
36540 KB |
Output is correct |
27 |
Correct |
40 ms |
29384 KB |
Output is correct |
28 |
Correct |
164 ms |
46272 KB |
Output is correct |
29 |
Correct |
42 ms |
27856 KB |
Output is correct |
30 |
Correct |
132 ms |
43840 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23900 KB |
Output is correct |
2 |
Correct |
13 ms |
23900 KB |
Output is correct |
3 |
Correct |
17 ms |
23968 KB |
Output is correct |
4 |
Correct |
13 ms |
24156 KB |
Output is correct |
5 |
Correct |
14 ms |
24408 KB |
Output is correct |
6 |
Correct |
15 ms |
24408 KB |
Output is correct |
7 |
Correct |
16 ms |
23900 KB |
Output is correct |
8 |
Correct |
14 ms |
23896 KB |
Output is correct |
9 |
Correct |
16 ms |
24644 KB |
Output is correct |
10 |
Correct |
12 ms |
23900 KB |
Output is correct |
11 |
Correct |
13 ms |
23896 KB |
Output is correct |
12 |
Correct |
12 ms |
23896 KB |
Output is correct |
13 |
Correct |
13 ms |
24156 KB |
Output is correct |
14 |
Correct |
13 ms |
23932 KB |
Output is correct |
15 |
Correct |
13 ms |
24156 KB |
Output is correct |
16 |
Correct |
18 ms |
24812 KB |
Output is correct |
17 |
Correct |
102 ms |
36540 KB |
Output is correct |
18 |
Correct |
40 ms |
29384 KB |
Output is correct |
19 |
Correct |
164 ms |
46272 KB |
Output is correct |
20 |
Correct |
42 ms |
27856 KB |
Output is correct |
21 |
Correct |
132 ms |
43840 KB |
Output is correct |
22 |
Correct |
87 ms |
45256 KB |
Output is correct |
23 |
Correct |
12 ms |
23896 KB |
Output is correct |
24 |
Correct |
15 ms |
23920 KB |
Output is correct |
25 |
Correct |
21 ms |
35524 KB |
Output is correct |
26 |
Correct |
12 ms |
23900 KB |
Output is correct |
27 |
Correct |
12 ms |
24156 KB |
Output is correct |
28 |
Correct |
18 ms |
25180 KB |
Output is correct |
29 |
Correct |
14 ms |
24156 KB |
Output is correct |
30 |
Correct |
558 ms |
84644 KB |
Output is correct |
31 |
Correct |
844 ms |
113684 KB |
Output is correct |
32 |
Correct |
22 ms |
27092 KB |
Output is correct |
33 |
Correct |
36 ms |
29404 KB |
Output is correct |
34 |
Correct |
36 ms |
29640 KB |
Output is correct |
35 |
Correct |
36 ms |
28872 KB |
Output is correct |
36 |
Correct |
172 ms |
46128 KB |
Output is correct |
37 |
Correct |
47 ms |
35272 KB |
Output is correct |