#include <bits/stdc++.h>
#define f first
#define s second
using namespace std;
#define int long long
const int maxn = 5e5 + 69;
vector<int> g[maxn];
vector<int> p(maxn), sz(maxn);
vector<int> deg(maxn), vis(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];
}
}
void dfs(int v) {
vis[v] = 1;
for(auto u : g[v]) {
if(!vis[u]) {
unite(u, v);
dfs(u);
}
}
}
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') {
g[a].push_back(b);
g[b].push_back(a);
}
else
edges.push_back({a, b});
}
int c = 0;
for(int i = 1;i <= n;i++) {
if(!vis[i]) {
dfs(i);
++c;
}
}
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}))
continue;
else {
bio.insert({a, b});
deg[a]++;
deg[b]++;
}
}
for(int i = 1;i <= n;i++) {
if(deg[i] == c - 1)
ans += sz[i];
}
if(c == 1)
ans = n;
cout << ans;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
295 ms |
49488 KB |
Output is correct |
2 |
Correct |
12 ms |
27676 KB |
Output is correct |
3 |
Correct |
13 ms |
27780 KB |
Output is correct |
4 |
Correct |
16 ms |
27612 KB |
Output is correct |
5 |
Correct |
14 ms |
27604 KB |
Output is correct |
6 |
Correct |
46 ms |
35160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
27612 KB |
Output is correct |
2 |
Correct |
14 ms |
27604 KB |
Output is correct |
3 |
Correct |
13 ms |
27604 KB |
Output is correct |
4 |
Correct |
12 ms |
27732 KB |
Output is correct |
5 |
Correct |
14 ms |
28264 KB |
Output is correct |
6 |
Correct |
251 ms |
53456 KB |
Output is correct |
7 |
Correct |
385 ms |
59440 KB |
Output is correct |
8 |
Correct |
18 ms |
28888 KB |
Output is correct |
9 |
Correct |
24 ms |
29848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
27676 KB |
Output is correct |
2 |
Correct |
13 ms |
27780 KB |
Output is correct |
3 |
Correct |
13 ms |
27604 KB |
Output is correct |
4 |
Correct |
12 ms |
27732 KB |
Output is correct |
5 |
Correct |
14 ms |
28264 KB |
Output is correct |
6 |
Correct |
11 ms |
27732 KB |
Output is correct |
7 |
Correct |
11 ms |
27684 KB |
Output is correct |
8 |
Correct |
15 ms |
27988 KB |
Output is correct |
9 |
Correct |
15 ms |
27860 KB |
Output is correct |
10 |
Incorrect |
14 ms |
28036 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
27732 KB |
Output is correct |
2 |
Correct |
11 ms |
27684 KB |
Output is correct |
3 |
Correct |
15 ms |
27988 KB |
Output is correct |
4 |
Correct |
15 ms |
27860 KB |
Output is correct |
5 |
Incorrect |
14 ms |
28036 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
27732 KB |
Output is correct |
2 |
Correct |
11 ms |
27684 KB |
Output is correct |
3 |
Correct |
15 ms |
27988 KB |
Output is correct |
4 |
Correct |
15 ms |
27860 KB |
Output is correct |
5 |
Incorrect |
14 ms |
28036 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |