#include <bits/stdc++.h>
#define f first
#define s second
using namespace std;
#define int long long
const int maxn = 5e5 + 69;
vector<int> p(maxn), sz(maxn);
vector<int> deg(maxn);
int find_set(int v) {
if(p[v] == v)
return v;
return p[v] = find_set(p[v]);
}
void unite(int a, int b) {
a = find_set(a);
b = find_set(b);
if(a != b) {
if(sz[a] < sz[b])
swap(a, b);
p[b] = a;
sz[a] += sz[b];
}
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int tt = 1;
//cin >> tt;
while(tt--) {
int n, m;
cin >> n >> m;
vector<pair<int, int>> edges;
for(int i = 1;i <= n;i++) {
p[i] = i;
sz[i] = 1;
}
for(int i = 0;i < m;i++) {
int a, b;
char c;
cin >> a >> b >> c;
if(c == 'T')
unite(a, b);
else
edges.push_back({a, b});
}
set<int> kurac;
for(int i = 1;i <= n;i++)
kurac.insert(find_set(i));
int c = kurac.size();
int ans = 0;
set<pair<int, int>> bio;
for(auto x : edges) {
int a = find_set(x.f);
int b = find_set(x.s);
if(a > b)
swap(a, b);
if(bio.count({a, b}) || a == b)
continue;
else {
bio.insert({a, b});
deg[a]++;
deg[b]++;
}
}
for(auto x : kurac)
ans += (deg[x] >= c - 1 ? sz[x] : 0);
if(c == 1)
ans = n;
cout << ans;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
81 ms |
12064 KB |
Output is correct |
2 |
Correct |
4 ms |
11988 KB |
Output is correct |
3 |
Correct |
4 ms |
11988 KB |
Output is correct |
4 |
Correct |
92 ms |
34108 KB |
Output is correct |
5 |
Correct |
6 ms |
12628 KB |
Output is correct |
6 |
Correct |
27 ms |
11988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
92 ms |
34108 KB |
Output is correct |
2 |
Correct |
6 ms |
12628 KB |
Output is correct |
3 |
Correct |
4 ms |
11988 KB |
Output is correct |
4 |
Correct |
4 ms |
12116 KB |
Output is correct |
5 |
Correct |
7 ms |
12580 KB |
Output is correct |
6 |
Correct |
254 ms |
39960 KB |
Output is correct |
7 |
Correct |
425 ms |
62868 KB |
Output is correct |
8 |
Correct |
12 ms |
13912 KB |
Output is correct |
9 |
Correct |
20 ms |
14980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
11988 KB |
Output is correct |
2 |
Correct |
4 ms |
11988 KB |
Output is correct |
3 |
Correct |
4 ms |
11988 KB |
Output is correct |
4 |
Correct |
4 ms |
12116 KB |
Output is correct |
5 |
Correct |
7 ms |
12580 KB |
Output is correct |
6 |
Correct |
4 ms |
11988 KB |
Output is correct |
7 |
Correct |
4 ms |
11988 KB |
Output is correct |
8 |
Correct |
5 ms |
12116 KB |
Output is correct |
9 |
Correct |
5 ms |
12244 KB |
Output is correct |
10 |
Correct |
6 ms |
12388 KB |
Output is correct |
11 |
Correct |
7 ms |
12200 KB |
Output is correct |
12 |
Correct |
4 ms |
12080 KB |
Output is correct |
13 |
Correct |
4 ms |
11988 KB |
Output is correct |
14 |
Correct |
7 ms |
12376 KB |
Output is correct |
15 |
Correct |
4 ms |
11988 KB |
Output is correct |
16 |
Correct |
4 ms |
12116 KB |
Output is correct |
17 |
Correct |
4 ms |
11988 KB |
Output is correct |
18 |
Correct |
5 ms |
12088 KB |
Output is correct |
19 |
Correct |
4 ms |
12092 KB |
Output is correct |
20 |
Correct |
5 ms |
12116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
11988 KB |
Output is correct |
2 |
Correct |
4 ms |
11988 KB |
Output is correct |
3 |
Correct |
5 ms |
12116 KB |
Output is correct |
4 |
Correct |
5 ms |
12244 KB |
Output is correct |
5 |
Correct |
6 ms |
12388 KB |
Output is correct |
6 |
Correct |
7 ms |
12200 KB |
Output is correct |
7 |
Correct |
4 ms |
12080 KB |
Output is correct |
8 |
Correct |
4 ms |
11988 KB |
Output is correct |
9 |
Correct |
7 ms |
12376 KB |
Output is correct |
10 |
Correct |
4 ms |
11988 KB |
Output is correct |
11 |
Correct |
4 ms |
12116 KB |
Output is correct |
12 |
Correct |
4 ms |
11988 KB |
Output is correct |
13 |
Correct |
5 ms |
12088 KB |
Output is correct |
14 |
Correct |
4 ms |
12092 KB |
Output is correct |
15 |
Correct |
5 ms |
12116 KB |
Output is correct |
16 |
Correct |
4 ms |
11988 KB |
Output is correct |
17 |
Correct |
4 ms |
11988 KB |
Output is correct |
18 |
Correct |
4 ms |
11988 KB |
Output is correct |
19 |
Correct |
4 ms |
12116 KB |
Output is correct |
20 |
Correct |
7 ms |
12580 KB |
Output is correct |
21 |
Correct |
6 ms |
12628 KB |
Output is correct |
22 |
Correct |
12 ms |
13912 KB |
Output is correct |
23 |
Correct |
20 ms |
14980 KB |
Output is correct |
24 |
Correct |
27 ms |
11988 KB |
Output is correct |
25 |
Correct |
10 ms |
12992 KB |
Output is correct |
26 |
Correct |
90 ms |
23924 KB |
Output is correct |
27 |
Correct |
36 ms |
17732 KB |
Output is correct |
28 |
Correct |
93 ms |
24716 KB |
Output is correct |
29 |
Correct |
32 ms |
15372 KB |
Output is correct |
30 |
Correct |
98 ms |
22776 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
11988 KB |
Output is correct |
2 |
Correct |
4 ms |
11988 KB |
Output is correct |
3 |
Correct |
5 ms |
12116 KB |
Output is correct |
4 |
Correct |
5 ms |
12244 KB |
Output is correct |
5 |
Correct |
6 ms |
12388 KB |
Output is correct |
6 |
Correct |
7 ms |
12200 KB |
Output is correct |
7 |
Correct |
4 ms |
12080 KB |
Output is correct |
8 |
Correct |
4 ms |
11988 KB |
Output is correct |
9 |
Correct |
7 ms |
12376 KB |
Output is correct |
10 |
Correct |
4 ms |
11988 KB |
Output is correct |
11 |
Correct |
4 ms |
12116 KB |
Output is correct |
12 |
Correct |
4 ms |
11988 KB |
Output is correct |
13 |
Correct |
5 ms |
12088 KB |
Output is correct |
14 |
Correct |
4 ms |
12092 KB |
Output is correct |
15 |
Correct |
5 ms |
12116 KB |
Output is correct |
16 |
Correct |
10 ms |
12992 KB |
Output is correct |
17 |
Correct |
90 ms |
23924 KB |
Output is correct |
18 |
Correct |
36 ms |
17732 KB |
Output is correct |
19 |
Correct |
93 ms |
24716 KB |
Output is correct |
20 |
Correct |
32 ms |
15372 KB |
Output is correct |
21 |
Correct |
98 ms |
22776 KB |
Output is correct |
22 |
Correct |
81 ms |
12064 KB |
Output is correct |
23 |
Correct |
4 ms |
11988 KB |
Output is correct |
24 |
Correct |
4 ms |
11988 KB |
Output is correct |
25 |
Correct |
92 ms |
34108 KB |
Output is correct |
26 |
Correct |
4 ms |
11988 KB |
Output is correct |
27 |
Correct |
4 ms |
12116 KB |
Output is correct |
28 |
Correct |
7 ms |
12580 KB |
Output is correct |
29 |
Correct |
6 ms |
12628 KB |
Output is correct |
30 |
Correct |
254 ms |
39960 KB |
Output is correct |
31 |
Correct |
425 ms |
62868 KB |
Output is correct |
32 |
Correct |
12 ms |
13912 KB |
Output is correct |
33 |
Correct |
20 ms |
14980 KB |
Output is correct |
34 |
Correct |
27 ms |
11988 KB |
Output is correct |
35 |
Correct |
24 ms |
14496 KB |
Output is correct |
36 |
Correct |
140 ms |
26500 KB |
Output is correct |
37 |
Correct |
64 ms |
15268 KB |
Output is correct |