# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
989076 | canadavid1 | Monthly railway pass (LMIO18_menesinis_bilietas) | C++14 | 482 ms | 111120 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |