#include <bits/stdc++.h>
using namespace std;
#define ld long double
#define pb push_back
#define fi first
#define se second
#define debug(x) cout << #x << " => " << x << "\n"
const int mod = 998244353;
const ld E = 1e-4;
struct Edge {
int u, v;
char c;
} a[500005];
int n, m, sum;
vector <int> g[500005];
int par[500005], sz[500005];
vector <int> e[500005];
int find(int u) {
if(u == par[u]) return u;
return par[u] = find(par[u]);
}
int join(int u, int v) {
u = find(u);
v = find(v);
if(u == v) return 0;
if(e[u].size() > e[v].size()) {
par[v] = u;
sz[u] += sz[v];
for(auto i : e[v]) e[u].pb(i);
}
else {
par[u] = v;
sz[v] += sz[u];
for(auto i : e[u]) e[v].pb(i);
}
return 1;
}
int ans[500005];
void solve () {
cin >> n >> m;
for(int i = 1; i <= n; i++) {
par[i] = i;
sz[i] = 1;
ans[i] = -1;
}
for(int i = 1; i <= m; i++) {
cin >> a[i].u >> a[i].v >> a[i].c;
}
for(int i = 1; i <= m; i++) {
if(a[i].c == 'A') {
e[a[i].u].pb(a[i].v);
e[a[i].v].pb(a[i].u);
}
}
for(int i = 1; i <= m; i++) {
if(a[i].c == 'T') {
join(a[i].u, a[i].v);
}
}
for(int i = 1; i <= n; i++) {
for(int j = 0; j < e[i].size(); j++)
e[i][j] = find(e[i][j]);
sort(e[i].begin(), e[i].end());
e[i].resize(unique(e[i].begin(), e[i].end()) - e[i].begin());
}
for(int i = 1; i <= n; i++) {
int u = find(i);
if(ans[u] == -1) {
int s = sz[u];
for(auto j : e[u]) {
if(find(j) != u) s += sz[find(j)];
}
if(s >= n) ans[u] = 1;
else ans[u] = 0;
}
sum += ans[u];
}
cout << sum << "\n";
}
signed main () {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve ();
}
Compilation message
menesinis_bilietas.cpp: In function 'void solve()':
menesinis_bilietas.cpp:70:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
70 | for(int j = 0; j < e[i].size(); j++)
| ~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
129 ms |
32604 KB |
Output is correct |
2 |
Correct |
11 ms |
23764 KB |
Output is correct |
3 |
Correct |
12 ms |
23812 KB |
Output is correct |
4 |
Correct |
18 ms |
29268 KB |
Output is correct |
5 |
Correct |
11 ms |
23984 KB |
Output is correct |
6 |
Correct |
50 ms |
26440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
29268 KB |
Output is correct |
2 |
Correct |
11 ms |
23984 KB |
Output is correct |
3 |
Correct |
12 ms |
23764 KB |
Output is correct |
4 |
Correct |
13 ms |
23856 KB |
Output is correct |
5 |
Correct |
14 ms |
24020 KB |
Output is correct |
6 |
Correct |
109 ms |
32156 KB |
Output is correct |
7 |
Correct |
219 ms |
47508 KB |
Output is correct |
8 |
Correct |
18 ms |
24712 KB |
Output is correct |
9 |
Correct |
21 ms |
24932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23812 KB |
Output is correct |
3 |
Correct |
12 ms |
23764 KB |
Output is correct |
4 |
Correct |
13 ms |
23856 KB |
Output is correct |
5 |
Correct |
14 ms |
24020 KB |
Output is correct |
6 |
Correct |
12 ms |
23760 KB |
Output is correct |
7 |
Correct |
11 ms |
23764 KB |
Output is correct |
8 |
Correct |
13 ms |
23936 KB |
Output is correct |
9 |
Correct |
13 ms |
24036 KB |
Output is correct |
10 |
Correct |
15 ms |
24196 KB |
Output is correct |
11 |
Correct |
16 ms |
23976 KB |
Output is correct |
12 |
Correct |
15 ms |
23800 KB |
Output is correct |
13 |
Correct |
12 ms |
23856 KB |
Output is correct |
14 |
Correct |
16 ms |
24352 KB |
Output is correct |
15 |
Correct |
12 ms |
23828 KB |
Output is correct |
16 |
Correct |
12 ms |
23940 KB |
Output is correct |
17 |
Correct |
15 ms |
23764 KB |
Output is correct |
18 |
Correct |
13 ms |
23864 KB |
Output is correct |
19 |
Correct |
12 ms |
23892 KB |
Output is correct |
20 |
Correct |
14 ms |
23924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23760 KB |
Output is correct |
2 |
Correct |
11 ms |
23764 KB |
Output is correct |
3 |
Correct |
13 ms |
23936 KB |
Output is correct |
4 |
Correct |
13 ms |
24036 KB |
Output is correct |
5 |
Correct |
15 ms |
24196 KB |
Output is correct |
6 |
Correct |
16 ms |
23976 KB |
Output is correct |
7 |
Correct |
15 ms |
23800 KB |
Output is correct |
8 |
Correct |
12 ms |
23856 KB |
Output is correct |
9 |
Correct |
16 ms |
24352 KB |
Output is correct |
10 |
Correct |
12 ms |
23828 KB |
Output is correct |
11 |
Correct |
12 ms |
23940 KB |
Output is correct |
12 |
Correct |
15 ms |
23764 KB |
Output is correct |
13 |
Correct |
13 ms |
23864 KB |
Output is correct |
14 |
Correct |
12 ms |
23892 KB |
Output is correct |
15 |
Correct |
14 ms |
23924 KB |
Output is correct |
16 |
Correct |
11 ms |
23764 KB |
Output is correct |
17 |
Correct |
12 ms |
23812 KB |
Output is correct |
18 |
Correct |
12 ms |
23764 KB |
Output is correct |
19 |
Correct |
13 ms |
23856 KB |
Output is correct |
20 |
Correct |
14 ms |
24020 KB |
Output is correct |
21 |
Correct |
11 ms |
23984 KB |
Output is correct |
22 |
Correct |
18 ms |
24712 KB |
Output is correct |
23 |
Correct |
21 ms |
24932 KB |
Output is correct |
24 |
Correct |
50 ms |
26440 KB |
Output is correct |
25 |
Correct |
30 ms |
25432 KB |
Output is correct |
26 |
Correct |
165 ms |
43712 KB |
Output is correct |
27 |
Correct |
67 ms |
31680 KB |
Output is correct |
28 |
Correct |
72 ms |
31444 KB |
Output is correct |
29 |
Correct |
57 ms |
29608 KB |
Output is correct |
30 |
Correct |
79 ms |
32660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23760 KB |
Output is correct |
2 |
Correct |
11 ms |
23764 KB |
Output is correct |
3 |
Correct |
13 ms |
23936 KB |
Output is correct |
4 |
Correct |
13 ms |
24036 KB |
Output is correct |
5 |
Correct |
15 ms |
24196 KB |
Output is correct |
6 |
Correct |
16 ms |
23976 KB |
Output is correct |
7 |
Correct |
15 ms |
23800 KB |
Output is correct |
8 |
Correct |
12 ms |
23856 KB |
Output is correct |
9 |
Correct |
16 ms |
24352 KB |
Output is correct |
10 |
Correct |
12 ms |
23828 KB |
Output is correct |
11 |
Correct |
12 ms |
23940 KB |
Output is correct |
12 |
Correct |
15 ms |
23764 KB |
Output is correct |
13 |
Correct |
13 ms |
23864 KB |
Output is correct |
14 |
Correct |
12 ms |
23892 KB |
Output is correct |
15 |
Correct |
14 ms |
23924 KB |
Output is correct |
16 |
Correct |
30 ms |
25432 KB |
Output is correct |
17 |
Correct |
165 ms |
43712 KB |
Output is correct |
18 |
Correct |
67 ms |
31680 KB |
Output is correct |
19 |
Correct |
72 ms |
31444 KB |
Output is correct |
20 |
Correct |
57 ms |
29608 KB |
Output is correct |
21 |
Correct |
79 ms |
32660 KB |
Output is correct |
22 |
Correct |
129 ms |
32604 KB |
Output is correct |
23 |
Correct |
11 ms |
23764 KB |
Output is correct |
24 |
Correct |
12 ms |
23812 KB |
Output is correct |
25 |
Correct |
18 ms |
29268 KB |
Output is correct |
26 |
Correct |
12 ms |
23764 KB |
Output is correct |
27 |
Correct |
13 ms |
23856 KB |
Output is correct |
28 |
Correct |
14 ms |
24020 KB |
Output is correct |
29 |
Correct |
11 ms |
23984 KB |
Output is correct |
30 |
Correct |
109 ms |
32156 KB |
Output is correct |
31 |
Correct |
219 ms |
47508 KB |
Output is correct |
32 |
Correct |
18 ms |
24712 KB |
Output is correct |
33 |
Correct |
21 ms |
24932 KB |
Output is correct |
34 |
Correct |
50 ms |
26440 KB |
Output is correct |
35 |
Correct |
39 ms |
27832 KB |
Output is correct |
36 |
Correct |
285 ms |
55048 KB |
Output is correct |
37 |
Correct |
79 ms |
31704 KB |
Output is correct |