QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#525641#3556. Making Friends on Joitter is FunDimash0 6ms22728kbC++172.5kb2024-08-20 19:51:502024-08-20 19:51:50

Judging History

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

  • [2024-08-20 19:51:50]
  • 评测
  • 测评结果:0
  • 用时:6ms
  • 内存:22728kb
  • [2024-08-20 19:51:50]
  • 提交

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],Gr[N];
int get(int v) {
    // cerr << v  << '\n';
    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) {
    a = get(a);
    b = get(b);
    if(a == b) return;
    res -= cl(s[a]);
    res -= cl(s[b]);
    res -= g[a][b] * 1ll * s[b];
    res -= g[b][a] * 1ll * s[a];
    g[b].erase(a);
    g[a].erase(b);
    res += cl(s[a] + s[b]);
    {
        for(auto [x,ok]:Gr[a]) {
            if(!ok) continue;
            if(get(x) == b) continue;
            int z = get(x);
            if(G[x][b] != 1) {
                g[z][b]++;
                gr[b][z]++;
                res += s[b];
            }
            G[x][b] = 1;
            Gr[b][x] = 1;
        }
    }
    {
        for(auto [x,ok]:Gr[b]) {
            if(!ok) continue;
            if(get(x) == a) continue;
            if(G[x][a] != 1) {
                res += s[a];
            }
        }
    }
    {
        for(auto [x,y]:g[a]) {
            g[b][x] += y;
            gr[x][b] += y;
        }
    }
    p[a] = b;
    s[b] += s[a];
    // cerr << a << ' ' << b << '\n';
    // for(auto [x,y]:g[b]) {
    //     cerr << x << ' ' << y << ' ' << g[x][b] << "y\n";
    // }
    // cerr << (int)g[b].size() << "x\n";
    vector<pair<int,int>> f;
    for(auto [x,y]:g[b]) {
        if(!y) continue;
        if(g[x][b]) {
            f.push_back({b,x});
        }
    }
    for(auto [c,d]:f) {
        merge(c,d);
    }
}
//G vertex->component
//g comp->comp
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 = get(a),y = get(b);
        if(x == y || G[a][y]) {
            cout << res << '\n';
            continue;
        }
        if(!g[y][x]) {
            G[a][y] = 1;Gr[y][a] = 1;
            g[x][y] += 1;gr[y][x] += 1;
            res += s[y];
            cout << res << '\n';
        } else {
            // cerr << a << ' ' << b << '\n';
            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: 1
Accepted
time: 3ms
memory: 22728kb

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: 1
Accepted
time: 6ms
memory: 22284kb

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: 0
Wrong Answer
time: 0ms
memory: 22372kb

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
11
16
21
21
21
21
21
21
21
21
21
21

result:

wrong answer 10th lines differ - expected: '15', found: '16'

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Skipped

Dependency #1:

0%