QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#525337 | #3556. Making Friends on Joitter is Fun | Dimash# | 0 | 3ms | 13000kb | C++17 | 1.9kb | 2024-08-20 15:39:05 | 2024-08-20 15:39:06 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 12, MOD = (int)1e9 + 7;
int n,m,s[N],p[N];
map<int,int> g[N],gr[N];
int get(int v) {
if(p[v] == v) return v;
return p[v] = get(p[v]);
}
ll cl(int x) {
return x * 1ll * (x + 1);
}
ll res = 0;
void merge(int a,int b) {
res -= g[a][b] * 1ll * s[b];
res -= g[b][a] * 1ll * s[a];
res -= cl(s[a]);res -= cl(s[b]);
res += cl(s[a] + s[b]);
vector<pair<int,int>> mrg;
p[a] = b;
for(auto [x,y]:gr[b]) {
res += y * 1ll * s[a];
}
for(auto [x,y]:gr[a]) {
if(!y) continue;
if(x == b) continue;
res += y * 1ll * s[b];
g[x].erase(a);
g[x][b] += y;
gr[b][x] += y;
if(g[b].count(x)) {
mrg.push_back({x,b});
}
}
for(auto [x,y]:g[a]) {
if(x == b || !y) continue;
gr[x].erase(a);
gr[x][b] += y;
g[b][x] += y;
if(gr[b].count(x)) {
mrg.push_back({x,b});
}
}
s[b] += s[a];
g[a].clear();
gr[a].clear();
for(auto [x,y]:mrg) {
x = get(x);
y = get(y);
if(x == y) continue;
merge(x,y);
}
}
void test() {
cin >> n >> m;
for(int i = 1;i <= n;i++) {
p[i] = i;
s[i] = 1;
}
for(int i = 1;i <= m;i++) {
int a,b;
cin >> a >> b;
a = get(a);
b = get(b);
if(a == b || g[a].count(b)) {
cout << res << '\n';
continue;
}
if(!g[b].count(a)) {
g[a][b]++;
gr[b][a]++;
res += s[b];
cout << res << '\n';
} else {
merge(a,b);
cout << res << '\n';
}
}
}
int main() {
ios_base::sync_with_stdio(false);cin.tie(0);
int t = 1;
// cin >> t;
while(t--) {
test();
}
}
詳細信息
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 0
Wrong Answer
time: 3ms
memory: 13000kb
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 20 20 20 20 20 20 20 20 20 20 20 20
result:
wrong answer 9th lines differ - expected: '16', found: '20'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Skipped
Dependency #1:
0%