Submission #961818

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

using namespace std;
typedef long long ll;

const int maxn = 2010;

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 1 ms 860 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 4 ms 604 KB Output is correct
8 Correct 4 ms 604 KB Output is correct
9 Correct 5 ms 760 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 1 ms 600 KB Output is correct
12 Correct 1 ms 600 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 1 ms 604 KB Output is correct
17 Correct 1 ms 856 KB Output is correct
18 Correct 1 ms 600 KB Output is correct
19 Correct 1 ms 604 KB Output is correct
20 Correct 1 ms 604 KB Output is correct
21 Correct 4 ms 604 KB Output is correct
22 Correct 1 ms 604 KB Output is correct
23 Correct 2 ms 604 KB Output is correct
24 Correct 2 ms 1012 KB Output is correct
25 Correct 4 ms 604 KB Output is correct
26 Correct 1 ms 600 KB Output is correct
27 Correct 1 ms 604 KB Output is correct
28 Correct 1 ms 604 KB Output is correct
29 Correct 1 ms 604 KB Output is correct
30 Correct 1 ms 728 KB Output is correct
31 Correct 4 ms 600 KB Output is correct
32 Correct 1 ms 604 KB Output is correct
33 Correct 3 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 860 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 4 ms 604 KB Output is correct
8 Correct 4 ms 604 KB Output is correct
9 Correct 5 ms 760 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 1 ms 600 KB Output is correct
12 Correct 1 ms 600 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 1 ms 604 KB Output is correct
17 Correct 1 ms 856 KB Output is correct
18 Correct 1 ms 600 KB Output is correct
19 Correct 1 ms 604 KB Output is correct
20 Correct 1 ms 604 KB Output is correct
21 Correct 4 ms 604 KB Output is correct
22 Correct 1 ms 604 KB Output is correct
23 Correct 2 ms 604 KB Output is correct
24 Correct 2 ms 1012 KB Output is correct
25 Correct 4 ms 604 KB Output is correct
26 Correct 1 ms 600 KB Output is correct
27 Correct 1 ms 604 KB Output is correct
28 Correct 1 ms 604 KB Output is correct
29 Correct 1 ms 604 KB Output is correct
30 Correct 1 ms 728 KB Output is correct
31 Correct 4 ms 600 KB Output is correct
32 Correct 1 ms 604 KB Output is correct
33 Correct 3 ms 604 KB Output is correct
34 Correct 15 ms 860 KB Output is correct
35 Correct 461 ms 6428 KB Output is correct
36 Correct 504 ms 9316 KB Output is correct
37 Correct 508 ms 9436 KB Output is correct
38 Correct 490 ms 8996 KB Output is correct
39 Correct 13 ms 1024 KB Output is correct
40 Correct 9 ms 1372 KB Output is correct
41 Correct 10 ms 1372 KB Output is correct
42 Correct 9 ms 1000 KB Output is correct
43 Correct 9 ms 860 KB Output is correct
44 Correct 9 ms 1040 KB Output is correct
45 Correct 9 ms 860 KB Output is correct
46 Correct 8 ms 860 KB Output is correct
47 Correct 9 ms 1272 KB Output is correct
48 Correct 9 ms 1116 KB Output is correct
49 Correct 22 ms 1884 KB Output is correct
50 Correct 504 ms 9476 KB Output is correct
51 Correct 20 ms 1372 KB Output is correct
52 Correct 487 ms 7932 KB Output is correct
53 Correct 27 ms 1884 KB Output is correct
54 Correct 490 ms 8404 KB Output is correct
55 Correct 15 ms 1428 KB Output is correct
56 Correct 10 ms 1372 KB Output is correct
57 Correct 10 ms 1372 KB Output is correct
58 Correct 10 ms 1440 KB Output is correct
59 Correct 10 ms 1372 KB Output is correct
60 Correct 470 ms 6288 KB Output is correct
61 Correct 14 ms 1372 KB Output is correct
62 Correct 489 ms 8824 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 860 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 4 ms 604 KB Output is correct
8 Correct 4 ms 604 KB Output is correct
9 Correct 5 ms 760 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 1 ms 600 KB Output is correct
12 Correct 1 ms 600 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 1 ms 604 KB Output is correct
17 Correct 1 ms 856 KB Output is correct
18 Correct 1 ms 600 KB Output is correct
19 Correct 1 ms 604 KB Output is correct
20 Correct 1 ms 604 KB Output is correct
21 Correct 4 ms 604 KB Output is correct
22 Correct 1 ms 604 KB Output is correct
23 Correct 2 ms 604 KB Output is correct
24 Correct 2 ms 1012 KB Output is correct
25 Correct 4 ms 604 KB Output is correct
26 Correct 1 ms 600 KB Output is correct
27 Correct 1 ms 604 KB Output is correct
28 Correct 1 ms 604 KB Output is correct
29 Correct 1 ms 604 KB Output is correct
30 Correct 1 ms 728 KB Output is correct
31 Correct 4 ms 600 KB Output is correct
32 Correct 1 ms 604 KB Output is correct
33 Correct 3 ms 604 KB Output is correct
34 Correct 15 ms 860 KB Output is correct
35 Correct 461 ms 6428 KB Output is correct
36 Correct 504 ms 9316 KB Output is correct
37 Correct 508 ms 9436 KB Output is correct
38 Correct 490 ms 8996 KB Output is correct
39 Correct 13 ms 1024 KB Output is correct
40 Correct 9 ms 1372 KB Output is correct
41 Correct 10 ms 1372 KB Output is correct
42 Correct 9 ms 1000 KB Output is correct
43 Correct 9 ms 860 KB Output is correct
44 Correct 9 ms 1040 KB Output is correct
45 Correct 9 ms 860 KB Output is correct
46 Correct 8 ms 860 KB Output is correct
47 Correct 9 ms 1272 KB Output is correct
48 Correct 9 ms 1116 KB Output is correct
49 Correct 22 ms 1884 KB Output is correct
50 Correct 504 ms 9476 KB Output is correct
51 Correct 20 ms 1372 KB Output is correct
52 Correct 487 ms 7932 KB Output is correct
53 Correct 27 ms 1884 KB Output is correct
54 Correct 490 ms 8404 KB Output is correct
55 Correct 15 ms 1428 KB Output is correct
56 Correct 10 ms 1372 KB Output is correct
57 Correct 10 ms 1372 KB Output is correct
58 Correct 10 ms 1440 KB Output is correct
59 Correct 10 ms 1372 KB Output is correct
60 Correct 470 ms 6288 KB Output is correct
61 Correct 14 ms 1372 KB Output is correct
62 Correct 489 ms 8824 KB Output is correct
63 Runtime error 1 ms 1116 KB Execution killed with signal 11
64 Halted 0 ms 0 KB -