이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "bits/stdc++.h"
#define st first
#define nd second
using lint = int64_t;
constexpr int MOD = int(1e9) + 7;
constexpr int INF = 0x3f3f3f3f;
constexpr int NINF = 0xcfcfcfcf;
constexpr lint LINF = 0x3f3f3f3f3f3f3f3f;
const long double PI = acosl(-1.0);
// Returns -1 if a < b, 0 if a = b and 1 if a > b.
int cmp_double(double a, double b = 0, double eps = 1e-9) {
return a + eps > b ? b + eps > a ? 0 : 1 : -1;
}
using namespace std;
struct UF {
vector<int> e, sz;
UF(int n) : e(n, -1) {}
bool same_set(int a, int b) { return find(a) == find(b); }
int size(int x) { return -e[find(x)]; }
int find(int x) { return e[x] < 0 ? x : e[x] = find(e[x]); }
bool unite(int a, int b) {
a = find(a), b = find(b);
if (a == b) return 0;
if (e[a] > e[b]) swap(a, b);
e[a] += e[b]; e[b] = a;
return 1;
}
};
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
//int __; cin >> __; for( int _ = 1 ; _ <= __ ; _++ ){ }
int n, m;
cin>>n>>m;
using pii = pair<int, int>;
vector<pii> bus;
UF uf(n);
while(m--){
int u, v;
cin>>u>>v;
u--, v--;
string type;
cin>>type;
if(type == "A"){
bus.emplace_back(u, v);
} else {
uf.unite(u, v);
}
}
map<int, int> compr;
int id = 0;
for(int u=0; u<n; u++)
if(!compr.count(uf.find(u)))
compr[uf.find(u)] = id++;
vector<set<int>> cg(id);
for(auto &[u, v]: bus){
u = uf.find(u);
v = uf.find(v);
cg[compr[u]].insert(compr[v]);
cg[compr[v]].insert(compr[u]);
}
int answer = 0;
for(auto &[u, c]: compr)
if((int) cg[c].size() == id-1)
answer += uf.size(u);
cout<<answer<<"\n";
return 0;
}
/*
[ ]Leu o problema certo???
[ ]Ver se precisa de long long
[ ]Viu o limite dos fors (é n? é m?)
[ ]Tamanho do vetor, será que é 2e5 em vez de 1e5??
[ ]Testar sample
[ ]Testar casos de borda
[ ]1LL no 1LL << i
[ ]Testar mod (é 1e9+7, mesmo?, será que o mod não ficou negativo?)
*/
# | 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... |