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 <set>
#define pii pair<int, int>
#define pub push_back
#define si set<int>
#define fst first
#define snd second
using namespace std;
int n, m, pr[500001] = {}, sz[500001] = {}, comp = 0;
vector<pii> edge;
vector<si> adj;
int findRep(int x) {return pr[x] = (pr[x] == x) ? x : findRep(pr[x]);}
void joinG(int a, int b)
{
a = findRep(a), b = findRep(b);
if (a != b)
{
pr[b] = a;
sz[a] += sz[b];
sz[b] = 0;
comp--;
}
}
int main()
{
ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
int res = 0;
cin >> n >> m;
for (int i = 0; i < n; i++) {adj.pub(si()); pr[i] = i; sz[i] = 1;}
comp = n;
for (int i = 0; i < m; i++)
{
int u, v; char c; cin >> u >> v >> c;
if (c == 'T') {joinG(u - 1, v - 1);}
else {edge.pub({u - 1, v - 1});}
}
for (int i = 0; i < edge.size(); i++)
{
int u = findRep(edge[i].fst), v = findRep(edge[i].snd);
if (u != v)
{
adj[u].insert(v);
adj[v].insert(u);
}
}
for (int i = 0; i < n; i++)
{
if (adj[i].size() == comp - 1) {res += sz[i];}
}
cout << res << "\n";
return 0;
}
Compilation message (stderr)
menesinis_bilietas.cpp: In function 'int main()':
menesinis_bilietas.cpp:38:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < edge.size(); i++)
~~^~~~~~~~~~~~~
menesinis_bilietas.cpp:49:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (adj[i].size() == comp - 1) {res += sz[i];}
~~~~~~~~~~~~~~^~~~~~~~~~~
# | 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... |