QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#710748#3556. Making Friends on Joitter is Funsitablechair0 35ms150472kbC++232.8kb2024-11-04 21:22:192024-11-04 21:22:19

Judging History

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

  • [2024-11-04 21:22:19]
  • 评测
  • 测评结果:0
  • 用时:35ms
  • 内存:150472kb
  • [2024-11-04 21:22:19]
  • 提交

answer

#include <bits/stdc++.h>

#define ll long long
#define ldb long double
#define endl '\n'
#define For(i,l,r) for(int i=l;i<=r;i++)
#define ForD(i,r,l) for(int i=r;i>=l;i--)
#define REP(i,l,r) For(i,l,r-1)
#define PER(i,r,l) ForD(i,r-1,l)
#define ff first
#define ss second
#define pb push_back
#define all(x) x.begin(),x.end()
#define All(x,n) x+1,x+1+n
#define Alll(x,n) x,x+n
#define sz(x) (signed)x.size()
#define unq(x) x.resize(unique(all(x))-x.begin())
#define mpa make_pair

#ifdef NCGM
#include"debug.h"
#else
#define debug(...) "fr";
#endif

using namespace std;

const int N=1e6+3;

int n,m,f[N],f1[N],mmb[N];
ll ans=0;
map<int,int> mp[N],mp1[N];
set<int> st[N];
vector<int> ahu;
vector<pair<int,int>> ahu1;
int find_set(int u) {
    return (f[u]<0?u:f[u]=find_set(f[u]));
}
int find_set1(int u) {
    return (f1[u]<0?u:f1[u]=find_set1(f1[u]));
}

void unite(int u,int v) {
    int a=find_set(u),b=find_set(v);
    if (a==b) return;
    int a1=find_set1(u),b1=find_set1(v);
    ans+=1LL*f[a]*(sz(st[a1])-1);
    ans+=1LL*f[b]*(sz(st[b1])-1);
    if (sz(st[a1])<sz(st[b1])) swap(a1,b1);
    if (sz(mp[a])+sz(mp1[a])<sz(mp[b])+sz(mp1[b])) swap(a,b);
    ahu.clear();
    for(auto el: mp[b]) {
        ahu.pb(el.ff);
        if (mp[find_set(el.ff)].count(a) || mp[find_set(el.ff)].count(b)) ahu1.pb({el.ff,a});
    }
    for(auto el: mp1[b])
        if (mp1[el.ff].count(a) || mp1[el.ff].count(b)) ahu1.pb({el.ff,a});

    for(auto el: mp1[b1]) {
       mp[el.ff][a]=mp[el.ff][b];
       mp[el.ff].erase(mp[el.ff].find(b));
    }
    for(auto el: ahu) {
        mp1[el][a]=mp1[el][b];
        mp1[el].erase(mp1[el].find(b));
    }
    for(auto el: st[b1]) st[a1].insert(el);
    st[b1].clear();
    for(auto el: mp[b]) mp[a][el.ff]+=el.ss;
    mp[b].clear();
    for(auto el: mp1[b]) mp1[a][el.ff]+=el.ss;
    mp1[b].clear();
    f[a]+=f[b];
    f1[a1]+=f1[b1];
    f[b]=a;
    f1[b1]=a1;
    ans-=1LL*f[a]*(sz(st[find_set1(a)])-1);
    //for(auto el: ahu1) {
       // if (find_set(el)==find_set(a)) continue;
       // unite(el,a);
    //}
}
void add_edge(int u,int v) {
    if (find_set(u)==find_set(v) || st[find_set1(v)].find(u)!=st[find_set1(v)].end()) return;
    mp[find_set(v)][find_set(u)]++;
    mp1[find_set(u)][find_set(v)]++;
    if (st[find_set1(v)].find(u)==st[find_set1(v)].end()) ans-=f[find_set(v)];
    st[find_set1(v)].insert(u);
    if (mp[find_set(u)].count(find_set(v))) unite(u,v);
    while(sz(ahu1)) {
        unite(ahu1.back().ff,ahu1.back().ss);
        ahu1.pop_back();
    }
}
int main() {
    cin.tie(0)->sync_with_stdio(0);
    cin >> n >> m;
    For(i,1,n) f[i]=f1[i]=-1;
    For(i,1,n) st[i].insert(i);
    For(i,1,m) {
        int u,v;
        cin >> u >> v;
        add_edge(u,v);
        cout << ans << endl;
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 1
Accepted
time: 15ms
memory: 148656kb

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: 16ms
memory: 148512kb

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
Accepted
time: 35ms
memory: 150472kb

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
15
20
20
20
20
20
20
20
20
20
20

result:

ok 20 lines

Test #4:

score: 1
Accepted
time: 20ms
memory: 148720kb

input:

10 10
9 1
10 6
4 10
1 8
4 9
7 8
2 4
6 5
10 1
1 7

output:

1
2
3
4
5
6
7
8
9
10

result:

ok 10 lines

Test #5:

score: 0
Wrong Answer
time: 20ms
memory: 150108kb

input:

10 90
10 6
7 8
8 4
9 3
2 8
9 2
1 10
1 8
8 9
5 6
2 10
4 3
7 2
10 2
10 5
3 7
1 9
8 7
1 2
9 1
7 6
9 7
10 3
8 3
6 3
7 5
8 2
6 1
4 9
7 1
4 2
6 8
7 3
9 8
5 1
10 4
6 4
1 4
6 7
3 1
9 10
3 2
1 7
2 5
10 1
2 7
2 1
5 8
7 9
2 4
6 9
3 8
10 7
8 5
5 4
8 6
5 10
3 5
5 7
8 1
4 7
4 1
10 8
9 5
4 8
6 10
1 6
2 9
1 5
6 5
3...

output:

1
2
3
4
5
6
7
8
9
10
11
12
13
17
18
19
20
32
32
59
60
60
60
60
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90
90

result:

wrong answer 18th lines differ - expected: '52', found: '32'

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Skipped

Dependency #1:

0%