QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#106678 | #3556. Making Friends on Joitter is Fun | bashkort# | 0 | 1ms | 3636kb | C++20 | 2.2kb | 2023-05-18 18:41:03 | 2024-05-31 13:40:09 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
vector<set<pair<int, int>>> in(n), out(n);
vector<int> fa(n), sz(n, 1);
iota(fa.begin(), fa.end(), 0);
auto leader = [&](int x) {
while (fa[x] != x) {
x = fa[x] = fa[fa[x]];
}
return x;
};
ll ans = 0;
auto unite = [&](int x, int y) {
x = leader(x), y = leader(y);
if (x == y) {
return;
}
if (in[x].size() + out[x].size() < in[y].size() + out[y].size()) {
swap(x, y);
}
ans -= 1LL * sz[x] * (sz[x] - 1) + 1LL * sz[y] * (sz[y] - 1);
ans -= in[x].size() * 1LL * sz[x];
sz[x] += sz[y];
ans += in[x].size() * 1LL * sz[x];
ans += 1LL * sz[x] * (sz[x] - 1);
fa[y] = x;
for (auto [p, v] : in[y]) {
out[p].extract({y, v});
ans -= sz[y];
assert(p == leader(p));
if (leader(p) != x) {
out[p].insert({x, v});
ans += sz[x];
in[x].insert({p, v});
}
}
for (auto [p, v] : out[y]) {
in[p].extract({y, v});
ans -= sz[p];
assert(p == leader(p));
if (leader(p) != x) {
in[p].insert({x, v});
ans += sz[x];
out[x].insert({p, v});
}
}
};
for (int i = 0; i < m; ++i) {
int a, b;
cin >> a >> b;
a -= 1, b -= 1;
int la = leader(a), lb = leader(b);
if (la != lb) {
auto it = in[la].lower_bound({lb, 0});
if (it != in[la].end() && it->first == lb) {
unite(la, lb);
} else {
if (!in[lb].count({la, a})) {
ans += sz[lb];
in[lb].insert({la, a});
out[la].insert({lb, a});
}
}
}
cout << ans << '\n';
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 3636kb
input:
5 20 4 2 1 5 2 3 2 5 3 2 3 1 4 5 1 2 5 2 1 4 4 1 3 4 5 1 2 4 2 1 4 3 1 3 5 4 3 5 5 3
output:
1 2 3 4 6 7 8 14 22 26 26 26 26 26 26 26 26 26 26 26
result:
wrong answer 8th lines differ - expected: '12', found: '14'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Skipped
Dependency #1:
0%