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 <bits/stdc++.h>
using namespace std;
#define int long long
int nSets;
vector<int> dsu;
int find(int x)
{
if (dsu[x] == x) return x;
return dsu[x] = find(dsu[x]);
}
void join(int a, int b)
{
a = find(a), b = find(b);
if (a == b) return;
dsu[a] = b;
nSets--;
}
signed main()
{
int N, M, a, b, ans = 0;
cin >> N >> M;
char t;
for (int i = 0; i < N; i++) dsu.push_back(i);
nSets = N;
vector<vector<pair<int, bool>>> adjlist(N, vector<pair<int, bool>>());
for (int i = 0; i < M; i++)
{
cin >> a >> b >> t;
a--, b--;
adjlist[a].push_back(pair<int, int>(b, t == 'A'));
adjlist[b].push_back(pair<int, int>(a, t == 'A'));
if (t == 'T') join(a, b);
}
vector<set<int>> btradjlist(N, set<int>());
for (int i = 0; i < N; i++)
{
for (auto j : adjlist[i])
{
if (j.second)
btradjlist[find(i)].insert(find(j.first));
}
}
for (int i = 0; i < N; i++)
{
if (btradjlist[find(i)].size() == nSets - 1) ans++;
}
cout << ans << '\n';
}
Compilation message (stderr)
menesinis_bilietas.cpp: In function 'int main()':
menesinis_bilietas.cpp:48:40: warning: comparison of integer expressions of different signedness: 'std::set<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
48 | if (btradjlist[find(i)].size() == nSets - 1) ans++;
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
# | 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... |