QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#106681 | #3556. Making Friends on Joitter is Fun | bashkort# | 0 | 0ms | 3820kb | C++20 | 2.4kb | 2023-05-18 19:00:02 | 2024-05-31 13:40:13 |
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);
}
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});
if (!in[x].count({p, 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});
if (!out[x].count({p, v})) {
ans += sz[p];
out[x].insert({p, v});
}
}
}
ans -= 1LL * sz[x] * (sz[x] - 1) + 1LL * sz[y] * (sz[y] - 1);
sz[x] += sz[y];
ans += in[x].size() * 1LL * sz[y];
ans += 1LL * sz[x] * (sz[x] - 1);
// for (auto [p, xt] : in[x]) {
// assert(leader(p) == p);
// assert(p != x);
// }
};
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);
// cout << "unite: " << a << " " << b << endl;
} 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;
}
详细
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 1
Accepted
time: 0ms
memory: 3820kb
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 12 16 20 20 20 20 20 20 20 20 20 20 20
result:
ok 20 lines
Test #2:
score: 0
Accepted
time: 0ms
memory: 3784kb
input:
5 20 4 5 1 2 2 1 4 3 3 2 5 2 1 5 4 1 2 3 1 3 1 4 4 2 5 1 3 5 2 5 3 1 2 4 3 4 5 4 5 3
output:
1 2 3 4 6 8 13 13 16 16 20 20 20 20 20 20 20 20 20 20
result:
ok 20 lines
Test #3:
score: -1
Wrong Answer
time: 0ms
memory: 3620kb
input:
5 20 3 1 5 1 3 4 5 2 1 2 5 4 3 5 2 4 1 3 2 5 4 5 4 3 4 2 2 3 3 2 5 3 2 1 1 5 4 1 1 4
output:
1 2 3 4 5 6 7 8 10 13 16 20 20 20 20 20 20 20 20 20
result:
wrong answer 9th lines differ - expected: '11', found: '10'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Skipped
Dependency #1:
0%