답안 #795957

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
795957 2023-07-28T01:05:15 Z Ozy 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++17
100 / 100
944 ms 69420 KB
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define debug(a) cout << #a << " = " << a << endl
#define debugsl(a) cout << #a << " = " <<  a << ", "
#define rep(i,a,b) for(int i = (a); i <= (b); i++)
#define repa(i,a,b) for(int i = (a); i >= (b); i--)
#define pll pair<lli,lli>

#define MAX 100000

lli n,m,a,b,res;
lli uf[MAX+2],tam[MAX+2];
set<lli> llegan[MAX+2],salen[MAX+2],hijos[MAX+2];
queue<pll> cola;

lli sube(lli pos) {
    if (uf[pos] == pos) return pos;
    uf[pos] = sube(uf[pos]);
    return uf[pos];
}

void arista_azul(lli a, lli b) {
    llegan[b].insert(a);
    salen[a].insert(b);
    if (llegan[a].count(b)) cola.push({a,b});
}

lli func(lli t) {return (t*(t-1));}

lli sz(lli a) {return (hijos[a].size() + llegan[a].size() + salen[a].size());}

void solve(lli a, lli b) {
    if (a == b) return;

    if (sz(a) < sz(b)) swap(a,b);

    res += hijos[a].size()*tam[b] + tam[a]*hijos[b].size();

    tam[a] += tam[b];
    uf[b] = a;

    for(auto h : hijos[b]) {
        if (hijos[a].count(h)) res -= tam[a];
        else hijos[a].insert(h);
    }


    llegan[a].erase(b); 
    salen[b].erase(a);
    llegan[b].erase(a);
    salen[a].erase(b);
    for(auto act : llegan[b]) {
        salen[act].erase(b);
        arista_azul(act,a);
    }
    for(auto act : salen[b]) {
        llegan[act].erase(b);
        arista_azul(a,act);
    }

    //debug(res);
}

int main() 
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    cin >> n >> m;
    // inicializa
    rep(i,1,n) {
        uf[i] = i;
        tam[i] = 1;
        hijos[i].insert(i);  //porque se inserta a si mismo
    }
    
    rep(i,1,m) {
        cin >> a >> b;

        b = sube(b);
        if(sube(a) == b || hijos[b].count(a)) {
            cout << res << "\n";   
            continue;
        }

        hijos[b].insert(a);
        res += tam[b];

        a = sube(a);
        arista_azul(a,b);


        while (!cola.empty()) {
            pll act = cola.front();
            cola.pop();
            solve(sube(act.first),sube(act.second));
        }
        cout << res << "\n";
    }

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14420 KB Output is correct
2 Correct 7 ms 14468 KB Output is correct
3 Correct 7 ms 14424 KB Output is correct
4 Correct 7 ms 14324 KB Output is correct
5 Correct 7 ms 14428 KB Output is correct
6 Correct 8 ms 14340 KB Output is correct
7 Correct 9 ms 14436 KB Output is correct
8 Correct 8 ms 14460 KB Output is correct
9 Correct 8 ms 14420 KB Output is correct
10 Correct 7 ms 14420 KB Output is correct
11 Correct 7 ms 14436 KB Output is correct
12 Correct 8 ms 14420 KB Output is correct
13 Correct 7 ms 14420 KB Output is correct
14 Correct 8 ms 14428 KB Output is correct
15 Correct 7 ms 14424 KB Output is correct
16 Correct 7 ms 14420 KB Output is correct
17 Correct 7 ms 14420 KB Output is correct
18 Correct 7 ms 14428 KB Output is correct
19 Correct 7 ms 14420 KB Output is correct
20 Correct 8 ms 14420 KB Output is correct
21 Correct 8 ms 14420 KB Output is correct
22 Correct 7 ms 14420 KB Output is correct
23 Correct 7 ms 14348 KB Output is correct
24 Correct 8 ms 14420 KB Output is correct
25 Correct 7 ms 14420 KB Output is correct
26 Correct 8 ms 14428 KB Output is correct
27 Correct 7 ms 14420 KB Output is correct
28 Correct 7 ms 14424 KB Output is correct
29 Correct 7 ms 14420 KB Output is correct
30 Correct 7 ms 14396 KB Output is correct
31 Correct 9 ms 14440 KB Output is correct
32 Correct 7 ms 14432 KB Output is correct
33 Correct 7 ms 14432 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14420 KB Output is correct
2 Correct 7 ms 14468 KB Output is correct
3 Correct 7 ms 14424 KB Output is correct
4 Correct 7 ms 14324 KB Output is correct
5 Correct 7 ms 14428 KB Output is correct
6 Correct 8 ms 14340 KB Output is correct
7 Correct 9 ms 14436 KB Output is correct
8 Correct 8 ms 14460 KB Output is correct
9 Correct 8 ms 14420 KB Output is correct
10 Correct 7 ms 14420 KB Output is correct
11 Correct 7 ms 14436 KB Output is correct
12 Correct 8 ms 14420 KB Output is correct
13 Correct 7 ms 14420 KB Output is correct
14 Correct 8 ms 14428 KB Output is correct
15 Correct 7 ms 14424 KB Output is correct
16 Correct 7 ms 14420 KB Output is correct
17 Correct 7 ms 14420 KB Output is correct
18 Correct 7 ms 14428 KB Output is correct
19 Correct 7 ms 14420 KB Output is correct
20 Correct 8 ms 14420 KB Output is correct
21 Correct 8 ms 14420 KB Output is correct
22 Correct 7 ms 14420 KB Output is correct
23 Correct 7 ms 14348 KB Output is correct
24 Correct 8 ms 14420 KB Output is correct
25 Correct 7 ms 14420 KB Output is correct
26 Correct 8 ms 14428 KB Output is correct
27 Correct 7 ms 14420 KB Output is correct
28 Correct 7 ms 14424 KB Output is correct
29 Correct 7 ms 14420 KB Output is correct
30 Correct 7 ms 14396 KB Output is correct
31 Correct 9 ms 14440 KB Output is correct
32 Correct 7 ms 14432 KB Output is correct
33 Correct 7 ms 14432 KB Output is correct
34 Correct 12 ms 14548 KB Output is correct
35 Correct 66 ms 20340 KB Output is correct
36 Correct 87 ms 23116 KB Output is correct
37 Correct 89 ms 23272 KB Output is correct
38 Correct 106 ms 22900 KB Output is correct
39 Correct 9 ms 14804 KB Output is correct
40 Correct 11 ms 15104 KB Output is correct
41 Correct 10 ms 15076 KB Output is correct
42 Correct 9 ms 14804 KB Output is correct
43 Correct 10 ms 14748 KB Output is correct
44 Correct 10 ms 14804 KB Output is correct
45 Correct 9 ms 14804 KB Output is correct
46 Correct 9 ms 14700 KB Output is correct
47 Correct 10 ms 14992 KB Output is correct
48 Correct 10 ms 14960 KB Output is correct
49 Correct 15 ms 15700 KB Output is correct
50 Correct 93 ms 23436 KB Output is correct
51 Correct 13 ms 15032 KB Output is correct
52 Correct 80 ms 21476 KB Output is correct
53 Correct 14 ms 15620 KB Output is correct
54 Correct 84 ms 22464 KB Output is correct
55 Correct 15 ms 15176 KB Output is correct
56 Correct 12 ms 15232 KB Output is correct
57 Correct 11 ms 15076 KB Output is correct
58 Correct 11 ms 15188 KB Output is correct
59 Correct 10 ms 15148 KB Output is correct
60 Correct 67 ms 20148 KB Output is correct
61 Correct 11 ms 14932 KB Output is correct
62 Correct 82 ms 22732 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14420 KB Output is correct
2 Correct 7 ms 14468 KB Output is correct
3 Correct 7 ms 14424 KB Output is correct
4 Correct 7 ms 14324 KB Output is correct
5 Correct 7 ms 14428 KB Output is correct
6 Correct 8 ms 14340 KB Output is correct
7 Correct 9 ms 14436 KB Output is correct
8 Correct 8 ms 14460 KB Output is correct
9 Correct 8 ms 14420 KB Output is correct
10 Correct 7 ms 14420 KB Output is correct
11 Correct 7 ms 14436 KB Output is correct
12 Correct 8 ms 14420 KB Output is correct
13 Correct 7 ms 14420 KB Output is correct
14 Correct 8 ms 14428 KB Output is correct
15 Correct 7 ms 14424 KB Output is correct
16 Correct 7 ms 14420 KB Output is correct
17 Correct 7 ms 14420 KB Output is correct
18 Correct 7 ms 14428 KB Output is correct
19 Correct 7 ms 14420 KB Output is correct
20 Correct 8 ms 14420 KB Output is correct
21 Correct 8 ms 14420 KB Output is correct
22 Correct 7 ms 14420 KB Output is correct
23 Correct 7 ms 14348 KB Output is correct
24 Correct 8 ms 14420 KB Output is correct
25 Correct 7 ms 14420 KB Output is correct
26 Correct 8 ms 14428 KB Output is correct
27 Correct 7 ms 14420 KB Output is correct
28 Correct 7 ms 14424 KB Output is correct
29 Correct 7 ms 14420 KB Output is correct
30 Correct 7 ms 14396 KB Output is correct
31 Correct 9 ms 14440 KB Output is correct
32 Correct 7 ms 14432 KB Output is correct
33 Correct 7 ms 14432 KB Output is correct
34 Correct 12 ms 14548 KB Output is correct
35 Correct 66 ms 20340 KB Output is correct
36 Correct 87 ms 23116 KB Output is correct
37 Correct 89 ms 23272 KB Output is correct
38 Correct 106 ms 22900 KB Output is correct
39 Correct 9 ms 14804 KB Output is correct
40 Correct 11 ms 15104 KB Output is correct
41 Correct 10 ms 15076 KB Output is correct
42 Correct 9 ms 14804 KB Output is correct
43 Correct 10 ms 14748 KB Output is correct
44 Correct 10 ms 14804 KB Output is correct
45 Correct 9 ms 14804 KB Output is correct
46 Correct 9 ms 14700 KB Output is correct
47 Correct 10 ms 14992 KB Output is correct
48 Correct 10 ms 14960 KB Output is correct
49 Correct 15 ms 15700 KB Output is correct
50 Correct 93 ms 23436 KB Output is correct
51 Correct 13 ms 15032 KB Output is correct
52 Correct 80 ms 21476 KB Output is correct
53 Correct 14 ms 15620 KB Output is correct
54 Correct 84 ms 22464 KB Output is correct
55 Correct 15 ms 15176 KB Output is correct
56 Correct 12 ms 15232 KB Output is correct
57 Correct 11 ms 15076 KB Output is correct
58 Correct 11 ms 15188 KB Output is correct
59 Correct 10 ms 15148 KB Output is correct
60 Correct 67 ms 20148 KB Output is correct
61 Correct 11 ms 14932 KB Output is correct
62 Correct 82 ms 22732 KB Output is correct
63 Correct 496 ms 68264 KB Output is correct
64 Correct 527 ms 68344 KB Output is correct
65 Correct 519 ms 68452 KB Output is correct
66 Correct 184 ms 34480 KB Output is correct
67 Correct 381 ms 61376 KB Output is correct
68 Correct 191 ms 34504 KB Output is correct
69 Correct 368 ms 35524 KB Output is correct
70 Correct 187 ms 34476 KB Output is correct
71 Correct 190 ms 34388 KB Output is correct
72 Correct 416 ms 45388 KB Output is correct
73 Correct 366 ms 47104 KB Output is correct
74 Correct 944 ms 69420 KB Output is correct
75 Correct 503 ms 42240 KB Output is correct
76 Correct 717 ms 54328 KB Output is correct
77 Correct 753 ms 54580 KB Output is correct
78 Correct 200 ms 38084 KB Output is correct
79 Correct 363 ms 42544 KB Output is correct
80 Correct 186 ms 36904 KB Output is correct
81 Correct 349 ms 40604 KB Output is correct
82 Correct 763 ms 55128 KB Output is correct
83 Correct 765 ms 55044 KB Output is correct
84 Correct 604 ms 53768 KB Output is correct
85 Correct 581 ms 53704 KB Output is correct
86 Correct 293 ms 67212 KB Output is correct
87 Correct 303 ms 69408 KB Output is correct
88 Correct 417 ms 46392 KB Output is correct
89 Correct 615 ms 52360 KB Output is correct