이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#ifdef local
#define safe cerr<<__PRETTY_FUNCTION__<<" line "<<__LINE__<<" safe\n"
#define pary(a...) danb(#a, a)
#define debug(a...) qqbx(#a, a)
template <typename ...T> void qqbx(const char *s, T ...a) {
int cnt = sizeof...(T);
((std::cerr << "\033[1;32m(" << s << ") = (") , ... , (std::cerr << a << (--cnt ? ", " : ")\033[0m\n")));
}
template <typename T> void danb(const char *s, T L, T R) {
std::cerr << "\033[1;32m[ " << s << " ] = [ ";
for (int f = 0; L != R; ++L)
std::cerr << (f++ ? ", " : "") << *L;
std::cerr << " ]\033[0m\n";
}
#else
#define debug(...) ((void)0)
#define safe ((void)0)
#define pary(...) ((void)0)
#endif // local
#define all(v) begin(v),end(v)
#define pb emplace_back
using namespace std;
using ll = long long;
using ld = long double;
template <typename T> using min_heap = priority_queue<T, vector<T>, greater<T>>;
const int mod = 1000000007;
const int inf = 1e9;
const ll INF = 1e18;
const int maxn = 100025;
int pa[maxn], sz[maxn];
int anc(int x) { return x==pa[x] ? x : pa[x]=anc(pa[x]); }
signed main() {
ios_base::sync_with_stdio(0), cin.tie(0);
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++) pa[i] = i, sz[i] = 1;
map< pair<int,int>, int > mp;
function<void(int,int)> join = [&](int a, int b) {
pa[a] = b;
sz[b] += sz[a];
map< pair<int,int>, int > tmp;
pair<int,int> jizz = { -1, -1 };
for (auto [e, val]: mp) {
auto [s, t] = e;
s = anc(s), t = anc(t);
if (s != t) {
if (sz[t] != 1) {
if (tmp.count({ t, s })) {
jizz = { s, t };
}
tmp[{s, t}] = 1LL * sz[s] * sz[t];
}
else
tmp[{s, t}] += val;
}
}
mp = tmp;
if (jizz.first != -1)
join(jizz.first, jizz.second);
};
while (m--) {
int a, b;
cin >> a >> b;
--a, --b;
a = anc(a), b = anc(b);
if (a != b) {
if (mp.count({ b, a })) {
join(a, b);
} else {
if (sz[b] != 1)
mp[{a, b}] = 1LL * sz[a] * sz[b];
else
mp[{a, b}] += 1;
}
}
ll ans = 0;
for (auto [e, val]: mp)
ans += val;
for (int i = 0; i < n; i++) if (anc(i) == i)
ans += 1LL * sz[i] * (sz[i] - 1);
cout << ans << '\n';
// addEdge(anc(a), anc(b));
// out[a].emplace_back(b);
// in[b].emplace_back(a);
}
}
컴파일 시 표준 에러 (stderr) 메시지
joitter2.cpp: In lambda function:
joitter2.cpp:47:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
47 | for (auto [e, val]: mp) {
| ^
joitter2.cpp:48:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
48 | auto [s, t] = e;
| ^
joitter2.cpp: In function 'int main()':
joitter2.cpp:82:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
82 | for (auto [e, val]: mp)
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |