Submission #1099405

# Submission time Handle Problem Language Result Execution time Memory
1099405 2024-10-11T09:17:55 Z Neco_arc Making Friends on Joitter is Fun (JOI20_joitter2) C++17
100 / 100
636 ms 81380 KB
#include <bits/stdc++.h>

#define ll long long
#define ull unsigned long long
#define all(x) x.begin(), x.end()
#define Neco "Joitter"
#define resp(x) sort(all(x)), x.resize(unique(all(x)) - x.begin())
#define getbit(x,i) ((x >> i)&1)
#define _left id * 2, l, mid
#define _right id * 2 + 1, mid + 1, r
#define cntbit(x) __builtin_popcountll(x)
#define fi(i, a, b) for(int i = a; i <= b; i++)
#define fid(i, a, b) for(int i = a; i >= b; i--)
#define maxn (int) 2e5 + 7

using namespace std;

const ll mod = 1e9 + 7; //972663749
const ll base = 911382323;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll GetRandom(ll l, ll r) {
    return uniform_int_distribution<ll> (l, r)(rng);
}

int n, q; ll ans;
int lab[maxn];
vector<pair<int, int>> Wait;
set<int> In[maxn], Out[maxn], C[maxn];

int get_rt(int u) { return lab[u] < 0 ? u : lab[u] = get_rt(lab[u]); }
int size(int u) { return C[u].size() + In[u].size() + Out[u].size(); }

void Add(int u, int v) {
    u = get_rt(u), v = get_rt(v);
    In[v].insert(u), Out[u].insert(v);
    if(Out[v].count(u)) Wait.push_back({u, v});
}

void uni(int u, int v) {
    u = get_rt(u), v = get_rt(v); if(u == v) return;
    if(size(u) < size(v)) swap(u, v);

    ans += 1ll * (-lab[u] * C[v].size()) + 1ll * (-lab[v] * C[u].size());
    lab[u] += lab[v], lab[v] = u;

    for(int x : C[v]) {
        if(C[u].count(x)) ans -= -lab[u];
        else C[u].insert(x);
    }

    for(int x : In[v]) {
        Out[x].erase(v), Add(x, u);
    }
    for(int x : Out[v]) {
        In[x].erase(v), Add(u, x);
    }

    In[v].clear(), Out[v].clear(), C[v].clear();
}

void solve()
{

    cin >> n >> q;
    fi(i, 1, n) lab[i] = -1, C[i] = {i};

    fi(i, 1, q) {
        int u, v; cin >> u >> v;
        v = get_rt(v);

        if(get_rt(u) != v && !C[v].count(u)) {
            C[v].insert(u), ans += -lab[v];
            u = get_rt(u);

            Add(u, v);
            while(!Wait.empty()) {
                int x, y; tie(x, y) = Wait.back();
                Wait.pop_back();
                uni(x, y);
            }
        }

        cout << ans << '\n';
    }

}


int main()
{

    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    if(fopen(Neco".inp", "r")) {
        freopen(Neco".inp", "r", stdin);
        freopen(Neco".out", "w", stdout);
    }

    int nTest = 1;
//    cin >> nTest;

    while(nTest--)
    {
        solve();
    }


    return 0;
}

Compilation message

joitter2.cpp: In function 'int main()':
joitter2.cpp:98:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   98 |         freopen(Neco".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
joitter2.cpp:99:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   99 |         freopen(Neco".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 13 ms 28508 KB Output is correct
2 Correct 13 ms 28584 KB Output is correct
3 Correct 14 ms 28508 KB Output is correct
4 Correct 12 ms 28508 KB Output is correct
5 Correct 12 ms 28508 KB Output is correct
6 Correct 12 ms 28560 KB Output is correct
7 Correct 14 ms 28508 KB Output is correct
8 Correct 13 ms 28508 KB Output is correct
9 Correct 12 ms 28620 KB Output is correct
10 Correct 13 ms 28508 KB Output is correct
11 Correct 12 ms 28660 KB Output is correct
12 Correct 13 ms 28608 KB Output is correct
13 Correct 13 ms 28412 KB Output is correct
14 Correct 13 ms 28508 KB Output is correct
15 Correct 14 ms 28500 KB Output is correct
16 Correct 13 ms 28504 KB Output is correct
17 Correct 14 ms 28508 KB Output is correct
18 Correct 13 ms 28468 KB Output is correct
19 Correct 12 ms 28508 KB Output is correct
20 Correct 11 ms 28508 KB Output is correct
21 Correct 12 ms 28504 KB Output is correct
22 Correct 11 ms 28488 KB Output is correct
23 Correct 12 ms 28508 KB Output is correct
24 Correct 12 ms 28504 KB Output is correct
25 Correct 14 ms 28508 KB Output is correct
26 Correct 14 ms 28504 KB Output is correct
27 Correct 13 ms 28508 KB Output is correct
28 Correct 14 ms 28508 KB Output is correct
29 Correct 13 ms 28504 KB Output is correct
30 Correct 13 ms 28508 KB Output is correct
31 Correct 14 ms 28580 KB Output is correct
32 Correct 14 ms 28644 KB Output is correct
33 Correct 12 ms 28508 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 28508 KB Output is correct
2 Correct 13 ms 28584 KB Output is correct
3 Correct 14 ms 28508 KB Output is correct
4 Correct 12 ms 28508 KB Output is correct
5 Correct 12 ms 28508 KB Output is correct
6 Correct 12 ms 28560 KB Output is correct
7 Correct 14 ms 28508 KB Output is correct
8 Correct 13 ms 28508 KB Output is correct
9 Correct 12 ms 28620 KB Output is correct
10 Correct 13 ms 28508 KB Output is correct
11 Correct 12 ms 28660 KB Output is correct
12 Correct 13 ms 28608 KB Output is correct
13 Correct 13 ms 28412 KB Output is correct
14 Correct 13 ms 28508 KB Output is correct
15 Correct 14 ms 28500 KB Output is correct
16 Correct 13 ms 28504 KB Output is correct
17 Correct 14 ms 28508 KB Output is correct
18 Correct 13 ms 28468 KB Output is correct
19 Correct 12 ms 28508 KB Output is correct
20 Correct 11 ms 28508 KB Output is correct
21 Correct 12 ms 28504 KB Output is correct
22 Correct 11 ms 28488 KB Output is correct
23 Correct 12 ms 28508 KB Output is correct
24 Correct 12 ms 28504 KB Output is correct
25 Correct 14 ms 28508 KB Output is correct
26 Correct 14 ms 28504 KB Output is correct
27 Correct 13 ms 28508 KB Output is correct
28 Correct 14 ms 28508 KB Output is correct
29 Correct 13 ms 28504 KB Output is correct
30 Correct 13 ms 28508 KB Output is correct
31 Correct 14 ms 28580 KB Output is correct
32 Correct 14 ms 28644 KB Output is correct
33 Correct 12 ms 28508 KB Output is correct
34 Correct 15 ms 28796 KB Output is correct
35 Correct 75 ms 34376 KB Output is correct
36 Correct 84 ms 37072 KB Output is correct
37 Correct 83 ms 37328 KB Output is correct
38 Correct 82 ms 36848 KB Output is correct
39 Correct 15 ms 28764 KB Output is correct
40 Correct 15 ms 28936 KB Output is correct
41 Correct 14 ms 28760 KB Output is correct
42 Correct 13 ms 28764 KB Output is correct
43 Correct 14 ms 28848 KB Output is correct
44 Correct 15 ms 29020 KB Output is correct
45 Correct 13 ms 28764 KB Output is correct
46 Correct 13 ms 28764 KB Output is correct
47 Correct 16 ms 28848 KB Output is correct
48 Correct 16 ms 28764 KB Output is correct
49 Correct 21 ms 29532 KB Output is correct
50 Correct 83 ms 37336 KB Output is correct
51 Correct 16 ms 29020 KB Output is correct
52 Correct 67 ms 35792 KB Output is correct
53 Correct 19 ms 29528 KB Output is correct
54 Correct 78 ms 36440 KB Output is correct
55 Correct 17 ms 29276 KB Output is correct
56 Correct 17 ms 29276 KB Output is correct
57 Correct 18 ms 29272 KB Output is correct
58 Correct 15 ms 29276 KB Output is correct
59 Correct 14 ms 28764 KB Output is correct
60 Correct 58 ms 33812 KB Output is correct
61 Correct 16 ms 29020 KB Output is correct
62 Correct 78 ms 36640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 28508 KB Output is correct
2 Correct 13 ms 28584 KB Output is correct
3 Correct 14 ms 28508 KB Output is correct
4 Correct 12 ms 28508 KB Output is correct
5 Correct 12 ms 28508 KB Output is correct
6 Correct 12 ms 28560 KB Output is correct
7 Correct 14 ms 28508 KB Output is correct
8 Correct 13 ms 28508 KB Output is correct
9 Correct 12 ms 28620 KB Output is correct
10 Correct 13 ms 28508 KB Output is correct
11 Correct 12 ms 28660 KB Output is correct
12 Correct 13 ms 28608 KB Output is correct
13 Correct 13 ms 28412 KB Output is correct
14 Correct 13 ms 28508 KB Output is correct
15 Correct 14 ms 28500 KB Output is correct
16 Correct 13 ms 28504 KB Output is correct
17 Correct 14 ms 28508 KB Output is correct
18 Correct 13 ms 28468 KB Output is correct
19 Correct 12 ms 28508 KB Output is correct
20 Correct 11 ms 28508 KB Output is correct
21 Correct 12 ms 28504 KB Output is correct
22 Correct 11 ms 28488 KB Output is correct
23 Correct 12 ms 28508 KB Output is correct
24 Correct 12 ms 28504 KB Output is correct
25 Correct 14 ms 28508 KB Output is correct
26 Correct 14 ms 28504 KB Output is correct
27 Correct 13 ms 28508 KB Output is correct
28 Correct 14 ms 28508 KB Output is correct
29 Correct 13 ms 28504 KB Output is correct
30 Correct 13 ms 28508 KB Output is correct
31 Correct 14 ms 28580 KB Output is correct
32 Correct 14 ms 28644 KB Output is correct
33 Correct 12 ms 28508 KB Output is correct
34 Correct 15 ms 28796 KB Output is correct
35 Correct 75 ms 34376 KB Output is correct
36 Correct 84 ms 37072 KB Output is correct
37 Correct 83 ms 37328 KB Output is correct
38 Correct 82 ms 36848 KB Output is correct
39 Correct 15 ms 28764 KB Output is correct
40 Correct 15 ms 28936 KB Output is correct
41 Correct 14 ms 28760 KB Output is correct
42 Correct 13 ms 28764 KB Output is correct
43 Correct 14 ms 28848 KB Output is correct
44 Correct 15 ms 29020 KB Output is correct
45 Correct 13 ms 28764 KB Output is correct
46 Correct 13 ms 28764 KB Output is correct
47 Correct 16 ms 28848 KB Output is correct
48 Correct 16 ms 28764 KB Output is correct
49 Correct 21 ms 29532 KB Output is correct
50 Correct 83 ms 37336 KB Output is correct
51 Correct 16 ms 29020 KB Output is correct
52 Correct 67 ms 35792 KB Output is correct
53 Correct 19 ms 29528 KB Output is correct
54 Correct 78 ms 36440 KB Output is correct
55 Correct 17 ms 29276 KB Output is correct
56 Correct 17 ms 29276 KB Output is correct
57 Correct 18 ms 29272 KB Output is correct
58 Correct 15 ms 29276 KB Output is correct
59 Correct 14 ms 28764 KB Output is correct
60 Correct 58 ms 33812 KB Output is correct
61 Correct 16 ms 29020 KB Output is correct
62 Correct 78 ms 36640 KB Output is correct
63 Correct 341 ms 81380 KB Output is correct
64 Correct 354 ms 81240 KB Output is correct
65 Correct 333 ms 81232 KB Output is correct
66 Correct 165 ms 37968 KB Output is correct
67 Correct 293 ms 46164 KB Output is correct
68 Correct 152 ms 37972 KB Output is correct
69 Correct 291 ms 45136 KB Output is correct
70 Correct 172 ms 37952 KB Output is correct
71 Correct 164 ms 38004 KB Output is correct
72 Correct 293 ms 45904 KB Output is correct
73 Correct 286 ms 46004 KB Output is correct
74 Correct 636 ms 58320 KB Output is correct
75 Correct 393 ms 50712 KB Output is correct
76 Correct 475 ms 56912 KB Output is correct
77 Correct 543 ms 57172 KB Output is correct
78 Correct 174 ms 43860 KB Output is correct
79 Correct 282 ms 47184 KB Output is correct
80 Correct 152 ms 43856 KB Output is correct
81 Correct 247 ms 47184 KB Output is correct
82 Correct 534 ms 65312 KB Output is correct
83 Correct 461 ms 65364 KB Output is correct
84 Correct 470 ms 66504 KB Output is correct
85 Correct 485 ms 66504 KB Output is correct
86 Correct 250 ms 41812 KB Output is correct
87 Correct 317 ms 44112 KB Output is correct
88 Correct 301 ms 45904 KB Output is correct
89 Correct 457 ms 56656 KB Output is correct