Submission #359662

# Submission time Handle Problem Language Result Execution time Memory
359662 2021-01-27T06:18:04 Z Nima_Naderi Making Friends on Joitter is Fun (JOI20_joitter2) C++14
100 / 100
1438 ms 129644 KB
///In the name of GOD
//#pragma GCC optimize("O2")
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
const ll MXN = 4e5 + 10;
ll n, m, ans, pnt;
ll Par[MXN], Sz[MXN];
set<ll> In[MXN], Out[MXN], To[MXN], Comp[MXN];
vector<pair<ll, ll>> Q;
ll Find(ll x){
    return (x == Par[x] ? x : Par[x] = Find(Par[x]));
}
void add(ll u, ll v){
    if(u == v) return;
    Out[u].insert(v), In[v].insert(u);
    if(In[u].find(v) != In[u].end()) Q.push_back({u, v});
}
void Union(ll x, ll y){
    x = Find(x), y = Find(y);
    if(x == y) return;
    if(Sz[x] < Sz[y]) swap(x, y);
    /*
    cout << "====================\n";
    cout << x << ' ' << y << '\n';
    cout << ans << '\n';
    cout << To[x].size() << '\n';
    cout << To[y].size() << '\n';
    cout << "====================\n";
    //*/
    ans -= Sz[x] * (Sz[x] - 1);
    ans -= Sz[y] * (Sz[y] - 1);

    ans -= 1ll * To[x].size() * (Sz[x]);
    ans -= 1ll * To[y].size() * (Sz[y]);

    for(auto X : In[y])  Out[X].erase(y);
    for(auto X : Out[y]) In[X].erase(y);
    for(auto X : In[y])  add(X, x);
    for(auto X : Out[y]) add(x, X);

    for(auto X : Comp[y]){
        auto itr = To[x].find(X);
        if(itr == To[x].end()) continue;
        To[x].erase(itr);
    }
    for(auto X : To[y]){
        if(Find(X) != x){
            To[x].insert(X);
        }
    }
    In[y].clear(), Out[y].clear(), To[y].clear();
    Sz[x] += Sz[y], Par[y] = x;
   // cout << "* " << Sz[x] * (Sz[x] - 1) + 1ll * To[x].size() * (Sz[x]) << '\n';
    ans += 1ll * To[x].size() * (Sz[x]);
    ans += Sz[x] * (Sz[x] - 1);
    for(auto X : Comp[y]) Comp[x].insert(X);
    Comp[y].clear();
    /*
    cout << "=====================\n";
    cout << x << '\n';
    cout << "COMP : \n";
    for(auto X : Comp[x]) cout << X << ' '; cout << '\n';
    cout << "In : \n";
    for(auto X : In[x]) cout << X << ' '; cout << '\n';
    cout << "Out : \n";
    for(auto X : Out[x]) cout << X << ' '; cout << '\n';
    cout << "To : \n";
    for(auto X : To[x]) cout << X << ' '; cout << '\n';
    cout << "=====================\n";
    //*/
}
int main(){
    ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);
    cin >> n >> m;
    for(int i = 1; i <= n; i ++) Par[i] = i, Sz[i] = 1, Comp[i].insert(i);
    for(int i = 1; i <= m; i ++){
        ll u, v; cin >> u >> v;
        if(Find(u) == Find(v) || To[Find(v)].find(u) != To[Find(v)].end()){
            cout << ans << '\n';
            continue;
        }
        To[Find(v)].insert(u), ans += Sz[Find(v)];
        add(Find(u), Find(v));
        while(pnt < Q.size()){
            ll x, y; tie(x, y) = Q[pnt];
            pnt ++;
            Union(x, y);
        }
        cout << ans << '\n';
    }
    //cout << To[5].size() << '\n';
    return 0;
}
/*!
    HE'S AN INSTIGATOR,
    ENEMY ELIMINATOR,
    AND WHEN HE KNOCKS YOU BETTER
    YOU BETTER LET HIM IN.
*/
//! N.N

Compilation message

joitter2.cpp: In function 'int main()':
joitter2.cpp:86:19: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   86 |         while(pnt < Q.size()){
      |               ~~~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 50 ms 75500 KB Output is correct
2 Correct 50 ms 75500 KB Output is correct
3 Correct 51 ms 75500 KB Output is correct
4 Correct 52 ms 75500 KB Output is correct
5 Correct 50 ms 75500 KB Output is correct
6 Correct 51 ms 75500 KB Output is correct
7 Correct 51 ms 75500 KB Output is correct
8 Correct 52 ms 75500 KB Output is correct
9 Correct 52 ms 75500 KB Output is correct
10 Correct 51 ms 75500 KB Output is correct
11 Correct 51 ms 75500 KB Output is correct
12 Correct 56 ms 75520 KB Output is correct
13 Correct 52 ms 75520 KB Output is correct
14 Correct 50 ms 75500 KB Output is correct
15 Correct 50 ms 75500 KB Output is correct
16 Correct 50 ms 75500 KB Output is correct
17 Correct 51 ms 75628 KB Output is correct
18 Correct 50 ms 75500 KB Output is correct
19 Correct 50 ms 75500 KB Output is correct
20 Correct 50 ms 75500 KB Output is correct
21 Correct 51 ms 75520 KB Output is correct
22 Correct 51 ms 75500 KB Output is correct
23 Correct 51 ms 75500 KB Output is correct
24 Correct 51 ms 75500 KB Output is correct
25 Correct 51 ms 75500 KB Output is correct
26 Correct 50 ms 75500 KB Output is correct
27 Correct 52 ms 75500 KB Output is correct
28 Correct 51 ms 75500 KB Output is correct
29 Correct 50 ms 75500 KB Output is correct
30 Correct 50 ms 75500 KB Output is correct
31 Correct 52 ms 75500 KB Output is correct
32 Correct 50 ms 75500 KB Output is correct
33 Correct 51 ms 75500 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 50 ms 75500 KB Output is correct
2 Correct 50 ms 75500 KB Output is correct
3 Correct 51 ms 75500 KB Output is correct
4 Correct 52 ms 75500 KB Output is correct
5 Correct 50 ms 75500 KB Output is correct
6 Correct 51 ms 75500 KB Output is correct
7 Correct 51 ms 75500 KB Output is correct
8 Correct 52 ms 75500 KB Output is correct
9 Correct 52 ms 75500 KB Output is correct
10 Correct 51 ms 75500 KB Output is correct
11 Correct 51 ms 75500 KB Output is correct
12 Correct 56 ms 75520 KB Output is correct
13 Correct 52 ms 75520 KB Output is correct
14 Correct 50 ms 75500 KB Output is correct
15 Correct 50 ms 75500 KB Output is correct
16 Correct 50 ms 75500 KB Output is correct
17 Correct 51 ms 75628 KB Output is correct
18 Correct 50 ms 75500 KB Output is correct
19 Correct 50 ms 75500 KB Output is correct
20 Correct 50 ms 75500 KB Output is correct
21 Correct 51 ms 75520 KB Output is correct
22 Correct 51 ms 75500 KB Output is correct
23 Correct 51 ms 75500 KB Output is correct
24 Correct 51 ms 75500 KB Output is correct
25 Correct 51 ms 75500 KB Output is correct
26 Correct 50 ms 75500 KB Output is correct
27 Correct 52 ms 75500 KB Output is correct
28 Correct 51 ms 75500 KB Output is correct
29 Correct 50 ms 75500 KB Output is correct
30 Correct 50 ms 75500 KB Output is correct
31 Correct 52 ms 75500 KB Output is correct
32 Correct 50 ms 75500 KB Output is correct
33 Correct 51 ms 75500 KB Output is correct
34 Correct 54 ms 75628 KB Output is correct
35 Correct 142 ms 81644 KB Output is correct
36 Correct 201 ms 84324 KB Output is correct
37 Correct 173 ms 84452 KB Output is correct
38 Correct 175 ms 84068 KB Output is correct
39 Correct 54 ms 75756 KB Output is correct
40 Correct 60 ms 76012 KB Output is correct
41 Correct 60 ms 75884 KB Output is correct
42 Correct 54 ms 75756 KB Output is correct
43 Correct 55 ms 75884 KB Output is correct
44 Correct 55 ms 75884 KB Output is correct
45 Correct 55 ms 75756 KB Output is correct
46 Correct 60 ms 75756 KB Output is correct
47 Correct 55 ms 75884 KB Output is correct
48 Correct 56 ms 75884 KB Output is correct
49 Correct 66 ms 76524 KB Output is correct
50 Correct 176 ms 84716 KB Output is correct
51 Correct 59 ms 76140 KB Output is correct
52 Correct 157 ms 82800 KB Output is correct
53 Correct 65 ms 76524 KB Output is correct
54 Correct 165 ms 83560 KB Output is correct
55 Correct 57 ms 76268 KB Output is correct
56 Correct 57 ms 76268 KB Output is correct
57 Correct 58 ms 76268 KB Output is correct
58 Correct 57 ms 76268 KB Output is correct
59 Correct 54 ms 75756 KB Output is correct
60 Correct 150 ms 80804 KB Output is correct
61 Correct 56 ms 75884 KB Output is correct
62 Correct 167 ms 83940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 50 ms 75500 KB Output is correct
2 Correct 50 ms 75500 KB Output is correct
3 Correct 51 ms 75500 KB Output is correct
4 Correct 52 ms 75500 KB Output is correct
5 Correct 50 ms 75500 KB Output is correct
6 Correct 51 ms 75500 KB Output is correct
7 Correct 51 ms 75500 KB Output is correct
8 Correct 52 ms 75500 KB Output is correct
9 Correct 52 ms 75500 KB Output is correct
10 Correct 51 ms 75500 KB Output is correct
11 Correct 51 ms 75500 KB Output is correct
12 Correct 56 ms 75520 KB Output is correct
13 Correct 52 ms 75520 KB Output is correct
14 Correct 50 ms 75500 KB Output is correct
15 Correct 50 ms 75500 KB Output is correct
16 Correct 50 ms 75500 KB Output is correct
17 Correct 51 ms 75628 KB Output is correct
18 Correct 50 ms 75500 KB Output is correct
19 Correct 50 ms 75500 KB Output is correct
20 Correct 50 ms 75500 KB Output is correct
21 Correct 51 ms 75520 KB Output is correct
22 Correct 51 ms 75500 KB Output is correct
23 Correct 51 ms 75500 KB Output is correct
24 Correct 51 ms 75500 KB Output is correct
25 Correct 51 ms 75500 KB Output is correct
26 Correct 50 ms 75500 KB Output is correct
27 Correct 52 ms 75500 KB Output is correct
28 Correct 51 ms 75500 KB Output is correct
29 Correct 50 ms 75500 KB Output is correct
30 Correct 50 ms 75500 KB Output is correct
31 Correct 52 ms 75500 KB Output is correct
32 Correct 50 ms 75500 KB Output is correct
33 Correct 51 ms 75500 KB Output is correct
34 Correct 54 ms 75628 KB Output is correct
35 Correct 142 ms 81644 KB Output is correct
36 Correct 201 ms 84324 KB Output is correct
37 Correct 173 ms 84452 KB Output is correct
38 Correct 175 ms 84068 KB Output is correct
39 Correct 54 ms 75756 KB Output is correct
40 Correct 60 ms 76012 KB Output is correct
41 Correct 60 ms 75884 KB Output is correct
42 Correct 54 ms 75756 KB Output is correct
43 Correct 55 ms 75884 KB Output is correct
44 Correct 55 ms 75884 KB Output is correct
45 Correct 55 ms 75756 KB Output is correct
46 Correct 60 ms 75756 KB Output is correct
47 Correct 55 ms 75884 KB Output is correct
48 Correct 56 ms 75884 KB Output is correct
49 Correct 66 ms 76524 KB Output is correct
50 Correct 176 ms 84716 KB Output is correct
51 Correct 59 ms 76140 KB Output is correct
52 Correct 157 ms 82800 KB Output is correct
53 Correct 65 ms 76524 KB Output is correct
54 Correct 165 ms 83560 KB Output is correct
55 Correct 57 ms 76268 KB Output is correct
56 Correct 57 ms 76268 KB Output is correct
57 Correct 58 ms 76268 KB Output is correct
58 Correct 57 ms 76268 KB Output is correct
59 Correct 54 ms 75756 KB Output is correct
60 Correct 150 ms 80804 KB Output is correct
61 Correct 56 ms 75884 KB Output is correct
62 Correct 167 ms 83940 KB Output is correct
63 Correct 649 ms 129624 KB Output is correct
64 Correct 651 ms 129644 KB Output is correct
65 Correct 646 ms 129644 KB Output is correct
66 Correct 310 ms 87960 KB Output is correct
67 Correct 709 ms 94248 KB Output is correct
68 Correct 317 ms 88000 KB Output is correct
69 Correct 712 ms 94816 KB Output is correct
70 Correct 329 ms 87908 KB Output is correct
71 Correct 314 ms 87900 KB Output is correct
72 Correct 736 ms 94448 KB Output is correct
73 Correct 760 ms 94212 KB Output is correct
74 Correct 1438 ms 107212 KB Output is correct
75 Correct 932 ms 100776 KB Output is correct
76 Correct 1167 ms 105356 KB Output is correct
77 Correct 1139 ms 105308 KB Output is correct
78 Correct 325 ms 92900 KB Output is correct
79 Correct 573 ms 96864 KB Output is correct
80 Correct 376 ms 93048 KB Output is correct
81 Correct 603 ms 96736 KB Output is correct
82 Correct 1281 ms 116024 KB Output is correct
83 Correct 1237 ms 115936 KB Output is correct
84 Correct 1033 ms 115808 KB Output is correct
85 Correct 1066 ms 115764 KB Output is correct
86 Correct 531 ms 88572 KB Output is correct
87 Correct 528 ms 90972 KB Output is correct
88 Correct 777 ms 94300 KB Output is correct
89 Correct 1161 ms 104940 KB Output is correct