Submission #736137

# Submission time Handle Problem Language Result Execution time Memory
736137 2023-05-05T09:02:02 Z keta_tsimakuridze Making Friends on Joitter is Fun (JOI20_joitter2) C++17
100 / 100
1305 ms 62032 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++) 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++) merge(U, nbx[i]);
      |                    ~~^~~~~~~~~~~~
joitter2.cpp: At global scope:
joitter2.cpp:54:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   54 | main(){
      | ^~~~
# Verdict Execution time Memory Grader output
1 Correct 12 ms 18992 KB Output is correct
2 Correct 11 ms 19104 KB Output is correct
3 Correct 11 ms 19028 KB Output is correct
4 Correct 14 ms 19092 KB Output is correct
5 Correct 13 ms 19004 KB Output is correct
6 Correct 11 ms 19028 KB Output is correct
7 Correct 16 ms 19104 KB Output is correct
8 Correct 15 ms 19148 KB Output is correct
9 Correct 17 ms 19128 KB Output is correct
10 Correct 10 ms 19028 KB Output is correct
11 Correct 12 ms 19028 KB Output is correct
12 Correct 11 ms 19092 KB Output is correct
13 Correct 11 ms 19096 KB Output is correct
14 Correct 15 ms 19092 KB Output is correct
15 Correct 11 ms 18988 KB Output is correct
16 Correct 13 ms 19028 KB Output is correct
17 Correct 12 ms 19100 KB Output is correct
18 Correct 16 ms 19040 KB Output is correct
19 Correct 14 ms 19096 KB Output is correct
20 Correct 11 ms 19028 KB Output is correct
21 Correct 19 ms 19072 KB Output is correct
22 Correct 18 ms 19076 KB Output is correct
23 Correct 13 ms 19028 KB Output is correct
24 Correct 12 ms 19028 KB Output is correct
25 Correct 20 ms 19108 KB Output is correct
26 Correct 17 ms 19028 KB Output is correct
27 Correct 10 ms 19096 KB Output is correct
28 Correct 10 ms 19096 KB Output is correct
29 Correct 13 ms 19008 KB Output is correct
30 Correct 14 ms 19092 KB Output is correct
31 Correct 14 ms 19040 KB Output is correct
32 Correct 11 ms 19028 KB Output is correct
33 Correct 13 ms 19028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 18992 KB Output is correct
2 Correct 11 ms 19104 KB Output is correct
3 Correct 11 ms 19028 KB Output is correct
4 Correct 14 ms 19092 KB Output is correct
5 Correct 13 ms 19004 KB Output is correct
6 Correct 11 ms 19028 KB Output is correct
7 Correct 16 ms 19104 KB Output is correct
8 Correct 15 ms 19148 KB Output is correct
9 Correct 17 ms 19128 KB Output is correct
10 Correct 10 ms 19028 KB Output is correct
11 Correct 12 ms 19028 KB Output is correct
12 Correct 11 ms 19092 KB Output is correct
13 Correct 11 ms 19096 KB Output is correct
14 Correct 15 ms 19092 KB Output is correct
15 Correct 11 ms 18988 KB Output is correct
16 Correct 13 ms 19028 KB Output is correct
17 Correct 12 ms 19100 KB Output is correct
18 Correct 16 ms 19040 KB Output is correct
19 Correct 14 ms 19096 KB Output is correct
20 Correct 11 ms 19028 KB Output is correct
21 Correct 19 ms 19072 KB Output is correct
22 Correct 18 ms 19076 KB Output is correct
23 Correct 13 ms 19028 KB Output is correct
24 Correct 12 ms 19028 KB Output is correct
25 Correct 20 ms 19108 KB Output is correct
26 Correct 17 ms 19028 KB Output is correct
27 Correct 10 ms 19096 KB Output is correct
28 Correct 10 ms 19096 KB Output is correct
29 Correct 13 ms 19008 KB Output is correct
30 Correct 14 ms 19092 KB Output is correct
31 Correct 14 ms 19040 KB Output is correct
32 Correct 11 ms 19028 KB Output is correct
33 Correct 13 ms 19028 KB Output is correct
34 Correct 27 ms 19156 KB Output is correct
35 Correct 550 ms 24412 KB Output is correct
36 Correct 613 ms 26492 KB Output is correct
37 Correct 623 ms 26516 KB Output is correct
38 Correct 579 ms 26184 KB Output is correct
39 Correct 21 ms 19156 KB Output is correct
40 Correct 18 ms 19276 KB Output is correct
41 Correct 18 ms 19276 KB Output is correct
42 Correct 23 ms 19148 KB Output is correct
43 Correct 27 ms 19192 KB Output is correct
44 Correct 20 ms 19284 KB Output is correct
45 Correct 21 ms 19104 KB Output is correct
46 Correct 18 ms 19184 KB Output is correct
47 Correct 22 ms 19236 KB Output is correct
48 Correct 22 ms 19268 KB Output is correct
49 Correct 53 ms 19916 KB Output is correct
50 Correct 638 ms 26620 KB Output is correct
51 Correct 34 ms 19504 KB Output is correct
52 Correct 657 ms 25224 KB Output is correct
53 Correct 36 ms 19788 KB Output is correct
54 Correct 596 ms 25896 KB Output is correct
55 Correct 30 ms 19604 KB Output is correct
56 Correct 22 ms 19532 KB Output is correct
57 Correct 33 ms 19868 KB Output is correct
58 Correct 23 ms 19928 KB Output is correct
59 Correct 38 ms 19180 KB Output is correct
60 Correct 643 ms 23516 KB Output is correct
61 Correct 26 ms 19332 KB Output is correct
62 Correct 655 ms 26092 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 18992 KB Output is correct
2 Correct 11 ms 19104 KB Output is correct
3 Correct 11 ms 19028 KB Output is correct
4 Correct 14 ms 19092 KB Output is correct
5 Correct 13 ms 19004 KB Output is correct
6 Correct 11 ms 19028 KB Output is correct
7 Correct 16 ms 19104 KB Output is correct
8 Correct 15 ms 19148 KB Output is correct
9 Correct 17 ms 19128 KB Output is correct
10 Correct 10 ms 19028 KB Output is correct
11 Correct 12 ms 19028 KB Output is correct
12 Correct 11 ms 19092 KB Output is correct
13 Correct 11 ms 19096 KB Output is correct
14 Correct 15 ms 19092 KB Output is correct
15 Correct 11 ms 18988 KB Output is correct
16 Correct 13 ms 19028 KB Output is correct
17 Correct 12 ms 19100 KB Output is correct
18 Correct 16 ms 19040 KB Output is correct
19 Correct 14 ms 19096 KB Output is correct
20 Correct 11 ms 19028 KB Output is correct
21 Correct 19 ms 19072 KB Output is correct
22 Correct 18 ms 19076 KB Output is correct
23 Correct 13 ms 19028 KB Output is correct
24 Correct 12 ms 19028 KB Output is correct
25 Correct 20 ms 19108 KB Output is correct
26 Correct 17 ms 19028 KB Output is correct
27 Correct 10 ms 19096 KB Output is correct
28 Correct 10 ms 19096 KB Output is correct
29 Correct 13 ms 19008 KB Output is correct
30 Correct 14 ms 19092 KB Output is correct
31 Correct 14 ms 19040 KB Output is correct
32 Correct 11 ms 19028 KB Output is correct
33 Correct 13 ms 19028 KB Output is correct
34 Correct 27 ms 19156 KB Output is correct
35 Correct 550 ms 24412 KB Output is correct
36 Correct 613 ms 26492 KB Output is correct
37 Correct 623 ms 26516 KB Output is correct
38 Correct 579 ms 26184 KB Output is correct
39 Correct 21 ms 19156 KB Output is correct
40 Correct 18 ms 19276 KB Output is correct
41 Correct 18 ms 19276 KB Output is correct
42 Correct 23 ms 19148 KB Output is correct
43 Correct 27 ms 19192 KB Output is correct
44 Correct 20 ms 19284 KB Output is correct
45 Correct 21 ms 19104 KB Output is correct
46 Correct 18 ms 19184 KB Output is correct
47 Correct 22 ms 19236 KB Output is correct
48 Correct 22 ms 19268 KB Output is correct
49 Correct 53 ms 19916 KB Output is correct
50 Correct 638 ms 26620 KB Output is correct
51 Correct 34 ms 19504 KB Output is correct
52 Correct 657 ms 25224 KB Output is correct
53 Correct 36 ms 19788 KB Output is correct
54 Correct 596 ms 25896 KB Output is correct
55 Correct 30 ms 19604 KB Output is correct
56 Correct 22 ms 19532 KB Output is correct
57 Correct 33 ms 19868 KB Output is correct
58 Correct 23 ms 19928 KB Output is correct
59 Correct 38 ms 19180 KB Output is correct
60 Correct 643 ms 23516 KB Output is correct
61 Correct 26 ms 19332 KB Output is correct
62 Correct 655 ms 26092 KB Output is correct
63 Correct 988 ms 57480 KB Output is correct
64 Correct 900 ms 57452 KB Output is correct
65 Correct 852 ms 57536 KB Output is correct
66 Correct 405 ms 24784 KB Output is correct
67 Correct 504 ms 29436 KB Output is correct
68 Correct 506 ms 24876 KB Output is correct
69 Correct 818 ms 30276 KB Output is correct
70 Correct 423 ms 24728 KB Output is correct
71 Correct 488 ms 24776 KB Output is correct
72 Correct 569 ms 29636 KB Output is correct
73 Correct 621 ms 29616 KB Output is correct
74 Correct 1305 ms 38828 KB Output is correct
75 Correct 1002 ms 34140 KB Output is correct
76 Correct 1046 ms 37888 KB Output is correct
77 Correct 979 ms 37556 KB Output is correct
78 Correct 404 ms 29256 KB Output is correct
79 Correct 695 ms 31324 KB Output is correct
80 Correct 425 ms 28324 KB Output is correct
81 Correct 789 ms 30004 KB Output is correct
82 Correct 837 ms 44908 KB Output is correct
83 Correct 799 ms 44708 KB Output is correct
84 Correct 777 ms 62032 KB Output is correct
85 Correct 849 ms 62016 KB Output is correct
86 Correct 419 ms 22624 KB Output is correct
87 Correct 600 ms 23600 KB Output is correct
88 Correct 505 ms 28236 KB Output is correct
89 Correct 960 ms 35612 KB Output is correct