답안 #484242

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
484242 2021-11-02T17:26:51 Z blue 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++17
100 / 100
1077 ms 72656 KB
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <queue>
#include <cassert>
using namespace std;

const int maxN = 100'000;

vector<int> label(1+maxN);
set<int> out_edge[1+maxN]; //label to label
set<int> in_edge[1+maxN];
vector<int> label_list[1+maxN];
set<int> in_count[1+maxN];

// vector<int> in_count(1+maxN, 0);

int sz(int u)
{
    return int(label_list[u].size());
}


long long answer = 0;


long long comp(int u)
{
    u = label[u];
    return (long long)(in_count[u].size()) * (long long)(label_list[u].size());
}

void insert_in(int B, int A)
{
    answer -= comp(label[B]);
    in_count[label[B]].insert(A);
    answer += comp(label[B]);
}

queue< pair<int, int> > join_queue;


void join(int u, int v)
{
    u = label[u];
    v = label[v];

    if(u == v) return;

    assert(u != v);

    answer -= comp(u);
    answer -= comp(v);

    out_edge[u].erase(v);
    out_edge[v].erase(u);
    in_edge[u].erase(v);
    in_edge[v].erase(u);

    if(sz(u) < sz(v)) swap(u, v);

    for(int l: label_list[v])
    {
        label_list[u].push_back(l);
        label[l] = u;
    }

    for(int ic: in_count[v]) in_count[u].insert(ic);

    for(int o: out_edge[v])
    {
        out_edge[u].insert(o);
        in_edge[o].erase(v);
        in_edge[o].insert(u);
        // assert(label[o] == o);

        if(out_edge[o].find(u) != out_edge[o].end())
            join_queue.push(make_pair(o, u));
    }

    for(int i: in_edge[v])
    {
        in_edge[u].insert(i);
        out_edge[i].erase(v);
        out_edge[i].insert(u);
        // assert(label[i] == i);

        if(in_edge[i].find(u) != in_edge[i].end())
            join_queue.push(make_pair(i, u));
    }

    answer += comp(u);

    in_count[v].clear();
    label_list[v].clear();
    out_edge[v].clear();
    in_edge[v].clear();
}


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

    int N, M;
    cin >> N >> M;


    for(int i = 1; i <= N; i++)
    {
        label[i] = i;
        label_list[i].push_back(i);
        in_count[i].insert(i);
    }


    for(int j = 1; j <= M; j++)
    {
        int A, B;
        cin >> A >> B;

        insert_in(B, A);

        A = label[A];
        B = label[B];

        // cerr << "label = " << A << ' ' << B << '\n';

        if(A != B)
        {
            out_edge[A].insert(B);
            in_edge[B].insert(A);



            if(out_edge[B].find(A) != out_edge[B].end())
            {
                join_queue.push(make_pair(A, B));
            }

            while(!join_queue.empty())
            {
                auto g = join_queue.front();
                join_queue.pop();
                join(g.first, g.second);
            }
        }

        cout << answer << '\n';



        // cerr << "\n operation done\n";
        // cerr << "labels: ";
        // for(int i = 1; i <= N; i++) cerr << label[i] << ' ';
        // cerr << '\n';
        //
        // for(int i = 1; i <= N; i++)
        // {
        //     cerr << "out edge " << i << ": ";
        //     for(int o: out_edge[i]) cerr << o << ' ';
        //     cerr << '\n';
        // }
        // cerr << "\n\n";
    }

    // for(int i = 1; i <= N; i++) cerr << label[i] << ' ';
    // cerr << '\n';
}
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 17100 KB Output is correct
2 Correct 8 ms 17100 KB Output is correct
3 Correct 8 ms 17100 KB Output is correct
4 Correct 8 ms 17100 KB Output is correct
5 Correct 7 ms 17100 KB Output is correct
6 Correct 8 ms 17152 KB Output is correct
7 Correct 8 ms 17100 KB Output is correct
8 Correct 9 ms 17100 KB Output is correct
9 Correct 9 ms 17100 KB Output is correct
10 Correct 8 ms 17100 KB Output is correct
11 Correct 8 ms 17100 KB Output is correct
12 Correct 9 ms 17100 KB Output is correct
13 Correct 10 ms 17100 KB Output is correct
14 Correct 9 ms 17100 KB Output is correct
15 Correct 9 ms 17052 KB Output is correct
16 Correct 8 ms 17100 KB Output is correct
17 Correct 9 ms 17144 KB Output is correct
18 Correct 8 ms 17100 KB Output is correct
19 Correct 8 ms 17100 KB Output is correct
20 Correct 9 ms 17100 KB Output is correct
21 Correct 9 ms 17188 KB Output is correct
22 Correct 8 ms 17100 KB Output is correct
23 Correct 8 ms 17128 KB Output is correct
24 Correct 8 ms 17164 KB Output is correct
25 Correct 8 ms 17188 KB Output is correct
26 Correct 8 ms 17108 KB Output is correct
27 Correct 8 ms 17100 KB Output is correct
28 Correct 10 ms 17140 KB Output is correct
29 Correct 8 ms 17100 KB Output is correct
30 Correct 9 ms 17100 KB Output is correct
31 Correct 9 ms 17100 KB Output is correct
32 Correct 9 ms 17100 KB Output is correct
33 Correct 9 ms 17100 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 17100 KB Output is correct
2 Correct 8 ms 17100 KB Output is correct
3 Correct 8 ms 17100 KB Output is correct
4 Correct 8 ms 17100 KB Output is correct
5 Correct 7 ms 17100 KB Output is correct
6 Correct 8 ms 17152 KB Output is correct
7 Correct 8 ms 17100 KB Output is correct
8 Correct 9 ms 17100 KB Output is correct
9 Correct 9 ms 17100 KB Output is correct
10 Correct 8 ms 17100 KB Output is correct
11 Correct 8 ms 17100 KB Output is correct
12 Correct 9 ms 17100 KB Output is correct
13 Correct 10 ms 17100 KB Output is correct
14 Correct 9 ms 17100 KB Output is correct
15 Correct 9 ms 17052 KB Output is correct
16 Correct 8 ms 17100 KB Output is correct
17 Correct 9 ms 17144 KB Output is correct
18 Correct 8 ms 17100 KB Output is correct
19 Correct 8 ms 17100 KB Output is correct
20 Correct 9 ms 17100 KB Output is correct
21 Correct 9 ms 17188 KB Output is correct
22 Correct 8 ms 17100 KB Output is correct
23 Correct 8 ms 17128 KB Output is correct
24 Correct 8 ms 17164 KB Output is correct
25 Correct 8 ms 17188 KB Output is correct
26 Correct 8 ms 17108 KB Output is correct
27 Correct 8 ms 17100 KB Output is correct
28 Correct 10 ms 17140 KB Output is correct
29 Correct 8 ms 17100 KB Output is correct
30 Correct 9 ms 17100 KB Output is correct
31 Correct 9 ms 17100 KB Output is correct
32 Correct 9 ms 17100 KB Output is correct
33 Correct 9 ms 17100 KB Output is correct
34 Correct 11 ms 17228 KB Output is correct
35 Correct 104 ms 22852 KB Output is correct
36 Correct 142 ms 25604 KB Output is correct
37 Correct 132 ms 25820 KB Output is correct
38 Correct 123 ms 25332 KB Output is correct
39 Correct 10 ms 17308 KB Output is correct
40 Correct 12 ms 17556 KB Output is correct
41 Correct 11 ms 17552 KB Output is correct
42 Correct 9 ms 17356 KB Output is correct
43 Correct 11 ms 17504 KB Output is correct
44 Correct 11 ms 17484 KB Output is correct
45 Correct 10 ms 17356 KB Output is correct
46 Correct 10 ms 17356 KB Output is correct
47 Correct 13 ms 17468 KB Output is correct
48 Correct 11 ms 17500 KB Output is correct
49 Correct 19 ms 18160 KB Output is correct
50 Correct 123 ms 25700 KB Output is correct
51 Correct 14 ms 17780 KB Output is correct
52 Correct 114 ms 24104 KB Output is correct
53 Correct 17 ms 18124 KB Output is correct
54 Correct 123 ms 24952 KB Output is correct
55 Correct 13 ms 17868 KB Output is correct
56 Correct 14 ms 17884 KB Output is correct
57 Correct 13 ms 17868 KB Output is correct
58 Correct 12 ms 17868 KB Output is correct
59 Correct 11 ms 17484 KB Output is correct
60 Correct 95 ms 22332 KB Output is correct
61 Correct 12 ms 17540 KB Output is correct
62 Correct 120 ms 25148 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 17100 KB Output is correct
2 Correct 8 ms 17100 KB Output is correct
3 Correct 8 ms 17100 KB Output is correct
4 Correct 8 ms 17100 KB Output is correct
5 Correct 7 ms 17100 KB Output is correct
6 Correct 8 ms 17152 KB Output is correct
7 Correct 8 ms 17100 KB Output is correct
8 Correct 9 ms 17100 KB Output is correct
9 Correct 9 ms 17100 KB Output is correct
10 Correct 8 ms 17100 KB Output is correct
11 Correct 8 ms 17100 KB Output is correct
12 Correct 9 ms 17100 KB Output is correct
13 Correct 10 ms 17100 KB Output is correct
14 Correct 9 ms 17100 KB Output is correct
15 Correct 9 ms 17052 KB Output is correct
16 Correct 8 ms 17100 KB Output is correct
17 Correct 9 ms 17144 KB Output is correct
18 Correct 8 ms 17100 KB Output is correct
19 Correct 8 ms 17100 KB Output is correct
20 Correct 9 ms 17100 KB Output is correct
21 Correct 9 ms 17188 KB Output is correct
22 Correct 8 ms 17100 KB Output is correct
23 Correct 8 ms 17128 KB Output is correct
24 Correct 8 ms 17164 KB Output is correct
25 Correct 8 ms 17188 KB Output is correct
26 Correct 8 ms 17108 KB Output is correct
27 Correct 8 ms 17100 KB Output is correct
28 Correct 10 ms 17140 KB Output is correct
29 Correct 8 ms 17100 KB Output is correct
30 Correct 9 ms 17100 KB Output is correct
31 Correct 9 ms 17100 KB Output is correct
32 Correct 9 ms 17100 KB Output is correct
33 Correct 9 ms 17100 KB Output is correct
34 Correct 11 ms 17228 KB Output is correct
35 Correct 104 ms 22852 KB Output is correct
36 Correct 142 ms 25604 KB Output is correct
37 Correct 132 ms 25820 KB Output is correct
38 Correct 123 ms 25332 KB Output is correct
39 Correct 10 ms 17308 KB Output is correct
40 Correct 12 ms 17556 KB Output is correct
41 Correct 11 ms 17552 KB Output is correct
42 Correct 9 ms 17356 KB Output is correct
43 Correct 11 ms 17504 KB Output is correct
44 Correct 11 ms 17484 KB Output is correct
45 Correct 10 ms 17356 KB Output is correct
46 Correct 10 ms 17356 KB Output is correct
47 Correct 13 ms 17468 KB Output is correct
48 Correct 11 ms 17500 KB Output is correct
49 Correct 19 ms 18160 KB Output is correct
50 Correct 123 ms 25700 KB Output is correct
51 Correct 14 ms 17780 KB Output is correct
52 Correct 114 ms 24104 KB Output is correct
53 Correct 17 ms 18124 KB Output is correct
54 Correct 123 ms 24952 KB Output is correct
55 Correct 13 ms 17868 KB Output is correct
56 Correct 14 ms 17884 KB Output is correct
57 Correct 13 ms 17868 KB Output is correct
58 Correct 12 ms 17868 KB Output is correct
59 Correct 11 ms 17484 KB Output is correct
60 Correct 95 ms 22332 KB Output is correct
61 Correct 12 ms 17540 KB Output is correct
62 Correct 120 ms 25148 KB Output is correct
63 Correct 589 ms 72656 KB Output is correct
64 Correct 594 ms 72600 KB Output is correct
65 Correct 568 ms 72588 KB Output is correct
66 Correct 217 ms 29900 KB Output is correct
67 Correct 703 ms 38488 KB Output is correct
68 Correct 236 ms 29824 KB Output is correct
69 Correct 405 ms 36736 KB Output is correct
70 Correct 234 ms 29884 KB Output is correct
71 Correct 245 ms 29852 KB Output is correct
72 Correct 547 ms 36752 KB Output is correct
73 Correct 544 ms 36928 KB Output is correct
74 Correct 1077 ms 47880 KB Output is correct
75 Correct 637 ms 42604 KB Output is correct
76 Correct 915 ms 47292 KB Output is correct
77 Correct 815 ms 47388 KB Output is correct
78 Correct 222 ms 35476 KB Output is correct
79 Correct 453 ms 38836 KB Output is correct
80 Correct 249 ms 35520 KB Output is correct
81 Correct 368 ms 38872 KB Output is correct
82 Correct 803 ms 57428 KB Output is correct
83 Correct 818 ms 57404 KB Output is correct
84 Correct 617 ms 57496 KB Output is correct
85 Correct 637 ms 57408 KB Output is correct
86 Correct 425 ms 34324 KB Output is correct
87 Correct 441 ms 36464 KB Output is correct
88 Correct 513 ms 36768 KB Output is correct
89 Correct 787 ms 47044 KB Output is correct