#include <iostream>
#include <vector>
#include <unordered_set>
struct uf
{
private:
std::vector<int> data;
public:
int find(int val)
{
if (data[val] < 0) return val;
return data[val] = find(data[val]);
}
uf(int s) : data(s,-1) {}
bool same(int a, int b)
{
return find(a)==find(b);
}
bool unite(int a, int b)
{
a = find(a);
b = find(b);
if (a==b) return false;
if (-data[a] < -data[b]) std::swap(a,b);
data[a] += data[b];
data[b] = a;
return true;
}
int size(int a)
{
return -data[find(a)];
}
};
int main()
{
int N,M;
std::cin >> N >> M;
uf uf(N);
std::vector<std::pair<int,int>> n;
for(int i = 0; i < M; i++)
{
int u,v; char c;
std::cin >> u >> v >> c;
u--;v--;
if(c=='T') uf.unite(u,v);
else n.emplace_back(u,v);
}
std::vector<std::unordered_set<int>> neigh(N);
for(auto[u,v] : n)
{
u = uf.find(u);
v = uf.find(v);
if(u==v) continue;
neigh[u].insert(v);
neigh[v].insert(u);
}
std::vector<int> Size(N);
for(int i = 0; i < N; i++)
{
if(i!=uf.find(i)) continue;
auto& sz = Size[i];
sz = uf.size(i);
for(auto n : neigh[i]) sz += uf.size(n);
}
int m = N;
int ct = 0;
for(int i = 0; i < N; i++) if(Size[i]==m) ct += uf.size(i);
std::cout << ct << "\n";
}
Compilation message
menesinis_bilietas.cpp: In function 'int main()':
menesinis_bilietas.cpp:52:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
52 | for(auto[u,v] : n)
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
190 ms |
25760 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
14 ms |
29788 KB |
Output is correct |
5 |
Correct |
1 ms |
1116 KB |
Output is correct |
6 |
Correct |
63 ms |
3252 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
29788 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
4 ms |
1116 KB |
Output is correct |
6 |
Correct |
296 ms |
41404 KB |
Output is correct |
7 |
Correct |
482 ms |
111120 KB |
Output is correct |
8 |
Correct |
10 ms |
4404 KB |
Output is correct |
9 |
Correct |
20 ms |
5416 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
4 ms |
1116 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
476 KB |
Output is correct |
9 |
Correct |
2 ms |
344 KB |
Output is correct |
10 |
Correct |
4 ms |
604 KB |
Output is correct |
11 |
Correct |
2 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
4 ms |
860 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
448 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
476 KB |
Output is correct |
4 |
Correct |
2 ms |
344 KB |
Output is correct |
5 |
Correct |
4 ms |
604 KB |
Output is correct |
6 |
Correct |
2 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
4 ms |
860 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
448 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
4 ms |
1116 KB |
Output is correct |
21 |
Correct |
1 ms |
1116 KB |
Output is correct |
22 |
Correct |
10 ms |
4404 KB |
Output is correct |
23 |
Correct |
20 ms |
5416 KB |
Output is correct |
24 |
Correct |
63 ms |
3252 KB |
Output is correct |
25 |
Correct |
10 ms |
1112 KB |
Output is correct |
26 |
Correct |
134 ms |
9424 KB |
Output is correct |
27 |
Correct |
50 ms |
3784 KB |
Output is correct |
28 |
Correct |
108 ms |
13724 KB |
Output is correct |
29 |
Correct |
37 ms |
3012 KB |
Output is correct |
30 |
Correct |
123 ms |
12988 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
476 KB |
Output is correct |
4 |
Correct |
2 ms |
344 KB |
Output is correct |
5 |
Correct |
4 ms |
604 KB |
Output is correct |
6 |
Correct |
2 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
4 ms |
860 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
448 KB |
Output is correct |
16 |
Correct |
10 ms |
1112 KB |
Output is correct |
17 |
Correct |
134 ms |
9424 KB |
Output is correct |
18 |
Correct |
50 ms |
3784 KB |
Output is correct |
19 |
Correct |
108 ms |
13724 KB |
Output is correct |
20 |
Correct |
37 ms |
3012 KB |
Output is correct |
21 |
Correct |
123 ms |
12988 KB |
Output is correct |
22 |
Correct |
190 ms |
25760 KB |
Output is correct |
23 |
Correct |
0 ms |
344 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
14 ms |
29788 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
4 ms |
1116 KB |
Output is correct |
29 |
Correct |
1 ms |
1116 KB |
Output is correct |
30 |
Correct |
296 ms |
41404 KB |
Output is correct |
31 |
Correct |
482 ms |
111120 KB |
Output is correct |
32 |
Correct |
10 ms |
4404 KB |
Output is correct |
33 |
Correct |
20 ms |
5416 KB |
Output is correct |
34 |
Correct |
63 ms |
3252 KB |
Output is correct |
35 |
Correct |
31 ms |
4552 KB |
Output is correct |
36 |
Correct |
193 ms |
19236 KB |
Output is correct |
37 |
Correct |
117 ms |
16208 KB |
Output is correct |