Submission #938687

# Submission time Handle Problem Language Result Execution time Memory
938687 2024-03-05T12:25:04 Z guagua0407 Making Friends on Joitter is Fun (JOI20_joitter2) C++17
100 / 100
1096 ms 89544 KB
//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define f first
#define s second
#define all(x) x.begin(),x.end()
#define _ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};

void setIO(string s) {
    freopen((s + ".in").c_str(), "r", stdin);
    freopen((s + ".out").c_str(), "w", stdout);
}

vector<map<int,int>> S1,S2,R1,R2;
vector<set<int>> belong;
queue<pair<int,int>> q;
ll ans=0;

struct DSU{
    vector<int> e;
    DSU(int n){
        e=vector<int>(n,-1);
        for(int i=0;i<n;i++){
            belong[i].insert(i);
        }
    }
    int find(int x){
        return (e[x]<0?x:e[x]=find(e[x]));
    }
    bool same(int a,int b){
        return find(a)==find(b);
    }
    int sz(int x){
        return -e[find(x)];
    }
    void sir1(int a,int b){
        if(R1[a].size()<sz(b)){
            for(auto it=R1[a].begin();it!=R1[a].end();){
                if(find((*it).f)==b){
                    S1[(*it).f].erase(a);
                    it=R1[a].erase(it);
                }
                else{
                    it++;
                }
            }
        }
        else{
            for(auto v:belong[b]){
                if(R1[a].find(v)!=R1[a].end()){
                    R1[a].erase(v);
                    S1[v].erase(a);
                }
            }
        }
    }
    void sir2(int a,int b){
        if(R2[a].size()<S2[b].size()){
            for(auto v:R2[a]){
                if(S2[b].find(v.f)!=S2[b].end()){
                    q.push({v.f,b});
                }
            }
        }
        else{
            for(auto v:S2[b]){
                if(R2[a].find(v.f)!=R2[a].end()){
                    q.push({v.f,b});
                }
            }
        }
    }
    bool unite(int a,int b){
        a=find(a);
        b=find(b);
        if(a==b) return false;
        ans+=2ll*sz(a)*sz(b)-1ll*S2[a][b]*sz(b)-1ll*S2[b][a]*sz(a);
        //cout<<S2[a][b]<<' '<<sz(b)<<'\n';
        //cout<<S2[b][a]<<' '<<sz(a)<<'\n';
        //cout<<a+1<<' '<<b+1<<' '<<2ll*sz(a)*sz(b)-1ll*S2[a][b]*sz(b)-1ll*S2[b][a]*sz(a)<<'\n';
        // clear (a,b) edges
        S2[a].erase(b);
        S2[b].erase(a);
        R2[a].erase(b);
        R2[b].erase(a);
        sir1(a,b);
        swap(a,b);
        sir1(a,b);
        //new one way edges
        int cnt=0;
        if(R1[a].size()>R1[b].size()) swap(a,b);
        for(auto v:R1[a]){
            if(R1[b].find(v.f)!=R1[b].end()){
                cnt++;
            }
        }
        ans+=1ll*((int)R1[a].size()-cnt)*sz(b)+1ll*((int)R1[b].size()-cnt)*sz(a);
        //out<<a+1<<' '<<b+1<<' '<<1ll*((int)R1[a].size()-cnt)*sz(b)+1ll*((int)R1[b].size()-cnt)*sz(a)<<'\n';
        //new scc edges
        sir2(a,b);
        swap(a,b);
        sir2(a,b);
        // merge
        if(e[a]>e[b]) swap(a,b);
        for(auto v:R1[b]){
            if(R1[a].find(v.f)!=R1[a].end()){
                S2[find(v.f)][a]--;
                if(S2[find(v.f)][a]==0) S2[find(v.f)].erase(a);
                R2[a][find(v.f)]--;
                if(R2[a][find(v.f)]==0) R2[a].erase(find(v.f));
            }
        }
        for(auto v:S2[b]){
            S2[a][v.f]+=v.s;
            R2[v.f].erase(b);
            R2[v.f][a]+=v.s;
        }
        for(auto v:R1[b]){
            S1[v.f].erase(b);
            S1[v.f][a]=1;
            R1[a][v.f]=1;
        }
        for(auto v:R2[b]){
            S2[v.f].erase(b);
            S2[v.f][a]+=v.s;
            R2[a][v.f]+=v.s;

        }
        e[a]+=e[b];
        e[b]=a;
        for(auto v:belong[b]){
            belong[a].insert(v);
        }
        return true;
    }
};

int main() {_
    int n,m;
    cin>>n>>m;
    //assert(n==4 and m==6);
    S1.resize(n);
    S2.resize(n);
    R1.resize(n);
    R2.resize(n);
    belong.resize(n);
    DSU dsu(n);
    for(int i=0;i<m;i++){
        int a,b;
        cin>>a>>b;
        a--;
        b--;
        if(dsu.same(a,b)){
            cout<<ans<<'\n';
            continue;
        }
        int pa=dsu.find(a);
        int pb=dsu.find(b);
        if(S1[a].find(pb)!=S1[a].end()){
            cout<<ans<<'\n';
            continue;
        }
        S1[a][pb]++;
        S2[pa][pb]++;
        R1[pb][a]=1;
        R2[pb][pa]++;
        ans+=dsu.sz(pb);
        //cout<<"-"<<'\n';
        if(S2[pb].find(pa)!=S2[pb].end()){
            //cout<<i+1<<' '<<pa+1<<' '<<pb+1<<'\n';
            q.push({pa,pb});
            while(!q.empty()){
                auto tmp=q.front();
                q.pop();
                dsu.unite(tmp.f,tmp.s);
            }
        }
        cout<<ans<<'\n';
    }
    return 0;
}
//maybe its multiset not set
//yeeorz
//laborz

Compilation message

joitter2.cpp: In member function 'void DSU::sir1(int, int)':
joitter2.cpp:42:24: warning: comparison of integer expressions of different signedness: 'std::map<int, int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   42 |         if(R1[a].size()<sz(b)){
      |            ~~~~~~~~~~~~^~~~~~
joitter2.cpp: In function 'void setIO(std::string)':
joitter2.cpp:15:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   15 |     freopen((s + ".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
joitter2.cpp:16:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   16 |     freopen((s + ".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 452 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 600 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 356 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 356 KB Output is correct
17 Correct 0 ms 344 KB Output is correct
18 Correct 0 ms 356 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 532 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 1 ms 344 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 452 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 600 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 356 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 356 KB Output is correct
17 Correct 0 ms 344 KB Output is correct
18 Correct 0 ms 356 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 532 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 1 ms 344 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 2 ms 856 KB Output is correct
35 Correct 61 ms 5976 KB Output is correct
36 Correct 92 ms 9372 KB Output is correct
37 Correct 99 ms 9812 KB Output is correct
38 Correct 86 ms 9072 KB Output is correct
39 Correct 3 ms 1116 KB Output is correct
40 Correct 4 ms 1536 KB Output is correct
41 Correct 4 ms 1372 KB Output is correct
42 Correct 3 ms 1116 KB Output is correct
43 Correct 5 ms 1116 KB Output is correct
44 Correct 4 ms 1116 KB Output is correct
45 Correct 2 ms 1116 KB Output is correct
46 Correct 3 ms 1116 KB Output is correct
47 Correct 5 ms 1372 KB Output is correct
48 Correct 4 ms 1372 KB Output is correct
49 Correct 11 ms 2260 KB Output is correct
50 Correct 99 ms 9788 KB Output is correct
51 Correct 8 ms 1564 KB Output is correct
52 Correct 73 ms 7476 KB Output is correct
53 Correct 11 ms 2140 KB Output is correct
54 Correct 80 ms 8644 KB Output is correct
55 Correct 6 ms 1628 KB Output is correct
56 Correct 6 ms 1848 KB Output is correct
57 Correct 6 ms 1628 KB Output is correct
58 Correct 6 ms 1628 KB Output is correct
59 Correct 4 ms 1372 KB Output is correct
60 Correct 60 ms 5444 KB Output is correct
61 Correct 5 ms 1372 KB Output is correct
62 Correct 84 ms 8808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 452 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 600 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 356 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 356 KB Output is correct
17 Correct 0 ms 344 KB Output is correct
18 Correct 0 ms 356 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 532 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 1 ms 344 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 2 ms 856 KB Output is correct
35 Correct 61 ms 5976 KB Output is correct
36 Correct 92 ms 9372 KB Output is correct
37 Correct 99 ms 9812 KB Output is correct
38 Correct 86 ms 9072 KB Output is correct
39 Correct 3 ms 1116 KB Output is correct
40 Correct 4 ms 1536 KB Output is correct
41 Correct 4 ms 1372 KB Output is correct
42 Correct 3 ms 1116 KB Output is correct
43 Correct 5 ms 1116 KB Output is correct
44 Correct 4 ms 1116 KB Output is correct
45 Correct 2 ms 1116 KB Output is correct
46 Correct 3 ms 1116 KB Output is correct
47 Correct 5 ms 1372 KB Output is correct
48 Correct 4 ms 1372 KB Output is correct
49 Correct 11 ms 2260 KB Output is correct
50 Correct 99 ms 9788 KB Output is correct
51 Correct 8 ms 1564 KB Output is correct
52 Correct 73 ms 7476 KB Output is correct
53 Correct 11 ms 2140 KB Output is correct
54 Correct 80 ms 8644 KB Output is correct
55 Correct 6 ms 1628 KB Output is correct
56 Correct 6 ms 1848 KB Output is correct
57 Correct 6 ms 1628 KB Output is correct
58 Correct 6 ms 1628 KB Output is correct
59 Correct 4 ms 1372 KB Output is correct
60 Correct 60 ms 5444 KB Output is correct
61 Correct 5 ms 1372 KB Output is correct
62 Correct 84 ms 8808 KB Output is correct
63 Correct 528 ms 89424 KB Output is correct
64 Correct 528 ms 89544 KB Output is correct
65 Correct 555 ms 89424 KB Output is correct
66 Correct 178 ms 37204 KB Output is correct
67 Correct 444 ms 64768 KB Output is correct
68 Correct 162 ms 36936 KB Output is correct
69 Correct 448 ms 44248 KB Output is correct
70 Correct 174 ms 37968 KB Output is correct
71 Correct 187 ms 38076 KB Output is correct
72 Correct 466 ms 49480 KB Output is correct
73 Correct 464 ms 51040 KB Output is correct
74 Correct 1096 ms 78624 KB Output is correct
75 Correct 599 ms 51976 KB Output is correct
76 Correct 826 ms 65104 KB Output is correct
77 Correct 806 ms 65360 KB Output is correct
78 Correct 264 ms 48724 KB Output is correct
79 Correct 472 ms 52500 KB Output is correct
80 Correct 220 ms 45080 KB Output is correct
81 Correct 336 ms 48320 KB Output is correct
82 Correct 827 ms 71508 KB Output is correct
83 Correct 828 ms 71268 KB Output is correct
84 Correct 656 ms 70488 KB Output is correct
85 Correct 627 ms 70456 KB Output is correct
86 Correct 303 ms 70744 KB Output is correct
87 Correct 310 ms 73168 KB Output is correct
88 Correct 438 ms 50588 KB Output is correct
89 Correct 773 ms 62796 KB Output is correct