#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 = 0;
for(auto i : Size) m = std::max(m,i);
int ct = 0;
for(int i = 0; i < N; i++) if(Size[i]==m) ct += uf.size(i);
std::cout << ct << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
197 ms |
25936 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
12 ms |
29788 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
197 ms |
25936 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |