답안 #1045146

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1045146 2024-08-05T17:34:37 Z Andrey 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++14
100 / 100
307 ms 50256 KB
#include<bits/stdc++.h>
using namespace std;

set<int> haha[100001];
set<int> bruh[100001];
vector<int> no[100001];
vector<int> dsu(100001);
vector<long long> st(100001,1);
queue<pair<int,int>> wut;

int calc(int a) {
    int c = a;
    while(dsu[c] != c) {
        c = dsu[c];
    }
    int e = a;
    while(e != dsu[e]) {
        int d = dsu[e];
        dsu[e] = c;
        e = d;
    }
    return c;
}

long long ans = 0;

void merge(int a, int b) {
    a = calc(a);
    b = calc(b);
    if(a == b) {
        return;
    }
    if(haha[a].size()+bruh[a].size()+st[a] < haha[b].size()+bruh[b].size()+st[b]) {
        swap(a,b);
    }
    ans+=st[a]*st[b]*2;
    ans-=(long long)haha[a].size()*st[a];
    ans-=(long long)haha[b].size()*st[b];
    vector<int> idk(0);
    vector<int> wow(0);
    while(!haha[b].empty()) {
        int c = *haha[b].lower_bound(-INT_MAX);
        haha[b].erase(c);
        idk.push_back(c);
    }
    while(!bruh[b].empty()) {
        int c = *bruh[b].lower_bound(-INT_MAX);
        bruh[b].erase(c);
        wow.push_back(c);
    }
    for(int i = 0; i < idk.size(); i++) {
        int c = calc(idk[i]);
        if(c == a) {
            continue;
        }
        bruh[c].erase(b);
        bruh[c].insert(a);
    }
    for(int i = 0; i < idk.size(); i++) {
        if(calc(idk[i]) != a) {
            haha[a].insert(idk[i]);
        }
    }
    for(int i = 0; i < wow.size(); i++) {
        if(wow[i] != a) {
            bruh[a].insert(wow[i]);
        }   
    }
    bruh[a].erase(b);
    for(int i = 0; i < no[b].size(); i++) {
        haha[a].erase(no[b][i]);
        no[a].push_back(no[b][i]);
    }
    st[a]+=st[b];
    ans+=(long long)haha[a].size()*st[a];
    dsu[b] = a;
    for(int i = 0; i < idk.size(); i++) {
        int c = calc(idk[i]);
        if(bruh[a].find(c) != bruh[a].end()) {
            wut.push({a,c});
        }
    }
    for(int i = 0; i < wow.size(); i++) {
        int c = wow[i];
        if(bruh[c].find(a) != bruh[c].end()) {
            wut.push({a,c});
        }
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int n,m,a,b;
    cin >> n >> m;
    for(int i = 1; i <= n; i++) {
        dsu[i] = i;
        no[i].push_back(i);
    }
    for(int i = 0; i < m; i++) {
        cin >> a >> b;
        int x = a;
        a = calc(a);
        b = calc(b);
        if(a == b) {
            cout << ans << "\n";
            continue;
        }
        ans-=(long long)haha[b].size()*st[b];
        haha[b].insert(x);
        bruh[a].insert(b);
        ans+=(long long)haha[b].size()*st[b];
        if(bruh[b].find(a) != bruh[b].end()) {
            wut.push({a,b});
        }
        while(!wut.empty()) {
            pair<int,int> c = wut.front();
            wut.pop();
            merge(c.first,c.second);
        }
        cout << ans << "\n";
    }
    return 0;
}

Compilation message

joitter2.cpp: In function 'void merge(int, int)':
joitter2.cpp:51:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   51 |     for(int i = 0; i < idk.size(); i++) {
      |                    ~~^~~~~~~~~~~~
joitter2.cpp:59:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   59 |     for(int i = 0; i < idk.size(); i++) {
      |                    ~~^~~~~~~~~~~~
joitter2.cpp:64:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   64 |     for(int i = 0; i < wow.size(); i++) {
      |                    ~~^~~~~~~~~~~~
joitter2.cpp:70:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   70 |     for(int i = 0; i < no[b].size(); i++) {
      |                    ~~^~~~~~~~~~~~~~
joitter2.cpp:77:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   77 |     for(int i = 0; i < idk.size(); i++) {
      |                    ~~^~~~~~~~~~~~
joitter2.cpp:83:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   83 |     for(int i = 0; i < wow.size(); i++) {
      |                    ~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 13148 KB Output is correct
2 Correct 2 ms 13148 KB Output is correct
3 Correct 2 ms 13148 KB Output is correct
4 Correct 2 ms 13148 KB Output is correct
5 Correct 3 ms 13332 KB Output is correct
6 Correct 3 ms 13148 KB Output is correct
7 Correct 3 ms 13404 KB Output is correct
8 Correct 3 ms 13404 KB Output is correct
9 Correct 3 ms 13404 KB Output is correct
10 Correct 3 ms 13148 KB Output is correct
11 Correct 3 ms 13148 KB Output is correct
12 Correct 2 ms 13148 KB Output is correct
13 Correct 3 ms 13400 KB Output is correct
14 Correct 2 ms 13160 KB Output is correct
15 Correct 3 ms 13148 KB Output is correct
16 Correct 2 ms 13148 KB Output is correct
17 Correct 2 ms 13396 KB Output is correct
18 Correct 2 ms 13148 KB Output is correct
19 Correct 2 ms 13148 KB Output is correct
20 Correct 3 ms 13148 KB Output is correct
21 Correct 2 ms 13404 KB Output is correct
22 Correct 3 ms 13144 KB Output is correct
23 Correct 3 ms 13404 KB Output is correct
24 Correct 2 ms 13148 KB Output is correct
25 Correct 3 ms 13404 KB Output is correct
26 Correct 2 ms 13404 KB Output is correct
27 Correct 2 ms 13148 KB Output is correct
28 Correct 2 ms 13148 KB Output is correct
29 Correct 2 ms 13148 KB Output is correct
30 Correct 2 ms 13188 KB Output is correct
31 Correct 3 ms 13404 KB Output is correct
32 Correct 2 ms 13392 KB Output is correct
33 Correct 3 ms 13404 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 13148 KB Output is correct
2 Correct 2 ms 13148 KB Output is correct
3 Correct 2 ms 13148 KB Output is correct
4 Correct 2 ms 13148 KB Output is correct
5 Correct 3 ms 13332 KB Output is correct
6 Correct 3 ms 13148 KB Output is correct
7 Correct 3 ms 13404 KB Output is correct
8 Correct 3 ms 13404 KB Output is correct
9 Correct 3 ms 13404 KB Output is correct
10 Correct 3 ms 13148 KB Output is correct
11 Correct 3 ms 13148 KB Output is correct
12 Correct 2 ms 13148 KB Output is correct
13 Correct 3 ms 13400 KB Output is correct
14 Correct 2 ms 13160 KB Output is correct
15 Correct 3 ms 13148 KB Output is correct
16 Correct 2 ms 13148 KB Output is correct
17 Correct 2 ms 13396 KB Output is correct
18 Correct 2 ms 13148 KB Output is correct
19 Correct 2 ms 13148 KB Output is correct
20 Correct 3 ms 13148 KB Output is correct
21 Correct 2 ms 13404 KB Output is correct
22 Correct 3 ms 13144 KB Output is correct
23 Correct 3 ms 13404 KB Output is correct
24 Correct 2 ms 13148 KB Output is correct
25 Correct 3 ms 13404 KB Output is correct
26 Correct 2 ms 13404 KB Output is correct
27 Correct 2 ms 13148 KB Output is correct
28 Correct 2 ms 13148 KB Output is correct
29 Correct 2 ms 13148 KB Output is correct
30 Correct 2 ms 13188 KB Output is correct
31 Correct 3 ms 13404 KB Output is correct
32 Correct 2 ms 13392 KB Output is correct
33 Correct 3 ms 13404 KB Output is correct
34 Correct 4 ms 13404 KB Output is correct
35 Correct 40 ms 18604 KB Output is correct
36 Correct 52 ms 20580 KB Output is correct
37 Correct 52 ms 20612 KB Output is correct
38 Correct 52 ms 20304 KB Output is correct
39 Correct 3 ms 13404 KB Output is correct
40 Correct 4 ms 13404 KB Output is correct
41 Correct 4 ms 13404 KB Output is correct
42 Correct 3 ms 13404 KB Output is correct
43 Correct 4 ms 13404 KB Output is correct
44 Correct 4 ms 13604 KB Output is correct
45 Correct 3 ms 13404 KB Output is correct
46 Correct 3 ms 13404 KB Output is correct
47 Correct 4 ms 13404 KB Output is correct
48 Correct 4 ms 13404 KB Output is correct
49 Correct 7 ms 13916 KB Output is correct
50 Correct 55 ms 20628 KB Output is correct
51 Correct 6 ms 13656 KB Output is correct
52 Correct 48 ms 19688 KB Output is correct
53 Correct 7 ms 13916 KB Output is correct
54 Correct 51 ms 20092 KB Output is correct
55 Correct 5 ms 13656 KB Output is correct
56 Correct 4 ms 13660 KB Output is correct
57 Correct 4 ms 13660 KB Output is correct
58 Correct 5 ms 13656 KB Output is correct
59 Correct 4 ms 13404 KB Output is correct
60 Correct 42 ms 18352 KB Output is correct
61 Correct 4 ms 13656 KB Output is correct
62 Correct 51 ms 20308 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 13148 KB Output is correct
2 Correct 2 ms 13148 KB Output is correct
3 Correct 2 ms 13148 KB Output is correct
4 Correct 2 ms 13148 KB Output is correct
5 Correct 3 ms 13332 KB Output is correct
6 Correct 3 ms 13148 KB Output is correct
7 Correct 3 ms 13404 KB Output is correct
8 Correct 3 ms 13404 KB Output is correct
9 Correct 3 ms 13404 KB Output is correct
10 Correct 3 ms 13148 KB Output is correct
11 Correct 3 ms 13148 KB Output is correct
12 Correct 2 ms 13148 KB Output is correct
13 Correct 3 ms 13400 KB Output is correct
14 Correct 2 ms 13160 KB Output is correct
15 Correct 3 ms 13148 KB Output is correct
16 Correct 2 ms 13148 KB Output is correct
17 Correct 2 ms 13396 KB Output is correct
18 Correct 2 ms 13148 KB Output is correct
19 Correct 2 ms 13148 KB Output is correct
20 Correct 3 ms 13148 KB Output is correct
21 Correct 2 ms 13404 KB Output is correct
22 Correct 3 ms 13144 KB Output is correct
23 Correct 3 ms 13404 KB Output is correct
24 Correct 2 ms 13148 KB Output is correct
25 Correct 3 ms 13404 KB Output is correct
26 Correct 2 ms 13404 KB Output is correct
27 Correct 2 ms 13148 KB Output is correct
28 Correct 2 ms 13148 KB Output is correct
29 Correct 2 ms 13148 KB Output is correct
30 Correct 2 ms 13188 KB Output is correct
31 Correct 3 ms 13404 KB Output is correct
32 Correct 2 ms 13392 KB Output is correct
33 Correct 3 ms 13404 KB Output is correct
34 Correct 4 ms 13404 KB Output is correct
35 Correct 40 ms 18604 KB Output is correct
36 Correct 52 ms 20580 KB Output is correct
37 Correct 52 ms 20612 KB Output is correct
38 Correct 52 ms 20304 KB Output is correct
39 Correct 3 ms 13404 KB Output is correct
40 Correct 4 ms 13404 KB Output is correct
41 Correct 4 ms 13404 KB Output is correct
42 Correct 3 ms 13404 KB Output is correct
43 Correct 4 ms 13404 KB Output is correct
44 Correct 4 ms 13604 KB Output is correct
45 Correct 3 ms 13404 KB Output is correct
46 Correct 3 ms 13404 KB Output is correct
47 Correct 4 ms 13404 KB Output is correct
48 Correct 4 ms 13404 KB Output is correct
49 Correct 7 ms 13916 KB Output is correct
50 Correct 55 ms 20628 KB Output is correct
51 Correct 6 ms 13656 KB Output is correct
52 Correct 48 ms 19688 KB Output is correct
53 Correct 7 ms 13916 KB Output is correct
54 Correct 51 ms 20092 KB Output is correct
55 Correct 5 ms 13656 KB Output is correct
56 Correct 4 ms 13660 KB Output is correct
57 Correct 4 ms 13660 KB Output is correct
58 Correct 5 ms 13656 KB Output is correct
59 Correct 4 ms 13404 KB Output is correct
60 Correct 42 ms 18352 KB Output is correct
61 Correct 4 ms 13656 KB Output is correct
62 Correct 51 ms 20308 KB Output is correct
63 Correct 189 ms 50004 KB Output is correct
64 Correct 186 ms 50004 KB Output is correct
65 Correct 179 ms 50256 KB Output is correct
66 Correct 53 ms 21448 KB Output is correct
67 Correct 97 ms 26952 KB Output is correct
68 Correct 59 ms 21404 KB Output is correct
69 Correct 142 ms 25932 KB Output is correct
70 Correct 51 ms 21344 KB Output is correct
71 Correct 56 ms 21228 KB Output is correct
72 Correct 127 ms 25784 KB Output is correct
73 Correct 119 ms 26060 KB Output is correct
74 Correct 286 ms 34000 KB Output is correct
75 Correct 184 ms 30408 KB Output is correct
76 Correct 219 ms 33480 KB Output is correct
77 Correct 239 ms 33476 KB Output is correct
78 Correct 95 ms 24532 KB Output is correct
79 Correct 126 ms 28084 KB Output is correct
80 Correct 68 ms 24528 KB Output is correct
81 Correct 119 ms 28108 KB Output is correct
82 Correct 269 ms 39292 KB Output is correct
83 Correct 307 ms 39376 KB Output is correct
84 Correct 208 ms 39412 KB Output is correct
85 Correct 214 ms 39432 KB Output is correct
86 Correct 61 ms 26184 KB Output is correct
87 Correct 81 ms 27972 KB Output is correct
88 Correct 120 ms 25868 KB Output is correct
89 Correct 230 ms 33148 KB Output is correct