Submission #961820

# Submission time Handle Problem Language Result Execution time Memory
961820 2024-04-12T13:57:51 Z danikoynov Making Friends on Joitter is Fun (JOI20_joitter2) C++14
100 / 100
1423 ms 83252 KB
#include <bits/stdc++.h>
#define endl '\n'

using namespace std;
typedef long long ll;

const int maxn = 2e5 + 10;

int n, par[maxn], m;
int sz[maxn];

int find_leader(int v)
{
    if (par[v] == v)
        return v;
    return (par[v] = find_leader(par[v]));
}


set < int > rem;
set < int > adj[maxn];
set < int > rev_adj[maxn], child[maxn];
ll ans;

queue < pair < int, int > > to_merge;

void insert_weak_connection(int a, int b)
{
     adj[a].insert(b);
     rev_adj[b].insert(a);

     if (adj[b].count(a))
     to_merge.push({a, b});
}
void unite(int v, int u)
{
     if (v == u)
          return;

     if (child[v].size() < child[u].size())
          swap(v, u);

     ans = ans + (ll)(child[v].size()) * sz[u] + (ll)(child[u].size()) * sz[v];


     par[u] = v;
     sz[v] += sz[u];

     for (int d : child[u])
     {
          if (child[v].count(d)) ans -= sz[v];
          else
               child[v].insert(d);
     }

     adj[v].erase(u);
     adj[u].erase(v);
     rev_adj[v].erase(u);
     rev_adj[u].erase(v);

     for (int d : adj[u])
     {
          rev_adj[d].erase(u);
          insert_weak_connection(v, d);
     }

     for (int d : rev_adj[u])
     {
          adj[d].erase(u);
          insert_weak_connection(d, v);
     }



}
void solve()
{
    cin >> n >> m;
    for (int i = 1; i <= n; i ++)
    {
        par[i] = i;
        sz[i] = 1;
          child[i].insert(i);
    }

    for (int i = 1; i <= m; i ++)
    {
        int v, u;
        cin >> v >> u;
        u = find_leader(u);
        if (find_leader(v) != u && !child[u].count(v))
        {
             child[u].insert(v);
             ans += sz[u];

             v = find_leader(v);
             insert_weak_connection(v, u);

             while(to_merge.size())
             {
                    pair < int, int > cur = to_merge.front();
                    to_merge.pop();
                    unite(find_leader(cur.first), find_leader(cur.second));
             }
        }

        cout << ans << endl;

    }
}

int main()
{
    solve();
    return 0;
}
/**
5 20
4 2
1 5
2 3
2 5
3 2
3 1
4 5
1 2

10 18
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 9
4 10
2 3
5 2
1 3
4 6
2 6
8 10
3 10
4 5
3 6
9 4
5 3
9 6
6 2
7 10
3 4
5 9
10 9
7 4


*/
# Verdict Execution time Memory Grader output
1 Correct 8 ms 29276 KB Output is correct
2 Correct 6 ms 29276 KB Output is correct
3 Correct 6 ms 29276 KB Output is correct
4 Correct 8 ms 29276 KB Output is correct
5 Correct 6 ms 29276 KB Output is correct
6 Correct 7 ms 29276 KB Output is correct
7 Correct 11 ms 29276 KB Output is correct
8 Correct 10 ms 29276 KB Output is correct
9 Correct 11 ms 29132 KB Output is correct
10 Correct 8 ms 29276 KB Output is correct
11 Correct 7 ms 29276 KB Output is correct
12 Correct 7 ms 29276 KB Output is correct
13 Correct 7 ms 29276 KB Output is correct
14 Correct 8 ms 29132 KB Output is correct
15 Correct 8 ms 29276 KB Output is correct
16 Correct 7 ms 29276 KB Output is correct
17 Correct 7 ms 29124 KB Output is correct
18 Correct 9 ms 29276 KB Output is correct
19 Correct 7 ms 29124 KB Output is correct
20 Correct 8 ms 29276 KB Output is correct
21 Correct 11 ms 29276 KB Output is correct
22 Correct 7 ms 29276 KB Output is correct
23 Correct 9 ms 29276 KB Output is correct
24 Correct 8 ms 29276 KB Output is correct
25 Correct 11 ms 29356 KB Output is correct
26 Correct 6 ms 29320 KB Output is correct
27 Correct 6 ms 29276 KB Output is correct
28 Correct 7 ms 29124 KB Output is correct
29 Correct 7 ms 29284 KB Output is correct
30 Correct 7 ms 29124 KB Output is correct
31 Correct 9 ms 29356 KB Output is correct
32 Correct 7 ms 29276 KB Output is correct
33 Correct 11 ms 29368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 29276 KB Output is correct
2 Correct 6 ms 29276 KB Output is correct
3 Correct 6 ms 29276 KB Output is correct
4 Correct 8 ms 29276 KB Output is correct
5 Correct 6 ms 29276 KB Output is correct
6 Correct 7 ms 29276 KB Output is correct
7 Correct 11 ms 29276 KB Output is correct
8 Correct 10 ms 29276 KB Output is correct
9 Correct 11 ms 29132 KB Output is correct
10 Correct 8 ms 29276 KB Output is correct
11 Correct 7 ms 29276 KB Output is correct
12 Correct 7 ms 29276 KB Output is correct
13 Correct 7 ms 29276 KB Output is correct
14 Correct 8 ms 29132 KB Output is correct
15 Correct 8 ms 29276 KB Output is correct
16 Correct 7 ms 29276 KB Output is correct
17 Correct 7 ms 29124 KB Output is correct
18 Correct 9 ms 29276 KB Output is correct
19 Correct 7 ms 29124 KB Output is correct
20 Correct 8 ms 29276 KB Output is correct
21 Correct 11 ms 29276 KB Output is correct
22 Correct 7 ms 29276 KB Output is correct
23 Correct 9 ms 29276 KB Output is correct
24 Correct 8 ms 29276 KB Output is correct
25 Correct 11 ms 29356 KB Output is correct
26 Correct 6 ms 29320 KB Output is correct
27 Correct 6 ms 29276 KB Output is correct
28 Correct 7 ms 29124 KB Output is correct
29 Correct 7 ms 29284 KB Output is correct
30 Correct 7 ms 29124 KB Output is correct
31 Correct 9 ms 29356 KB Output is correct
32 Correct 7 ms 29276 KB Output is correct
33 Correct 11 ms 29368 KB Output is correct
34 Correct 22 ms 29280 KB Output is correct
35 Correct 481 ms 32668 KB Output is correct
36 Correct 526 ms 35184 KB Output is correct
37 Correct 514 ms 35460 KB Output is correct
38 Correct 506 ms 34840 KB Output is correct
39 Correct 15 ms 29544 KB Output is correct
40 Correct 17 ms 30032 KB Output is correct
41 Correct 20 ms 29788 KB Output is correct
42 Correct 14 ms 29560 KB Output is correct
43 Correct 17 ms 29532 KB Output is correct
44 Correct 18 ms 29676 KB Output is correct
45 Correct 14 ms 29532 KB Output is correct
46 Correct 16 ms 29532 KB Output is correct
47 Correct 17 ms 29632 KB Output is correct
48 Correct 17 ms 29780 KB Output is correct
49 Correct 31 ms 30300 KB Output is correct
50 Correct 517 ms 35500 KB Output is correct
51 Correct 29 ms 29788 KB Output is correct
52 Correct 502 ms 33728 KB Output is correct
53 Correct 29 ms 30208 KB Output is correct
54 Correct 532 ms 34404 KB Output is correct
55 Correct 18 ms 30012 KB Output is correct
56 Correct 18 ms 30040 KB Output is correct
57 Correct 18 ms 29948 KB Output is correct
58 Correct 20 ms 30248 KB Output is correct
59 Correct 18 ms 30044 KB Output is correct
60 Correct 502 ms 32360 KB Output is correct
61 Correct 19 ms 29788 KB Output is correct
62 Correct 509 ms 34924 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 29276 KB Output is correct
2 Correct 6 ms 29276 KB Output is correct
3 Correct 6 ms 29276 KB Output is correct
4 Correct 8 ms 29276 KB Output is correct
5 Correct 6 ms 29276 KB Output is correct
6 Correct 7 ms 29276 KB Output is correct
7 Correct 11 ms 29276 KB Output is correct
8 Correct 10 ms 29276 KB Output is correct
9 Correct 11 ms 29132 KB Output is correct
10 Correct 8 ms 29276 KB Output is correct
11 Correct 7 ms 29276 KB Output is correct
12 Correct 7 ms 29276 KB Output is correct
13 Correct 7 ms 29276 KB Output is correct
14 Correct 8 ms 29132 KB Output is correct
15 Correct 8 ms 29276 KB Output is correct
16 Correct 7 ms 29276 KB Output is correct
17 Correct 7 ms 29124 KB Output is correct
18 Correct 9 ms 29276 KB Output is correct
19 Correct 7 ms 29124 KB Output is correct
20 Correct 8 ms 29276 KB Output is correct
21 Correct 11 ms 29276 KB Output is correct
22 Correct 7 ms 29276 KB Output is correct
23 Correct 9 ms 29276 KB Output is correct
24 Correct 8 ms 29276 KB Output is correct
25 Correct 11 ms 29356 KB Output is correct
26 Correct 6 ms 29320 KB Output is correct
27 Correct 6 ms 29276 KB Output is correct
28 Correct 7 ms 29124 KB Output is correct
29 Correct 7 ms 29284 KB Output is correct
30 Correct 7 ms 29124 KB Output is correct
31 Correct 9 ms 29356 KB Output is correct
32 Correct 7 ms 29276 KB Output is correct
33 Correct 11 ms 29368 KB Output is correct
34 Correct 22 ms 29280 KB Output is correct
35 Correct 481 ms 32668 KB Output is correct
36 Correct 526 ms 35184 KB Output is correct
37 Correct 514 ms 35460 KB Output is correct
38 Correct 506 ms 34840 KB Output is correct
39 Correct 15 ms 29544 KB Output is correct
40 Correct 17 ms 30032 KB Output is correct
41 Correct 20 ms 29788 KB Output is correct
42 Correct 14 ms 29560 KB Output is correct
43 Correct 17 ms 29532 KB Output is correct
44 Correct 18 ms 29676 KB Output is correct
45 Correct 14 ms 29532 KB Output is correct
46 Correct 16 ms 29532 KB Output is correct
47 Correct 17 ms 29632 KB Output is correct
48 Correct 17 ms 29780 KB Output is correct
49 Correct 31 ms 30300 KB Output is correct
50 Correct 517 ms 35500 KB Output is correct
51 Correct 29 ms 29788 KB Output is correct
52 Correct 502 ms 33728 KB Output is correct
53 Correct 29 ms 30208 KB Output is correct
54 Correct 532 ms 34404 KB Output is correct
55 Correct 18 ms 30012 KB Output is correct
56 Correct 18 ms 30040 KB Output is correct
57 Correct 18 ms 29948 KB Output is correct
58 Correct 20 ms 30248 KB Output is correct
59 Correct 18 ms 30044 KB Output is correct
60 Correct 502 ms 32360 KB Output is correct
61 Correct 19 ms 29788 KB Output is correct
62 Correct 509 ms 34924 KB Output is correct
63 Correct 968 ms 82024 KB Output is correct
64 Correct 938 ms 82040 KB Output is correct
65 Correct 992 ms 82328 KB Output is correct
66 Correct 476 ms 48208 KB Output is correct
67 Correct 715 ms 75256 KB Output is correct
68 Correct 491 ms 48348 KB Output is correct
69 Correct 660 ms 49444 KB Output is correct
70 Correct 515 ms 48200 KB Output is correct
71 Correct 503 ms 48224 KB Output is correct
72 Correct 711 ms 59440 KB Output is correct
73 Correct 689 ms 61000 KB Output is correct
74 Correct 1423 ms 83176 KB Output is correct
75 Correct 948 ms 55880 KB Output is correct
76 Correct 1183 ms 68364 KB Output is correct
77 Correct 1267 ms 68376 KB Output is correct
78 Correct 543 ms 54392 KB Output is correct
79 Correct 951 ms 58456 KB Output is correct
80 Correct 463 ms 50572 KB Output is correct
81 Correct 871 ms 54140 KB Output is correct
82 Correct 1084 ms 68612 KB Output is correct
83 Correct 1155 ms 68776 KB Output is correct
84 Correct 962 ms 67464 KB Output is correct
85 Correct 948 ms 67456 KB Output is correct
86 Correct 612 ms 80864 KB Output is correct
87 Correct 781 ms 83252 KB Output is correct
88 Correct 706 ms 60384 KB Output is correct
89 Correct 1090 ms 66324 KB Output is correct