QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#710763#3556. Making Friends on Joitter is Funsitablechair0 9ms50748kbC++232.8kb2024-11-04 21:32:112024-11-04 21:32:12

Judging History

This is the latest submission verdict.

  • [2024-11-04 21:32:12]
  • Judged
  • Verdict: 0
  • Time: 9ms
  • Memory: 50748kb
  • [2024-11-04 21:32:11]
  • Submitted

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=3e5+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,ahu1[N];
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[a].pb(el.ff);
    }
    for(auto el: mp1[b])
        if (mp1[el.ff].count(a) || mp1[el.ff].count(b)) ahu1[a].pb(el.ff);

    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[a]) {
        if (find_set(el)==find_set(a)) continue;
        unite(el,a);
    }
    ahu1[a].clear();
}
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);
}
int main() {
    cin.tie(0)->sync_with_stdio(0);
    // freopen("B.INP","r",stdin);
    // freopen("B.OUT","w",stdout);
    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;
}

详细

Subtask #1:

score: 0
Runtime Error

Test #1:

score: 1
Accepted
time: 7ms
memory: 48772kb

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: 3ms
memory: 48716kb

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: 4ms
memory: 48644kb

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

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: 1
Accepted
time: 9ms
memory: 50748kb

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
52
52
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:

ok 90 lines

Test #6:

score: 1
Accepted
time: 4ms
memory: 50728kb

input:

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

output:

1
2
3
4
5
6
7
8
9
11
12
13
14
17
20
22
24
26
26
28
29
35
35
49
49
55
55
55
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:

ok 90 lines

Test #7:

score: 0
Runtime Error

input:

50 2450
21 49
31 13
28 21
35 9
40 19
8 18
8 41
13 46
5 2
46 9
30 46
37 36
4 19
23 33
28 39
2 23
38 28
13 26
46 44
29 27
35 15
10 23
49 33
2 6
16 22
2 10
29 15
18 5
15 40
46 29
18 47
31 5
9 45
18 29
15 27
40 27
12 43
14 22
8 31
50 29
16 4
35 43
36 40
16 34
28 14
30 13
9 40
44 47
33 36
10 29
26 33
8 1...

output:


result:


Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Skipped

Dependency #1:

0%