Submission #918888

# Submission time Handle Problem Language Result Execution time Memory
918888 2024-01-30T16:27:02 Z Tuanlinh123 Making Friends on Joitter is Fun (JOI20_joitter2) C++17
100 / 100
1342 ms 89332 KB
#include<bits/stdc++.h>
#define ll long long
#define pll pair<ll, ll>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ld long double
using namespace std;

const ll maxn=100005;
set <pll> out[maxn];
ll ans, pa[maxn], sz[maxn];
set <ll> in[maxn], comin[maxn], comout[maxn];

ll Find(ll i)
{
    if (pa[i]!=i)
        pa[i]=Find(pa[i]);
    return pa[i];
}

void addedge(ll a, ll b)
{
    ll Pa=Find(a), Pb=Find(b);
    if (Pa==Pb) return;
    if (comin[Pa].find(Pb)!=comin[Pa].end())
    {
        if (sz[Pa]<sz[Pb]) swap(Pa, Pb);
        vector <pll> upd;
        for (pll i:out[Pb])
        {
            in[i.se].erase(i.fi), ans-=sz[i.se];
            if (i.se!=Pa) upd.pb(i);
        }
        ans+=(ll)in[Pa].size()*sz[Pb];
        for (ll i:comin[Pb])
        {
            comin[Pa].insert(i);
            comout[i].erase(Pb);
            comout[i].insert(Pa);
        }
        for (ll i:comout[Pb])
        {
            comout[Pa].insert(i);
            comin[i].erase(Pb);
            comin[i].insert(Pa);
        }
        for (ll i:in[Pb])
        {
            ll f=Find(i); out[f].erase(mp(i, Pb));
            if (f==Pa) ans-=sz[Pb];
            else if (in[Pa].find(i)==in[Pa].end())
                upd.pb(mp(i, Pa)), ans-=sz[Pb];
            else ans-=sz[Pb];
        }
        ans-=sz[Pa]*(sz[Pa]-1)+sz[Pb]*(sz[Pb]-1);
        sz[Pa]+=sz[Pb], pa[Pb]=Pa;
        ans+=sz[Pa]*(sz[Pa]-1);
        for (pll i:upd) addedge(i.fi, i.se);
    }
    else if (in[Pb].find(a)==in[Pb].end())
    {
        comin[Pb].insert(Pa), in[Pb].insert(a); 
        comout[Pa].insert(Pb), out[Pa].insert({a, Pb});
        ans+=sz[Pb];
    }
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    ll n, m; cin >> n >> m;
    for (ll i=1; i<=n; i++)
        pa[i]=i, sz[i]=1;
    for (ll i=1; i<=m; i++)
    {
        ll u, v; cin >> u >> v;
        addedge(u, v), cout << ans << "\n";
    }
}
# Verdict Execution time Memory Grader output
1 Correct 6 ms 20568 KB Output is correct
2 Correct 6 ms 20768 KB Output is correct
3 Correct 6 ms 20568 KB Output is correct
4 Correct 6 ms 20568 KB Output is correct
5 Correct 5 ms 20568 KB Output is correct
6 Correct 6 ms 20572 KB Output is correct
7 Correct 6 ms 20824 KB Output is correct
8 Correct 6 ms 20824 KB Output is correct
9 Correct 6 ms 20828 KB Output is correct
10 Correct 6 ms 20568 KB Output is correct
11 Correct 6 ms 20568 KB Output is correct
12 Correct 6 ms 20824 KB Output is correct
13 Correct 5 ms 20568 KB Output is correct
14 Correct 7 ms 20568 KB Output is correct
15 Correct 5 ms 20568 KB Output is correct
16 Correct 5 ms 20568 KB Output is correct
17 Correct 5 ms 20568 KB Output is correct
18 Correct 6 ms 20568 KB Output is correct
19 Correct 7 ms 20568 KB Output is correct
20 Correct 6 ms 20828 KB Output is correct
21 Correct 6 ms 20824 KB Output is correct
22 Correct 5 ms 20568 KB Output is correct
23 Correct 6 ms 20828 KB Output is correct
24 Correct 6 ms 20824 KB Output is correct
25 Correct 6 ms 20824 KB Output is correct
26 Correct 5 ms 20596 KB Output is correct
27 Correct 6 ms 20824 KB Output is correct
28 Correct 5 ms 20824 KB Output is correct
29 Correct 6 ms 20824 KB Output is correct
30 Correct 6 ms 20980 KB Output is correct
31 Correct 7 ms 20824 KB Output is correct
32 Correct 5 ms 20568 KB Output is correct
33 Correct 6 ms 20828 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 20568 KB Output is correct
2 Correct 6 ms 20768 KB Output is correct
3 Correct 6 ms 20568 KB Output is correct
4 Correct 6 ms 20568 KB Output is correct
5 Correct 5 ms 20568 KB Output is correct
6 Correct 6 ms 20572 KB Output is correct
7 Correct 6 ms 20824 KB Output is correct
8 Correct 6 ms 20824 KB Output is correct
9 Correct 6 ms 20828 KB Output is correct
10 Correct 6 ms 20568 KB Output is correct
11 Correct 6 ms 20568 KB Output is correct
12 Correct 6 ms 20824 KB Output is correct
13 Correct 5 ms 20568 KB Output is correct
14 Correct 7 ms 20568 KB Output is correct
15 Correct 5 ms 20568 KB Output is correct
16 Correct 5 ms 20568 KB Output is correct
17 Correct 5 ms 20568 KB Output is correct
18 Correct 6 ms 20568 KB Output is correct
19 Correct 7 ms 20568 KB Output is correct
20 Correct 6 ms 20828 KB Output is correct
21 Correct 6 ms 20824 KB Output is correct
22 Correct 5 ms 20568 KB Output is correct
23 Correct 6 ms 20828 KB Output is correct
24 Correct 6 ms 20824 KB Output is correct
25 Correct 6 ms 20824 KB Output is correct
26 Correct 5 ms 20596 KB Output is correct
27 Correct 6 ms 20824 KB Output is correct
28 Correct 5 ms 20824 KB Output is correct
29 Correct 6 ms 20824 KB Output is correct
30 Correct 6 ms 20980 KB Output is correct
31 Correct 7 ms 20824 KB Output is correct
32 Correct 5 ms 20568 KB Output is correct
33 Correct 6 ms 20828 KB Output is correct
34 Correct 8 ms 20824 KB Output is correct
35 Correct 70 ms 25076 KB Output is correct
36 Correct 86 ms 28704 KB Output is correct
37 Correct 85 ms 29008 KB Output is correct
38 Correct 82 ms 28240 KB Output is correct
39 Correct 7 ms 20824 KB Output is correct
40 Correct 9 ms 21080 KB Output is correct
41 Correct 8 ms 21080 KB Output is correct
42 Correct 6 ms 20824 KB Output is correct
43 Correct 8 ms 21080 KB Output is correct
44 Correct 8 ms 21084 KB Output is correct
45 Correct 7 ms 20824 KB Output is correct
46 Correct 7 ms 20824 KB Output is correct
47 Correct 8 ms 21080 KB Output is correct
48 Correct 9 ms 21080 KB Output is correct
49 Correct 15 ms 22360 KB Output is correct
50 Correct 93 ms 29192 KB Output is correct
51 Correct 11 ms 21336 KB Output is correct
52 Correct 74 ms 26356 KB Output is correct
53 Correct 15 ms 22360 KB Output is correct
54 Correct 81 ms 27728 KB Output is correct
55 Correct 11 ms 21708 KB Output is correct
56 Correct 11 ms 21596 KB Output is correct
57 Correct 10 ms 21848 KB Output is correct
58 Correct 11 ms 21852 KB Output is correct
59 Correct 7 ms 21080 KB Output is correct
60 Correct 59 ms 23376 KB Output is correct
61 Correct 9 ms 21336 KB Output is correct
62 Correct 89 ms 28392 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 20568 KB Output is correct
2 Correct 6 ms 20768 KB Output is correct
3 Correct 6 ms 20568 KB Output is correct
4 Correct 6 ms 20568 KB Output is correct
5 Correct 5 ms 20568 KB Output is correct
6 Correct 6 ms 20572 KB Output is correct
7 Correct 6 ms 20824 KB Output is correct
8 Correct 6 ms 20824 KB Output is correct
9 Correct 6 ms 20828 KB Output is correct
10 Correct 6 ms 20568 KB Output is correct
11 Correct 6 ms 20568 KB Output is correct
12 Correct 6 ms 20824 KB Output is correct
13 Correct 5 ms 20568 KB Output is correct
14 Correct 7 ms 20568 KB Output is correct
15 Correct 5 ms 20568 KB Output is correct
16 Correct 5 ms 20568 KB Output is correct
17 Correct 5 ms 20568 KB Output is correct
18 Correct 6 ms 20568 KB Output is correct
19 Correct 7 ms 20568 KB Output is correct
20 Correct 6 ms 20828 KB Output is correct
21 Correct 6 ms 20824 KB Output is correct
22 Correct 5 ms 20568 KB Output is correct
23 Correct 6 ms 20828 KB Output is correct
24 Correct 6 ms 20824 KB Output is correct
25 Correct 6 ms 20824 KB Output is correct
26 Correct 5 ms 20596 KB Output is correct
27 Correct 6 ms 20824 KB Output is correct
28 Correct 5 ms 20824 KB Output is correct
29 Correct 6 ms 20824 KB Output is correct
30 Correct 6 ms 20980 KB Output is correct
31 Correct 7 ms 20824 KB Output is correct
32 Correct 5 ms 20568 KB Output is correct
33 Correct 6 ms 20828 KB Output is correct
34 Correct 8 ms 20824 KB Output is correct
35 Correct 70 ms 25076 KB Output is correct
36 Correct 86 ms 28704 KB Output is correct
37 Correct 85 ms 29008 KB Output is correct
38 Correct 82 ms 28240 KB Output is correct
39 Correct 7 ms 20824 KB Output is correct
40 Correct 9 ms 21080 KB Output is correct
41 Correct 8 ms 21080 KB Output is correct
42 Correct 6 ms 20824 KB Output is correct
43 Correct 8 ms 21080 KB Output is correct
44 Correct 8 ms 21084 KB Output is correct
45 Correct 7 ms 20824 KB Output is correct
46 Correct 7 ms 20824 KB Output is correct
47 Correct 8 ms 21080 KB Output is correct
48 Correct 9 ms 21080 KB Output is correct
49 Correct 15 ms 22360 KB Output is correct
50 Correct 93 ms 29192 KB Output is correct
51 Correct 11 ms 21336 KB Output is correct
52 Correct 74 ms 26356 KB Output is correct
53 Correct 15 ms 22360 KB Output is correct
54 Correct 81 ms 27728 KB Output is correct
55 Correct 11 ms 21708 KB Output is correct
56 Correct 11 ms 21596 KB Output is correct
57 Correct 10 ms 21848 KB Output is correct
58 Correct 11 ms 21852 KB Output is correct
59 Correct 7 ms 21080 KB Output is correct
60 Correct 59 ms 23376 KB Output is correct
61 Correct 9 ms 21336 KB Output is correct
62 Correct 89 ms 28392 KB Output is correct
63 Correct 604 ms 83792 KB Output is correct
64 Correct 618 ms 83792 KB Output is correct
65 Correct 603 ms 83776 KB Output is correct
66 Correct 120 ms 32112 KB Output is correct
67 Correct 342 ms 45140 KB Output is correct
68 Correct 131 ms 34384 KB Output is correct
69 Correct 300 ms 38716 KB Output is correct
70 Correct 127 ms 34548 KB Output is correct
71 Correct 128 ms 34384 KB Output is correct
72 Correct 394 ms 45904 KB Output is correct
73 Correct 397 ms 46176 KB Output is correct
74 Correct 1342 ms 89332 KB Output is correct
75 Correct 530 ms 48328 KB Output is correct
76 Correct 882 ms 67296 KB Output is correct
77 Correct 932 ms 67760 KB Output is correct
78 Correct 186 ms 42832 KB Output is correct
79 Correct 330 ms 47608 KB Output is correct
80 Correct 203 ms 43664 KB Output is correct
81 Correct 298 ms 47228 KB Output is correct
82 Correct 901 ms 68064 KB Output is correct
83 Correct 892 ms 67920 KB Output is correct
84 Correct 767 ms 86676 KB Output is correct
85 Correct 769 ms 86688 KB Output is correct
86 Correct 161 ms 37680 KB Output is correct
87 Correct 176 ms 39724 KB Output is correct
88 Correct 421 ms 46588 KB Output is correct
89 Correct 871 ms 64464 KB Output is correct