답안 #246535

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
246535 2020-07-09T14:34:56 Z egekabas 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++14
100 / 100
387 ms 34556 KB
#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll>    pll;
typedef pair<ull, ull>    pull;
typedef pair<int, int>  pii;
typedef pair<ld, ld>  pld;
ll ans = 0;
ll sq = 500;
ll n, m;
ll prt[100009];
ll sz[100009];
ll id[100009];
ll curbig = 1;
bitset<100009> comesto[509];
bitset<100009> bigin[509];
bitset<100009> bigout[509];
ll totin[100009];
vector<ll> in[100009];
vector<pll> out[100009];
bitset<100009> tmp;

ll findprt(ll x){
    if(prt[x] == x) return x;
    return prt[x] = findprt(prt[x]);
}
void makebig(ll x){
    id[x] = curbig++;
    for(auto u : in[x]){
        bigin[id[x]][findprt(u)] = 1;
        if(findprt(u) != x && comesto[id[x]][u] == 0){
            totin[x]++;
            comesto[id[x]][u] = 1;
        }
    }
    for(auto u : out[x])
        bigout[id[x]][findprt(u.ss)] = 1;
    
}
ll calc(ll x){
    if(id[x]) return totin[x]*sz[x];
    ll cur = 0;
    for(auto u : in[x])
        if(findprt(u) != x && tmp[u] == 0){
            tmp[u] = 1;
            cur += sz[x];
        }
    for(auto u : in[x])
        tmp[u] = 0;
    return cur;
}
void merge(ll x, ll y){
    x = findprt(x);
    y = findprt(y);
    if(x == y) return;
    if(!id[x] && ll(in[x].size()+out[x].size()) > sq)
        makebig(x);
    if(!id[y] && ll(in[y].size()+out[y].size()) > sq)
        makebig(y);
    if(id[y] || (!id[x] && in[y].size()+out[y].size() > in[x].size()+out[x].size()))
        swap(x, y);
    
    if(id[x]){
        if(bigin[id[x]][y] == 0 || bigout[id[x]][y] == 0)
            return;
    }
    else{
        ll goin = 0;
        ll goout = 0;
        for(auto u : in[y])
            if(findprt(u) == x)++goin;
        for(auto u : out[y])
            if(findprt(u.ss) == x)++goout;
        if(goin == 0 || goout == 0)
            return;
    }
    ans -= calc(x)+calc(y);
    vector<ll> mergelater;  
    if(id[x] && id[y]){
        totin[x] -= bigin[id[x]][y];
        prt[y] = x;
        bigout[id[x]] |= bigout[id[y]];
        bigin[id[x]] |= bigin[id[y]];
        comesto[id[x]] |= comesto[id[y]];
        totin[x] = 0;
        for(ll i = 1; i <= n; ++i){
            if(findprt(i) != x && comesto[id[x]][i])
                ++totin[x];
            if(findprt(i) == i && i != x && bigout[id[x]][i] && bigin[id[x]][i])
                mergelater.pb(i);
        }
        ans -= sz[x]*(sz[x]-1)+sz[y]*(sz[y]-1);
        sz[x] += sz[y];
        ans += sz[x]*(sz[x]-1);
    }
    else if(id[x]){

        prt[y] = x;
        for(auto u : in[y]){
            bigin[id[x]][findprt(u)] = 1;
            if(findprt(u) != x && comesto[id[x]][u] == 0){
                //cout << "HEY1\n";
                totin[x]++;
                comesto[id[x]][u] = 1;
            }
            if(findprt(u) != x && bigout[id[x]][findprt(u)])
                mergelater.pb(findprt(u));
        }
        for(auto u : out[y]){
            if(findprt(u.ss) == x && comesto[id[x]][u.ff] == 1){
                //cout << "HEY2\n";
                totin[x]--;
                comesto[id[x]][u.ff] = 0;
            }
            bigout[id[x]][findprt(u.ss)] = 1;
            if(findprt(u.ss) != x && bigin[id[x]][findprt(u.ss)])
                mergelater.pb(findprt(u.ss));
        }

        ans -= sz[x]*(sz[x]-1)+sz[y]*(sz[y]-1);
        sz[x] += sz[y];
        ans += sz[x]*(sz[x]-1);
    }
    else{
        prt[y] = x;
        for(auto u : in[y])
            if(findprt(u) != x) in[x].pb(u);
        for(auto u : out[y])
            if(findprt(u.ss) != x) out[x].pb(u);
        ans -= sz[x]*(sz[x]-1)+sz[y]*(sz[y]-1);
        sz[x] += sz[y];
        ans += sz[x]*(sz[x]-1);
        for(auto u : in[x])
            tmp[findprt(u)] = 1;
        for(auto u : out[x])
            if(tmp[findprt(u.ss)] && findprt(u.ss) != x)
                mergelater.pb(findprt(u.ss));
        for(auto u : in[x])
            tmp[findprt(u)] = 0;
    }
    for(ll i = 1; i < curbig; ++i){
        if(bigin[i][y])
            bigin[i][x] = bigin[i][y];
        bigin[i][y] = 0;
        if(bigout[i][y])
            bigout[i][x] = bigout[i][y];
        bigout[i][y] = 0; 
    }
    if(!id[x] && ll(in[x].size()+out[x].size()) > sq)
        makebig(x);
    ans += calc(x);
    for(auto u : mergelater){
        merge(x, u);
    }
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);

    //freopen("in.txt", "r", stdin);                                                                                             
    //freopen("out.txt", "w", stdout);

    cin >> n >> m;
    for(ll i = 1; i <= n; ++i){
        sz[i] = 1;
        prt[i] = i;
    }
    while(m--){
        ll x, y;
        cin >> x >> y;
        ll dont = (findprt(x) == findprt(y));
        if(id[findprt(y)]){
            if(comesto[id[findprt(y)]][x])
                dont = 1;   
        }
        else{
            for(auto u : in[findprt(y)])
                if(u == x)
                    dont = 1;
        }
        if(dont){
            cout << ans << '\n';
            continue;
        }
        if(id[findprt(x)] == 0)
            out[findprt(x)].pb({x, y});
        if(id[findprt(y)] == 0)
            in[findprt(y)].pb(x);
        if(id[findprt(y)]){
            comesto[id[findprt(y)]][x] = 1;
            totin[findprt(y)]++;
        }
        x = findprt(x);
        y = findprt(y);
        ans += sz[y];
        if(id[x])
            bigout[id[x]][y] = 1;
        if(id[y])
            bigin[id[y]][x] = 1;
        merge(x, y);
        cout << ans << '\n';
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 5120 KB Output is correct
2 Correct 7 ms 4992 KB Output is correct
3 Correct 7 ms 5120 KB Output is correct
4 Correct 7 ms 5120 KB Output is correct
5 Correct 7 ms 4992 KB Output is correct
6 Correct 8 ms 4992 KB Output is correct
7 Correct 8 ms 5120 KB Output is correct
8 Correct 8 ms 5120 KB Output is correct
9 Correct 8 ms 5120 KB Output is correct
10 Correct 7 ms 5120 KB Output is correct
11 Correct 7 ms 5120 KB Output is correct
12 Correct 7 ms 4992 KB Output is correct
13 Correct 7 ms 5120 KB Output is correct
14 Correct 7 ms 4992 KB Output is correct
15 Correct 7 ms 5120 KB Output is correct
16 Correct 7 ms 4992 KB Output is correct
17 Correct 7 ms 5120 KB Output is correct
18 Correct 8 ms 4992 KB Output is correct
19 Correct 8 ms 5120 KB Output is correct
20 Correct 8 ms 5120 KB Output is correct
21 Correct 8 ms 5120 KB Output is correct
22 Correct 8 ms 5120 KB Output is correct
23 Correct 8 ms 5120 KB Output is correct
24 Correct 8 ms 5120 KB Output is correct
25 Correct 8 ms 5120 KB Output is correct
26 Correct 7 ms 5120 KB Output is correct
27 Correct 7 ms 5120 KB Output is correct
28 Correct 7 ms 5120 KB Output is correct
29 Correct 7 ms 5120 KB Output is correct
30 Correct 7 ms 5120 KB Output is correct
31 Correct 8 ms 5120 KB Output is correct
32 Correct 7 ms 5120 KB Output is correct
33 Correct 8 ms 5120 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 5120 KB Output is correct
2 Correct 7 ms 4992 KB Output is correct
3 Correct 7 ms 5120 KB Output is correct
4 Correct 7 ms 5120 KB Output is correct
5 Correct 7 ms 4992 KB Output is correct
6 Correct 8 ms 4992 KB Output is correct
7 Correct 8 ms 5120 KB Output is correct
8 Correct 8 ms 5120 KB Output is correct
9 Correct 8 ms 5120 KB Output is correct
10 Correct 7 ms 5120 KB Output is correct
11 Correct 7 ms 5120 KB Output is correct
12 Correct 7 ms 4992 KB Output is correct
13 Correct 7 ms 5120 KB Output is correct
14 Correct 7 ms 4992 KB Output is correct
15 Correct 7 ms 5120 KB Output is correct
16 Correct 7 ms 4992 KB Output is correct
17 Correct 7 ms 5120 KB Output is correct
18 Correct 8 ms 4992 KB Output is correct
19 Correct 8 ms 5120 KB Output is correct
20 Correct 8 ms 5120 KB Output is correct
21 Correct 8 ms 5120 KB Output is correct
22 Correct 8 ms 5120 KB Output is correct
23 Correct 8 ms 5120 KB Output is correct
24 Correct 8 ms 5120 KB Output is correct
25 Correct 8 ms 5120 KB Output is correct
26 Correct 7 ms 5120 KB Output is correct
27 Correct 7 ms 5120 KB Output is correct
28 Correct 7 ms 5120 KB Output is correct
29 Correct 7 ms 5120 KB Output is correct
30 Correct 7 ms 5120 KB Output is correct
31 Correct 8 ms 5120 KB Output is correct
32 Correct 7 ms 5120 KB Output is correct
33 Correct 8 ms 5120 KB Output is correct
34 Correct 12 ms 5248 KB Output is correct
35 Correct 93 ms 9976 KB Output is correct
36 Correct 97 ms 11352 KB Output is correct
37 Correct 107 ms 11348 KB Output is correct
38 Correct 108 ms 11252 KB Output is correct
39 Correct 10 ms 5376 KB Output is correct
40 Correct 10 ms 5376 KB Output is correct
41 Correct 10 ms 5376 KB Output is correct
42 Correct 10 ms 5376 KB Output is correct
43 Correct 9 ms 5376 KB Output is correct
44 Correct 9 ms 5376 KB Output is correct
45 Correct 10 ms 5376 KB Output is correct
46 Correct 10 ms 5376 KB Output is correct
47 Correct 11 ms 5376 KB Output is correct
48 Correct 11 ms 5376 KB Output is correct
49 Correct 14 ms 5760 KB Output is correct
50 Correct 100 ms 11472 KB Output is correct
51 Correct 11 ms 5504 KB Output is correct
52 Correct 97 ms 10904 KB Output is correct
53 Correct 12 ms 5632 KB Output is correct
54 Correct 117 ms 11388 KB Output is correct
55 Correct 11 ms 5376 KB Output is correct
56 Correct 10 ms 5376 KB Output is correct
57 Correct 10 ms 5632 KB Output is correct
58 Correct 13 ms 5664 KB Output is correct
59 Correct 10 ms 5376 KB Output is correct
60 Correct 103 ms 10360 KB Output is correct
61 Correct 11 ms 5376 KB Output is correct
62 Correct 101 ms 11256 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 5120 KB Output is correct
2 Correct 7 ms 4992 KB Output is correct
3 Correct 7 ms 5120 KB Output is correct
4 Correct 7 ms 5120 KB Output is correct
5 Correct 7 ms 4992 KB Output is correct
6 Correct 8 ms 4992 KB Output is correct
7 Correct 8 ms 5120 KB Output is correct
8 Correct 8 ms 5120 KB Output is correct
9 Correct 8 ms 5120 KB Output is correct
10 Correct 7 ms 5120 KB Output is correct
11 Correct 7 ms 5120 KB Output is correct
12 Correct 7 ms 4992 KB Output is correct
13 Correct 7 ms 5120 KB Output is correct
14 Correct 7 ms 4992 KB Output is correct
15 Correct 7 ms 5120 KB Output is correct
16 Correct 7 ms 4992 KB Output is correct
17 Correct 7 ms 5120 KB Output is correct
18 Correct 8 ms 4992 KB Output is correct
19 Correct 8 ms 5120 KB Output is correct
20 Correct 8 ms 5120 KB Output is correct
21 Correct 8 ms 5120 KB Output is correct
22 Correct 8 ms 5120 KB Output is correct
23 Correct 8 ms 5120 KB Output is correct
24 Correct 8 ms 5120 KB Output is correct
25 Correct 8 ms 5120 KB Output is correct
26 Correct 7 ms 5120 KB Output is correct
27 Correct 7 ms 5120 KB Output is correct
28 Correct 7 ms 5120 KB Output is correct
29 Correct 7 ms 5120 KB Output is correct
30 Correct 7 ms 5120 KB Output is correct
31 Correct 8 ms 5120 KB Output is correct
32 Correct 7 ms 5120 KB Output is correct
33 Correct 8 ms 5120 KB Output is correct
34 Correct 12 ms 5248 KB Output is correct
35 Correct 93 ms 9976 KB Output is correct
36 Correct 97 ms 11352 KB Output is correct
37 Correct 107 ms 11348 KB Output is correct
38 Correct 108 ms 11252 KB Output is correct
39 Correct 10 ms 5376 KB Output is correct
40 Correct 10 ms 5376 KB Output is correct
41 Correct 10 ms 5376 KB Output is correct
42 Correct 10 ms 5376 KB Output is correct
43 Correct 9 ms 5376 KB Output is correct
44 Correct 9 ms 5376 KB Output is correct
45 Correct 10 ms 5376 KB Output is correct
46 Correct 10 ms 5376 KB Output is correct
47 Correct 11 ms 5376 KB Output is correct
48 Correct 11 ms 5376 KB Output is correct
49 Correct 14 ms 5760 KB Output is correct
50 Correct 100 ms 11472 KB Output is correct
51 Correct 11 ms 5504 KB Output is correct
52 Correct 97 ms 10904 KB Output is correct
53 Correct 12 ms 5632 KB Output is correct
54 Correct 117 ms 11388 KB Output is correct
55 Correct 11 ms 5376 KB Output is correct
56 Correct 10 ms 5376 KB Output is correct
57 Correct 10 ms 5632 KB Output is correct
58 Correct 13 ms 5664 KB Output is correct
59 Correct 10 ms 5376 KB Output is correct
60 Correct 103 ms 10360 KB Output is correct
61 Correct 11 ms 5376 KB Output is correct
62 Correct 101 ms 11256 KB Output is correct
63 Correct 302 ms 24688 KB Output is correct
64 Correct 305 ms 24696 KB Output is correct
65 Correct 282 ms 24696 KB Output is correct
66 Correct 144 ms 17400 KB Output is correct
67 Correct 264 ms 20600 KB Output is correct
68 Correct 134 ms 17400 KB Output is correct
69 Correct 155 ms 17272 KB Output is correct
70 Correct 137 ms 17400 KB Output is correct
71 Correct 148 ms 17440 KB Output is correct
72 Correct 280 ms 20472 KB Output is correct
73 Correct 306 ms 20796 KB Output is correct
74 Correct 387 ms 29928 KB Output is correct
75 Correct 278 ms 20216 KB Output is correct
76 Correct 313 ms 23800 KB Output is correct
77 Correct 329 ms 23928 KB Output is correct
78 Correct 121 ms 14600 KB Output is correct
79 Correct 192 ms 19428 KB Output is correct
80 Correct 113 ms 15096 KB Output is correct
81 Correct 176 ms 19704 KB Output is correct
82 Correct 239 ms 19196 KB Output is correct
83 Correct 261 ms 19192 KB Output is correct
84 Correct 224 ms 34556 KB Output is correct
85 Correct 197 ms 34552 KB Output is correct
86 Correct 172 ms 21752 KB Output is correct
87 Correct 205 ms 24056 KB Output is correct
88 Correct 303 ms 20600 KB Output is correct
89 Correct 303 ms 23340 KB Output is correct