Submission #736134

# Submission time Handle Problem Language Result Execution time Memory
736134 2023-05-05T09:00:53 Z keta_tsimakuridze Making Friends on Joitter is Fun (JOI20_joitter2) C++14
100 / 100
1152 ms 63544 KB
#include<bits/stdc++.h>
#define f first
#define s second
#define int long long
#define pii pair<int,int>
using namespace std;
const int N = 2e5 + 5, mod = 1e9 + 7; // !
int t, ans = 0, p[N], sz[N];
multiset<int> out[N];
set<pair<int,int>> in[N];
void _(int x, int y) {
    if(out[x].find(y) != out[x].end()) out[x].erase(y);
    while(in[y].upper_bound({x, 0}) != in[y].end() && (*in[y].upper_bound({x, 0})).f == x) {
        pii p = *in[y].upper_bound({x, 0});
        in[y].erase(p);
        ans -= sz[y];
    }
}
int find(int x) {
    return (p[x] == x ? p[x] : p[x] = find(p[x]));
}
void merge(int u, int v) {
    int U = find(u), V = find(v);
    if(U == V) return;
    _(U, V);
    _(V, U);
    if((int)in[U].size() + (int)out[U].size() < (int)in[V].size() + (int)out[V].size()) swap(U, V);
    p[V] = U;
    ans += sz[U] * sz[V] * 2;
    sz[U] += sz[V];
    ans += (int)in[U].size() * sz[V];
    vector<int> nbx;
    while(out[V].size()) {
        int x = *out[V].begin();
        out[V].erase(out[V].find(x));
        out[U].insert(x);
        pii p = *in[x].upper_bound({V, 0});
        in[x].erase(p); p.f = U; in[x].insert(p);
        if(out[x].find(U) != out[x].end()) nbx.push_back(x);
    }
    while(in[V].size()) {
        pii p = *in[V].begin();
        int x = p.f;
        out[x].erase(out[x].find(V));
        in[V].erase(p);
        if(in[U].find(p) == in[U].end()) ans += (sz[U] - sz[V]), out[x].insert(U);
        else ans -= sz[V];
        in[U].insert(p);
        if(out[U].find(x) != out[U].end()) nbx.push_back(x);

    }
    for(int i = 0; i < nbx.size(); i++) {
        int x = nbx[i];
        while(out[x].find(u) != out[x].end()) {
            out[x].erase(out[x].find(u));
            in[u].erase(*in[u].upper_bound({x, 0}));
            ans -= sz[u];
        }
        while(out[u].find(x) != out[u].end()) {
            out[u].erase(out[u].find(x));
            in[x].erase(*in[x].upper_bound({u, 0}));
            ans -= sz[x];
        }
    }
    for(int i = 0; i < nbx.size(); i++) merge(U, nbx[i]);
}
main(){
    int n, m;
    cin >> n >> m;
    for(int i = 1; i <= n; i++) p[i] = i, sz[i] = 1;
    for(int i = 1; i <= m; i++) {
        int u, v;
        cin >> u >> v;
        int U = find(u), V = find(v);
        if(out[V].find(U) != out[V].end() || U == V) merge(u, v);
        else
        if(in[V].find({U, u}) == in[V].end()) ans += sz[V], out[U].insert(V), in[V].insert({U, u});
        cout << ans << endl;
    }
 }

Compilation message

joitter2.cpp: In function 'void merge(long long int, long long int)':
joitter2.cpp:52:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   52 |     for(int i = 0; i < nbx.size(); i++) {
      |                    ~~^~~~~~~~~~~~
joitter2.cpp:65:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   65 |     for(int i = 0; i < nbx.size(); i++) merge(U, nbx[i]);
      |                    ~~^~~~~~~~~~~~
joitter2.cpp: At global scope:
joitter2.cpp:67:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   67 | main(){
      | ^~~~
# Verdict Execution time Memory Grader output
1 Correct 10 ms 19092 KB Output is correct
2 Correct 10 ms 19028 KB Output is correct
3 Correct 9 ms 19100 KB Output is correct
4 Correct 10 ms 19028 KB Output is correct
5 Correct 10 ms 19028 KB Output is correct
6 Correct 10 ms 19028 KB Output is correct
7 Correct 14 ms 19028 KB Output is correct
8 Correct 15 ms 19108 KB Output is correct
9 Correct 13 ms 19028 KB Output is correct
10 Correct 11 ms 19088 KB Output is correct
11 Correct 11 ms 19028 KB Output is correct
12 Correct 9 ms 19044 KB Output is correct
13 Correct 10 ms 19028 KB Output is correct
14 Correct 12 ms 19056 KB Output is correct
15 Correct 11 ms 19088 KB Output is correct
16 Correct 10 ms 18984 KB Output is correct
17 Correct 10 ms 19028 KB Output is correct
18 Correct 11 ms 19096 KB Output is correct
19 Correct 11 ms 19112 KB Output is correct
20 Correct 10 ms 19096 KB Output is correct
21 Correct 14 ms 19088 KB Output is correct
22 Correct 10 ms 19028 KB Output is correct
23 Correct 11 ms 19104 KB Output is correct
24 Correct 11 ms 19028 KB Output is correct
25 Correct 13 ms 19096 KB Output is correct
26 Correct 9 ms 19028 KB Output is correct
27 Correct 11 ms 19028 KB Output is correct
28 Correct 10 ms 19096 KB Output is correct
29 Correct 11 ms 19096 KB Output is correct
30 Correct 10 ms 19084 KB Output is correct
31 Correct 14 ms 19028 KB Output is correct
32 Correct 10 ms 19068 KB Output is correct
33 Correct 12 ms 19028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 19092 KB Output is correct
2 Correct 10 ms 19028 KB Output is correct
3 Correct 9 ms 19100 KB Output is correct
4 Correct 10 ms 19028 KB Output is correct
5 Correct 10 ms 19028 KB Output is correct
6 Correct 10 ms 19028 KB Output is correct
7 Correct 14 ms 19028 KB Output is correct
8 Correct 15 ms 19108 KB Output is correct
9 Correct 13 ms 19028 KB Output is correct
10 Correct 11 ms 19088 KB Output is correct
11 Correct 11 ms 19028 KB Output is correct
12 Correct 9 ms 19044 KB Output is correct
13 Correct 10 ms 19028 KB Output is correct
14 Correct 12 ms 19056 KB Output is correct
15 Correct 11 ms 19088 KB Output is correct
16 Correct 10 ms 18984 KB Output is correct
17 Correct 10 ms 19028 KB Output is correct
18 Correct 11 ms 19096 KB Output is correct
19 Correct 11 ms 19112 KB Output is correct
20 Correct 10 ms 19096 KB Output is correct
21 Correct 14 ms 19088 KB Output is correct
22 Correct 10 ms 19028 KB Output is correct
23 Correct 11 ms 19104 KB Output is correct
24 Correct 11 ms 19028 KB Output is correct
25 Correct 13 ms 19096 KB Output is correct
26 Correct 9 ms 19028 KB Output is correct
27 Correct 11 ms 19028 KB Output is correct
28 Correct 10 ms 19096 KB Output is correct
29 Correct 11 ms 19096 KB Output is correct
30 Correct 10 ms 19084 KB Output is correct
31 Correct 14 ms 19028 KB Output is correct
32 Correct 10 ms 19068 KB Output is correct
33 Correct 12 ms 19028 KB Output is correct
34 Correct 26 ms 19156 KB Output is correct
35 Correct 499 ms 24564 KB Output is correct
36 Correct 551 ms 26904 KB Output is correct
37 Correct 546 ms 27016 KB Output is correct
38 Correct 527 ms 26828 KB Output is correct
39 Correct 17 ms 19156 KB Output is correct
40 Correct 17 ms 19236 KB Output is correct
41 Correct 17 ms 19244 KB Output is correct
42 Correct 17 ms 19096 KB Output is correct
43 Correct 18 ms 19256 KB Output is correct
44 Correct 18 ms 19284 KB Output is correct
45 Correct 18 ms 19196 KB Output is correct
46 Correct 17 ms 19156 KB Output is correct
47 Correct 18 ms 19284 KB Output is correct
48 Correct 17 ms 19284 KB Output is correct
49 Correct 32 ms 19796 KB Output is correct
50 Correct 536 ms 26912 KB Output is correct
51 Correct 30 ms 19500 KB Output is correct
52 Correct 513 ms 25804 KB Output is correct
53 Correct 36 ms 19788 KB Output is correct
54 Correct 522 ms 26428 KB Output is correct
55 Correct 19 ms 19540 KB Output is correct
56 Correct 19 ms 19624 KB Output is correct
57 Correct 22 ms 19936 KB Output is correct
58 Correct 20 ms 19876 KB Output is correct
59 Correct 18 ms 19156 KB Output is correct
60 Correct 505 ms 24100 KB Output is correct
61 Correct 19 ms 19328 KB Output is correct
62 Correct 522 ms 26572 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 19092 KB Output is correct
2 Correct 10 ms 19028 KB Output is correct
3 Correct 9 ms 19100 KB Output is correct
4 Correct 10 ms 19028 KB Output is correct
5 Correct 10 ms 19028 KB Output is correct
6 Correct 10 ms 19028 KB Output is correct
7 Correct 14 ms 19028 KB Output is correct
8 Correct 15 ms 19108 KB Output is correct
9 Correct 13 ms 19028 KB Output is correct
10 Correct 11 ms 19088 KB Output is correct
11 Correct 11 ms 19028 KB Output is correct
12 Correct 9 ms 19044 KB Output is correct
13 Correct 10 ms 19028 KB Output is correct
14 Correct 12 ms 19056 KB Output is correct
15 Correct 11 ms 19088 KB Output is correct
16 Correct 10 ms 18984 KB Output is correct
17 Correct 10 ms 19028 KB Output is correct
18 Correct 11 ms 19096 KB Output is correct
19 Correct 11 ms 19112 KB Output is correct
20 Correct 10 ms 19096 KB Output is correct
21 Correct 14 ms 19088 KB Output is correct
22 Correct 10 ms 19028 KB Output is correct
23 Correct 11 ms 19104 KB Output is correct
24 Correct 11 ms 19028 KB Output is correct
25 Correct 13 ms 19096 KB Output is correct
26 Correct 9 ms 19028 KB Output is correct
27 Correct 11 ms 19028 KB Output is correct
28 Correct 10 ms 19096 KB Output is correct
29 Correct 11 ms 19096 KB Output is correct
30 Correct 10 ms 19084 KB Output is correct
31 Correct 14 ms 19028 KB Output is correct
32 Correct 10 ms 19068 KB Output is correct
33 Correct 12 ms 19028 KB Output is correct
34 Correct 26 ms 19156 KB Output is correct
35 Correct 499 ms 24564 KB Output is correct
36 Correct 551 ms 26904 KB Output is correct
37 Correct 546 ms 27016 KB Output is correct
38 Correct 527 ms 26828 KB Output is correct
39 Correct 17 ms 19156 KB Output is correct
40 Correct 17 ms 19236 KB Output is correct
41 Correct 17 ms 19244 KB Output is correct
42 Correct 17 ms 19096 KB Output is correct
43 Correct 18 ms 19256 KB Output is correct
44 Correct 18 ms 19284 KB Output is correct
45 Correct 18 ms 19196 KB Output is correct
46 Correct 17 ms 19156 KB Output is correct
47 Correct 18 ms 19284 KB Output is correct
48 Correct 17 ms 19284 KB Output is correct
49 Correct 32 ms 19796 KB Output is correct
50 Correct 536 ms 26912 KB Output is correct
51 Correct 30 ms 19500 KB Output is correct
52 Correct 513 ms 25804 KB Output is correct
53 Correct 36 ms 19788 KB Output is correct
54 Correct 522 ms 26428 KB Output is correct
55 Correct 19 ms 19540 KB Output is correct
56 Correct 19 ms 19624 KB Output is correct
57 Correct 22 ms 19936 KB Output is correct
58 Correct 20 ms 19876 KB Output is correct
59 Correct 18 ms 19156 KB Output is correct
60 Correct 505 ms 24100 KB Output is correct
61 Correct 19 ms 19328 KB Output is correct
62 Correct 522 ms 26572 KB Output is correct
63 Correct 764 ms 58916 KB Output is correct
64 Correct 772 ms 58944 KB Output is correct
65 Correct 778 ms 58868 KB Output is correct
66 Correct 409 ms 24968 KB Output is correct
67 Correct 452 ms 29740 KB Output is correct
68 Correct 466 ms 25028 KB Output is correct
69 Correct 608 ms 30484 KB Output is correct
70 Correct 427 ms 25020 KB Output is correct
71 Correct 445 ms 25060 KB Output is correct
72 Correct 545 ms 29800 KB Output is correct
73 Correct 555 ms 29872 KB Output is correct
74 Correct 1152 ms 39600 KB Output is correct
75 Correct 936 ms 35568 KB Output is correct
76 Correct 1080 ms 39088 KB Output is correct
77 Correct 994 ms 39120 KB Output is correct
78 Correct 414 ms 29396 KB Output is correct
79 Correct 781 ms 33100 KB Output is correct
80 Correct 439 ms 29304 KB Output is correct
81 Correct 839 ms 32580 KB Output is correct
82 Correct 941 ms 46156 KB Output is correct
83 Correct 1097 ms 46044 KB Output is correct
84 Correct 832 ms 63340 KB Output is correct
85 Correct 893 ms 63544 KB Output is correct
86 Correct 429 ms 24196 KB Output is correct
87 Correct 635 ms 26476 KB Output is correct
88 Correct 568 ms 29980 KB Output is correct
89 Correct 970 ms 38492 KB Output is correct