#include <bits/stdc++.h>
using namespace std;
struct DSU {
vector<int> e;
DSU(int n) {
e = vector<int>(n, -1);
}
int get(int x) {
return e[x] < 0 ? x : e[x] = get(e[x]);
}
bool same(int x, int y) {
return get(x) == get(y);
}
int size(int x) {
return -e[get(x)];
}
bool unite(int x, int y) {
// cout << x << " " << y << " -> ";
x = get(x), y = get(y);
// cout << x << " " << y << "\n";
if (x == y) return false;
if (e[x] > e[y]) swap(x, y);
e[x] += e[y];
e[y] = x;
// cout << x << " " << y << "\n";
return true;
}
};
vector<int> adj[(int)5e5 + 1];
int main() {
int n, m;
cin >> n >> m;
DSU dsu(n);
if (!m) {
cout << 0;
return 0;
}
vector<pair<int, int>> bus;
for (int i = 0; i < m; ++i) {
int a, b;
char c;
cin >> a >> b >> c;
--a; --b;
if (c == 'T') {
// cout << "a";
dsu.unite(a, b);
} else {
bus.push_back({a, b});
}
}
// for (auto u : dsu.e) {
// cout << u << " ";
// }
// cout << "\n";
bool free = true;
for (auto u : bus) {
int x = dsu.get(u.first);
int y = dsu.get(u.second);
if (x == y) continue;
free = false;
adj[x].push_back(y);
adj[y].push_back(x);
}
// cout << free << "\n";
if (free) {
bool connected = true;
// cerr << "a";
// cout << "a";
for (int i = 0; i < n; ++i) {
if (dsu.get(i) != dsu.get(0)) {
connected = false;
break;
}
}
if (connected) cout << n;
else cout << 0;
return 0;
}
// cerr << "a";
int cnt = 0;
for (int i = 0; i < n; ++i) {
auto& curr = adj[i];
if (curr.size()) {
sort(curr.begin(), curr.end());
auto it = unique(curr.begin(), curr.end());
curr.erase(it, curr.end());
int sum = (dsu.e[i] < 0 ? -dsu.e[i] : 1);
for (auto& u : curr) {
sum += -dsu.e[u];
}
cnt += (sum == n) * (dsu.e[i] < 0 ? -dsu.e[i] : 1);
}
}
cout << cnt;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
308 ms |
13140 KB |
Output is correct |
2 |
Correct |
6 ms |
11988 KB |
Output is correct |
3 |
Correct |
7 ms |
11984 KB |
Output is correct |
4 |
Correct |
7 ms |
13780 KB |
Output is correct |
5 |
Correct |
7 ms |
11988 KB |
Output is correct |
6 |
Correct |
109 ms |
14352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
13780 KB |
Output is correct |
2 |
Correct |
7 ms |
11988 KB |
Output is correct |
3 |
Correct |
7 ms |
11988 KB |
Output is correct |
4 |
Correct |
6 ms |
11988 KB |
Output is correct |
5 |
Correct |
10 ms |
12232 KB |
Output is correct |
6 |
Correct |
229 ms |
19036 KB |
Output is correct |
7 |
Correct |
385 ms |
30988 KB |
Output is correct |
8 |
Correct |
16 ms |
12756 KB |
Output is correct |
9 |
Correct |
24 ms |
12880 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
11988 KB |
Output is correct |
2 |
Correct |
7 ms |
11984 KB |
Output is correct |
3 |
Correct |
7 ms |
11988 KB |
Output is correct |
4 |
Correct |
6 ms |
11988 KB |
Output is correct |
5 |
Correct |
10 ms |
12232 KB |
Output is correct |
6 |
Correct |
6 ms |
11988 KB |
Output is correct |
7 |
Correct |
5 ms |
11988 KB |
Output is correct |
8 |
Correct |
7 ms |
12116 KB |
Output is correct |
9 |
Correct |
10 ms |
12184 KB |
Output is correct |
10 |
Correct |
11 ms |
12312 KB |
Output is correct |
11 |
Correct |
11 ms |
12184 KB |
Output is correct |
12 |
Correct |
6 ms |
11992 KB |
Output is correct |
13 |
Correct |
6 ms |
12012 KB |
Output is correct |
14 |
Correct |
12 ms |
12300 KB |
Output is correct |
15 |
Correct |
6 ms |
11984 KB |
Output is correct |
16 |
Correct |
7 ms |
12056 KB |
Output is correct |
17 |
Correct |
6 ms |
11988 KB |
Output is correct |
18 |
Correct |
8 ms |
12052 KB |
Output is correct |
19 |
Correct |
7 ms |
12052 KB |
Output is correct |
20 |
Correct |
8 ms |
12056 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
11988 KB |
Output is correct |
2 |
Correct |
5 ms |
11988 KB |
Output is correct |
3 |
Correct |
7 ms |
12116 KB |
Output is correct |
4 |
Correct |
10 ms |
12184 KB |
Output is correct |
5 |
Correct |
11 ms |
12312 KB |
Output is correct |
6 |
Correct |
11 ms |
12184 KB |
Output is correct |
7 |
Correct |
6 ms |
11992 KB |
Output is correct |
8 |
Correct |
6 ms |
12012 KB |
Output is correct |
9 |
Correct |
12 ms |
12300 KB |
Output is correct |
10 |
Correct |
6 ms |
11984 KB |
Output is correct |
11 |
Correct |
7 ms |
12056 KB |
Output is correct |
12 |
Correct |
6 ms |
11988 KB |
Output is correct |
13 |
Correct |
8 ms |
12052 KB |
Output is correct |
14 |
Correct |
7 ms |
12052 KB |
Output is correct |
15 |
Correct |
8 ms |
12056 KB |
Output is correct |
16 |
Correct |
6 ms |
11988 KB |
Output is correct |
17 |
Correct |
7 ms |
11984 KB |
Output is correct |
18 |
Correct |
7 ms |
11988 KB |
Output is correct |
19 |
Correct |
6 ms |
11988 KB |
Output is correct |
20 |
Correct |
10 ms |
12232 KB |
Output is correct |
21 |
Correct |
7 ms |
11988 KB |
Output is correct |
22 |
Correct |
16 ms |
12756 KB |
Output is correct |
23 |
Correct |
24 ms |
12880 KB |
Output is correct |
24 |
Correct |
109 ms |
14352 KB |
Output is correct |
25 |
Correct |
23 ms |
12880 KB |
Output is correct |
26 |
Correct |
236 ms |
23304 KB |
Output is correct |
27 |
Correct |
89 ms |
16592 KB |
Output is correct |
28 |
Correct |
118 ms |
18132 KB |
Output is correct |
29 |
Correct |
69 ms |
15556 KB |
Output is correct |
30 |
Correct |
123 ms |
16872 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
11988 KB |
Output is correct |
2 |
Correct |
5 ms |
11988 KB |
Output is correct |
3 |
Correct |
7 ms |
12116 KB |
Output is correct |
4 |
Correct |
10 ms |
12184 KB |
Output is correct |
5 |
Correct |
11 ms |
12312 KB |
Output is correct |
6 |
Correct |
11 ms |
12184 KB |
Output is correct |
7 |
Correct |
6 ms |
11992 KB |
Output is correct |
8 |
Correct |
6 ms |
12012 KB |
Output is correct |
9 |
Correct |
12 ms |
12300 KB |
Output is correct |
10 |
Correct |
6 ms |
11984 KB |
Output is correct |
11 |
Correct |
7 ms |
12056 KB |
Output is correct |
12 |
Correct |
6 ms |
11988 KB |
Output is correct |
13 |
Correct |
8 ms |
12052 KB |
Output is correct |
14 |
Correct |
7 ms |
12052 KB |
Output is correct |
15 |
Correct |
8 ms |
12056 KB |
Output is correct |
16 |
Correct |
23 ms |
12880 KB |
Output is correct |
17 |
Correct |
236 ms |
23304 KB |
Output is correct |
18 |
Correct |
89 ms |
16592 KB |
Output is correct |
19 |
Correct |
118 ms |
18132 KB |
Output is correct |
20 |
Correct |
69 ms |
15556 KB |
Output is correct |
21 |
Correct |
123 ms |
16872 KB |
Output is correct |
22 |
Correct |
308 ms |
13140 KB |
Output is correct |
23 |
Correct |
6 ms |
11988 KB |
Output is correct |
24 |
Correct |
7 ms |
11984 KB |
Output is correct |
25 |
Correct |
7 ms |
13780 KB |
Output is correct |
26 |
Correct |
7 ms |
11988 KB |
Output is correct |
27 |
Correct |
6 ms |
11988 KB |
Output is correct |
28 |
Correct |
10 ms |
12232 KB |
Output is correct |
29 |
Correct |
7 ms |
11988 KB |
Output is correct |
30 |
Correct |
229 ms |
19036 KB |
Output is correct |
31 |
Correct |
385 ms |
30988 KB |
Output is correct |
32 |
Correct |
16 ms |
12756 KB |
Output is correct |
33 |
Correct |
24 ms |
12880 KB |
Output is correct |
34 |
Correct |
109 ms |
14352 KB |
Output is correct |
35 |
Correct |
49 ms |
13708 KB |
Output is correct |
36 |
Correct |
292 ms |
24964 KB |
Output is correct |
37 |
Correct |
132 ms |
15776 KB |
Output is correct |