#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);
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[u.first].push_back(u.second);
adj[u.second].push_back(u.first);
}
if (free) {
bool connected = true;
for (auto& p : dsu.e) {
if (dsu.get(p) != dsu.e[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()) {
set<int> st;
int x = dsu.get(i);
st.insert(x);
int sum = -dsu.e[x];
for (auto& u : curr) {
int y = dsu.get(u);
if (!st.count(y)) {
st.insert(y);
sum += -dsu.e[y];
}
}
cnt += (sum == n) * (-dsu.e[x]);
}
}
cout << cnt;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
320 ms |
33356 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
13780 KB |
Output is correct |
2 |
Correct |
6 ms |
11988 KB |
Output is correct |
3 |
Correct |
7 ms |
12044 KB |
Output is correct |
4 |
Correct |
11 ms |
12244 KB |
Output is correct |
5 |
Correct |
7 ms |
12048 KB |
Output is correct |
6 |
Correct |
362 ms |
25372 KB |
Output is correct |
7 |
Correct |
707 ms |
56248 KB |
Output is correct |
8 |
Correct |
22 ms |
13532 KB |
Output is correct |
9 |
Correct |
31 ms |
13956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
11988 KB |
Output is correct |
2 |
Correct |
7 ms |
12044 KB |
Output is correct |
3 |
Correct |
11 ms |
12244 KB |
Output is correct |
4 |
Correct |
7 ms |
11920 KB |
Output is correct |
5 |
Correct |
7 ms |
11984 KB |
Output is correct |
6 |
Incorrect |
11 ms |
12136 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
11920 KB |
Output is correct |
2 |
Correct |
7 ms |
11984 KB |
Output is correct |
3 |
Incorrect |
11 ms |
12136 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
11920 KB |
Output is correct |
2 |
Correct |
7 ms |
11984 KB |
Output is correct |
3 |
Incorrect |
11 ms |
12136 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |