QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#525501#3556. Making Friends on Joitter is FunDimash#0 0ms17872kbC++172.1kb2024-08-20 17:14:342024-08-20 17:14:35

Judging History

你现在查看的是最新测评结果

  • [2024-08-20 17:14:35]
  • 评测
  • 测评结果:0
  • 用时:0ms
  • 内存:17872kb
  • [2024-08-20 17:14:34]
  • 提交

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];
map<int,int> G[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;
    gr[b].erase(a);
    g[b].erase(a);
    gr[a].erase(b);
    g[a].erase(b);
    for(auto [x,y]:gr[b]) {
        res += y * 1ll * s[a];
    }
    for(auto [x,y]:gr[a]) {
        if(!y) continue;
        res += y * 1ll * s[b];
        G[x][b] = 1;
        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(!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;
        int x = a,y = b;
        a = get(a);
        b = get(b);
        if(a == b || G[a].count(b)) {
            cout << res << '\n';
            continue;
        }
        if(!g[b].count(a)) {
            G[x][b] = 1;
            g[a][b] = s[b];
            gr[b][a] = s[b];
            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: 0ms
memory: 17872kb

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%