/*input
5 5
3 1 A
3 5 A
4 5 T
2 3 T
2 1 A
*/
#include <bits/stdc++.h>
using namespace std;
namespace my_template {
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef vector<pi> vpi;
typedef vector<vpi> vpii;
typedef vector<pl> vpl;
typedef vector<cd> vcd;
typedef vector<pd> vpd;
typedef vector<bool> vb;
typedef vector<vb> vbb;
typedef std::string str;
typedef std::vector<str> vs;
#define x first
#define y second
#define debug(...) cout<<"["<<#__VA_ARGS__<<": "<<__VA_ARGS__<<"]\n"
const ld PI = 3.14159265358979323846264338327950288419716939937510582097494L;
template<typename T>
pair<T, T> operator+(const pair<T, T> &a, const pair<T, T> &b) { return pair<T, T>(a.x + b.x, a.y + b.y); }
template<typename T>
pair<T, T> operator-(const pair<T, T> &a, const pair<T, T> &b) { return pair<T, T>(a.x - b.x, a.y - b.y); }
template<typename T>
T operator*(const pair<T, T> &a, const pair<T, T> &b) { return (a.x * b.x + a.y * b.y); }
template<typename T>
T operator^(const pair<T, T> &a, const pair<T, T> &b) { return (a.x * b.y - a.y * b.x); }
template<typename T>
void print(vector<T> vec, string name = "") {
cout << name;
for (auto u : vec)
cout << u << ' ';
cout << '\n';
}
}
using namespace my_template;
const int MOD = 1000000007;
const ll INF = std::numeric_limits<ll>::max();
const int MX = 500101;
vii trauk(MX);
vpi autob;
vector<unordered_set<int>> newEdges(MX);
vi sz;
vi p;
int findSet(int i) { return (p[i] == i) ? i : (p[i] = findSet(p[i])); }
bool isSameSet(int i, int j) { return findSet(i) == findSet(j); }
void unionSet(int i, int j) {
if (isSameSet(i, j)) return;
int x = findSet(i), y = findSet(j);
if (sz[x] < sz[y]) swap(x, y);
p[y] = x;
sz[x] += sz[y];
}
vi visited;
void dfs(int x) {
visited[x] = true;
for (auto u : trauk[x]) {
if (!visited[u]) {
dfs(u);
unionSet(x, u);
}
}
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
// #ifndef LOCAL
// freopen("lmio_2018_3e2_menesinis_bilietas_vyr.in", "r", stdin);
// freopen("lmio_2018_3e2_menesinis_bilietas_vyr.out", "w", stdout);
// #endif
int N, M;
cin >> N >> M;
p = vi(N + 1); for (int i = 0; i <= N; i++) p[i] = i;
sz = vi(N + 1, 1);
for (int i = 0; i < M; ++i)
{
int a, b;
char ch;
cin >> a >> b >> ch; a--; b--;
if (ch == 'A') {
autob.emplace_back(a, b);
} else {
trauk[a].emplace_back(b);
trauk[b].emplace_back(a);
}
}
visited = vi(MX, 0);
for (int i = 0; i < N; ++i)
{
if (!visited[i])
dfs(i);
}
for (auto u : autob) {
if (isSameSet(u.x, u.y)) continue;
newEdges[findSet(u.x)].insert(findSet(u.y));
newEdges[findSet(u.y)].insert(findSet(u.x));
}
vi allNewNodes;
{
set<int> turiu;
for (int i = 0; i < N; ++i)
{
turiu.insert(findSet(i));
}
allNewNodes = vi(turiu.begin(), turiu.end());
}
int newNodeCnt = (int)allNewNodes.size();
int ats = 0;
for (auto u : allNewNodes) {
// printf("u = %d, newEdges = %d, newNodeCnt = %d\n", u, (int)newEdges[u].size(), newNodeCnt);
if (1 + (int)newEdges[u].size() == newNodeCnt)
ats += sz[u];
}
printf("%d\n", ats);
}
/* Look for:
* special cases (n=1?)
* overflow (ll vs int?)
* the exact constraints (multiple sets are too slow for n=10^6 :( )
* array bounds
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
478 ms |
68204 KB |
Output is correct |
2 |
Correct |
30 ms |
41580 KB |
Output is correct |
3 |
Correct |
30 ms |
41452 KB |
Output is correct |
4 |
Correct |
171 ms |
69044 KB |
Output is correct |
5 |
Correct |
33 ms |
42220 KB |
Output is correct |
6 |
Correct |
90 ms |
46828 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
171 ms |
69044 KB |
Output is correct |
2 |
Correct |
33 ms |
42220 KB |
Output is correct |
3 |
Correct |
31 ms |
41484 KB |
Output is correct |
4 |
Correct |
31 ms |
41580 KB |
Output is correct |
5 |
Correct |
39 ms |
42220 KB |
Output is correct |
6 |
Correct |
513 ms |
80220 KB |
Output is correct |
7 |
Correct |
564 ms |
116848 KB |
Output is correct |
8 |
Correct |
42 ms |
44140 KB |
Output is correct |
9 |
Correct |
59 ms |
45532 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
41580 KB |
Output is correct |
2 |
Correct |
30 ms |
41452 KB |
Output is correct |
3 |
Correct |
31 ms |
41484 KB |
Output is correct |
4 |
Correct |
31 ms |
41580 KB |
Output is correct |
5 |
Correct |
39 ms |
42220 KB |
Output is correct |
6 |
Correct |
31 ms |
41452 KB |
Output is correct |
7 |
Correct |
31 ms |
41452 KB |
Output is correct |
8 |
Correct |
31 ms |
41580 KB |
Output is correct |
9 |
Correct |
37 ms |
41580 KB |
Output is correct |
10 |
Correct |
34 ms |
41708 KB |
Output is correct |
11 |
Correct |
34 ms |
41964 KB |
Output is correct |
12 |
Correct |
31 ms |
41452 KB |
Output is correct |
13 |
Correct |
31 ms |
41452 KB |
Output is correct |
14 |
Correct |
34 ms |
41860 KB |
Output is correct |
15 |
Correct |
31 ms |
41452 KB |
Output is correct |
16 |
Correct |
32 ms |
41580 KB |
Output is correct |
17 |
Correct |
30 ms |
41452 KB |
Output is correct |
18 |
Correct |
37 ms |
41708 KB |
Output is correct |
19 |
Correct |
31 ms |
41452 KB |
Output is correct |
20 |
Correct |
33 ms |
41600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
41452 KB |
Output is correct |
2 |
Correct |
31 ms |
41452 KB |
Output is correct |
3 |
Correct |
31 ms |
41580 KB |
Output is correct |
4 |
Correct |
37 ms |
41580 KB |
Output is correct |
5 |
Correct |
34 ms |
41708 KB |
Output is correct |
6 |
Correct |
34 ms |
41964 KB |
Output is correct |
7 |
Correct |
31 ms |
41452 KB |
Output is correct |
8 |
Correct |
31 ms |
41452 KB |
Output is correct |
9 |
Correct |
34 ms |
41860 KB |
Output is correct |
10 |
Correct |
31 ms |
41452 KB |
Output is correct |
11 |
Correct |
32 ms |
41580 KB |
Output is correct |
12 |
Correct |
30 ms |
41452 KB |
Output is correct |
13 |
Correct |
37 ms |
41708 KB |
Output is correct |
14 |
Correct |
31 ms |
41452 KB |
Output is correct |
15 |
Correct |
33 ms |
41600 KB |
Output is correct |
16 |
Correct |
30 ms |
41580 KB |
Output is correct |
17 |
Correct |
30 ms |
41452 KB |
Output is correct |
18 |
Correct |
31 ms |
41484 KB |
Output is correct |
19 |
Correct |
31 ms |
41580 KB |
Output is correct |
20 |
Correct |
39 ms |
42220 KB |
Output is correct |
21 |
Correct |
33 ms |
42220 KB |
Output is correct |
22 |
Correct |
42 ms |
44140 KB |
Output is correct |
23 |
Correct |
59 ms |
45532 KB |
Output is correct |
24 |
Correct |
90 ms |
46828 KB |
Output is correct |
25 |
Correct |
42 ms |
42244 KB |
Output is correct |
26 |
Correct |
153 ms |
49848 KB |
Output is correct |
27 |
Correct |
70 ms |
44392 KB |
Output is correct |
28 |
Correct |
199 ms |
55904 KB |
Output is correct |
29 |
Correct |
64 ms |
44140 KB |
Output is correct |
30 |
Correct |
230 ms |
54492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
41452 KB |
Output is correct |
2 |
Correct |
31 ms |
41452 KB |
Output is correct |
3 |
Correct |
31 ms |
41580 KB |
Output is correct |
4 |
Correct |
37 ms |
41580 KB |
Output is correct |
5 |
Correct |
34 ms |
41708 KB |
Output is correct |
6 |
Correct |
34 ms |
41964 KB |
Output is correct |
7 |
Correct |
31 ms |
41452 KB |
Output is correct |
8 |
Correct |
31 ms |
41452 KB |
Output is correct |
9 |
Correct |
34 ms |
41860 KB |
Output is correct |
10 |
Correct |
31 ms |
41452 KB |
Output is correct |
11 |
Correct |
32 ms |
41580 KB |
Output is correct |
12 |
Correct |
30 ms |
41452 KB |
Output is correct |
13 |
Correct |
37 ms |
41708 KB |
Output is correct |
14 |
Correct |
31 ms |
41452 KB |
Output is correct |
15 |
Correct |
33 ms |
41600 KB |
Output is correct |
16 |
Correct |
42 ms |
42244 KB |
Output is correct |
17 |
Correct |
153 ms |
49848 KB |
Output is correct |
18 |
Correct |
70 ms |
44392 KB |
Output is correct |
19 |
Correct |
199 ms |
55904 KB |
Output is correct |
20 |
Correct |
64 ms |
44140 KB |
Output is correct |
21 |
Correct |
230 ms |
54492 KB |
Output is correct |
22 |
Correct |
478 ms |
68204 KB |
Output is correct |
23 |
Correct |
30 ms |
41580 KB |
Output is correct |
24 |
Correct |
30 ms |
41452 KB |
Output is correct |
25 |
Correct |
171 ms |
69044 KB |
Output is correct |
26 |
Correct |
31 ms |
41484 KB |
Output is correct |
27 |
Correct |
31 ms |
41580 KB |
Output is correct |
28 |
Correct |
39 ms |
42220 KB |
Output is correct |
29 |
Correct |
33 ms |
42220 KB |
Output is correct |
30 |
Correct |
513 ms |
80220 KB |
Output is correct |
31 |
Correct |
564 ms |
116848 KB |
Output is correct |
32 |
Correct |
42 ms |
44140 KB |
Output is correct |
33 |
Correct |
59 ms |
45532 KB |
Output is correct |
34 |
Correct |
90 ms |
46828 KB |
Output is correct |
35 |
Correct |
67 ms |
45284 KB |
Output is correct |
36 |
Correct |
277 ms |
57688 KB |
Output is correct |
37 |
Correct |
248 ms |
52588 KB |
Output is correct |