#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int maxN = 5e5 + 5;
vector<int> adj[maxN];
bool ok[maxN];
bool vis[maxN];
int parent[maxN];
int sz[maxN];
int find(int v) {
if (v == parent[v])
return v;
return parent[v] = find(parent[v]);
}
void unite(int a, int b) {
a = find(a), b = find(b);
if (a == b)
return;
if (sz[a] < sz[b])
swap(a, b);
parent[b] = a;
sz[a] += sz[b];
}
void dfs(int v) {
vis[v] = true;
for(int u : adj[v]) {
ok[v] |= ok[u];
if (!vis[u]) {
dfs(u);
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
for(int i = 1; i <= n; i++) {
parent[i] = i;
sz[i] = 1;
}
for(int i = 0; i < m; i++) {
char c;
int a, b;
cin >> a >> b >> c;
if (c == 'T') {
unite(a, b);
}
adj[a].push_back(b);
adj[b].push_back(a);
}
set<int> s;
for(int i = 1; i <= n; i++) {
parent[i] = find(i);
}
for(int i = 1; i <= n; i++) {
s.insert(parent[i]);
}
int comp = s.size();
for(int i = 1; i <= n; i++) {
s.clear();
s.insert(parent[i]);
for(int j : adj[i]) {
s.insert(parent[j]);
}
int sum = s.size();
if (sum == comp) {
ok[i] = true;
}
}
for(int i = 1; i <= n; i++) {
ok[parent[i]] |= ok[i];
}
for(int i = 1; i <= n; i++) {
ok[i] |= ok[parent[i]];
}
int ans = 0;
for(int i = 0; i <= n; i++) {
ans += ok[i];
}
cout << ans << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
422 ms |
25452 KB |
Output is correct |
2 |
Correct |
10 ms |
12140 KB |
Output is correct |
3 |
Correct |
9 ms |
12140 KB |
Output is correct |
4 |
Correct |
133 ms |
38380 KB |
Output is correct |
5 |
Correct |
11 ms |
12780 KB |
Output is correct |
6 |
Correct |
64 ms |
17132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
133 ms |
38380 KB |
Output is correct |
2 |
Correct |
11 ms |
12780 KB |
Output is correct |
3 |
Correct |
8 ms |
12140 KB |
Output is correct |
4 |
Correct |
9 ms |
12140 KB |
Output is correct |
5 |
Correct |
11 ms |
12268 KB |
Output is correct |
6 |
Correct |
241 ms |
18900 KB |
Output is correct |
7 |
Correct |
708 ms |
49244 KB |
Output is correct |
8 |
Correct |
20 ms |
13420 KB |
Output is correct |
9 |
Correct |
28 ms |
13548 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
12140 KB |
Output is correct |
2 |
Correct |
9 ms |
12140 KB |
Output is correct |
3 |
Correct |
8 ms |
12140 KB |
Output is correct |
4 |
Correct |
9 ms |
12140 KB |
Output is correct |
5 |
Correct |
11 ms |
12268 KB |
Output is correct |
6 |
Correct |
8 ms |
12140 KB |
Output is correct |
7 |
Correct |
8 ms |
12140 KB |
Output is correct |
8 |
Correct |
9 ms |
12140 KB |
Output is correct |
9 |
Incorrect |
11 ms |
12268 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
12140 KB |
Output is correct |
2 |
Correct |
8 ms |
12140 KB |
Output is correct |
3 |
Correct |
9 ms |
12140 KB |
Output is correct |
4 |
Incorrect |
11 ms |
12268 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
12140 KB |
Output is correct |
2 |
Correct |
8 ms |
12140 KB |
Output is correct |
3 |
Correct |
9 ms |
12140 KB |
Output is correct |
4 |
Incorrect |
11 ms |
12268 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |