Submission #745450

#TimeUsernameProblemLanguageResultExecution timeMemory
745450AlexandruabcdeMarshmallow Molecules (CCO19_day2problem2)C++14
25 / 25
140 ms24576 KiB
#include <bits/stdc++.h> using namespace std; typedef pair <int, int> PII; constexpr int NMAX = 1e5 + 5; int N, M; set <int> G[NMAX]; map <PII, int> mp; void Unite (int x, int y) { if (G[x].size() > G[y].size()) swap(G[x], G[y]); for (auto it : G[x]) G[y].insert(it); } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> N >> M; for (int i = 1; i <= M; ++ i ) { int x, y; cin >> x >> y; if (x > y) swap(x, y); G[x].insert(y); } long long answer = 0; for (int i = 1; i <= N; ++ i ) { answer += 1LL * G[i].size(); if (G[i].empty()) continue; auto it = G[i].begin(); G[i].erase(it); Unite(i, *it); } cout << answer << '\n'; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...