답안 #1014590

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1014590 2024-07-05T07:44:50 Z codefox 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++14
17 / 100
3584 ms 1048576 KB
#include <bits/stdc++.h>

using namespace std;

vector<int> rep;
vector<set<int>> repgraph;
vector<set<int>> revgraph;
vector<long long> rsize;
vector<vector<int>> rin;

long long sol = 0;

int finde(int i)
{
    if (i != rep[i]) rep[i] = finde(rep[i]);
    return rep[i];
}

int main()
{
    int n, m;
    cin >> n >> m;
    rep.resize(n);
    repgraph.assign(n, set<int>());
    revgraph.assign(n, set<int>());
    rin.assign(n, vector<int>());
    rsize.assign(n, 1);
    iota(rep.begin(), rep.end(), 0);
    for (int i = 0; i < n; i++)
    {
        rin[i].push_back(i);
    }

    for (int i = 0; i < m; i++)
    {
        int a, b;
        cin >> a >> b;
        a--; b--;
        if (finde(a)==finde(b))
        {
            cout << sol << "\n";
            continue;
        }
        if (repgraph[finde(b)].find(finde(a))==repgraph[finde(b)].end())
        {
            //a is not contained in graph from b;
            //connect a to all nodes in b
            sol -= rsize[finde(b)]*revgraph[finde(b)].size();
            repgraph[finde(a)].insert(finde(b));
            revgraph[finde(b)].insert(a);
            sol += rsize[finde(b)]*revgraph[finde(b)].size();
            cout << sol << "\n";
            continue;
        }
        queue<int> q;
        q.push(a);
        while (q.size())
        {
            a = q.front();
            q.pop();
            if (finde(a)==finde(b)) continue;
            repgraph[finde(b)].erase(finde(a));
            if (repgraph[finde(a)].size()>repgraph[finde(b)].size())
            {
                swap(a, b);
            }
            sol -= revgraph[finde(a)].size()*rsize[finde(a)];
            sol -= revgraph[finde(b)].size()*rsize[finde(b)];
            for (int ele:repgraph[finde(a)])
            {
                if(finde(ele)==finde(b)) continue;
                if (repgraph[finde(ele)].find(finde(b))!= repgraph[finde(ele)].end()) q.push(ele);
                repgraph[finde(b)].insert(finde(ele));
            }
            if (rin[finde(a)].size()+revgraph[finde(a)].size() > rin[finde(b)].size()+revgraph[finde(b)].size())
            {
                //repgraph[finde(a)].swap(repgraph[finde(b)]);
                //swap(a, b);
            }
            for (int ele:rin[finde(a)])
            {
                rin[finde(b)].push_back(ele);
                revgraph[finde(b)].erase(ele);
            }
            for (int ele:revgraph[finde(a)])
            {
                repgraph[finde(ele)].erase(finde(a));
                if (repgraph[finde(b)].find(finde(ele))!= repgraph[finde(b)].end())
                {
                    repgraph[finde(b)].erase(finde(ele));
                    q.push(ele);
                }
                else if (finde(ele) != finde(b) && finde(ele)!=finde(a))
                {
                    repgraph[finde(ele)].insert(finde(b));
                    revgraph[finde(b)].insert(ele);
                }
            }
            sol += rsize[finde(a)]*rsize[finde(b)]*2;
            int sum = rsize[finde(a)]+rsize[finde(b)];
            rsize[finde(a)] = sum;
            rsize[finde(b)] = sum;
            rep[finde(a)] = rep[finde(b)];
            sol += revgraph[finde(b)].size()*rsize[finde(b)];
        }
        cout << sol << "\n";
    }

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 600 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 3 ms 348 KB Output is correct
8 Correct 3 ms 348 KB Output is correct
9 Correct 4 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 424 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
21 Correct 4 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 2 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 2 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 2 ms 468 KB Output is correct
32 Correct 1 ms 600 KB Output is correct
33 Correct 3 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 600 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 3 ms 348 KB Output is correct
8 Correct 3 ms 348 KB Output is correct
9 Correct 4 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 424 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
21 Correct 4 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 2 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 2 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 2 ms 468 KB Output is correct
32 Correct 1 ms 600 KB Output is correct
33 Correct 3 ms 348 KB Output is correct
34 Correct 14 ms 512 KB Output is correct
35 Correct 358 ms 5716 KB Output is correct
36 Correct 422 ms 8020 KB Output is correct
37 Correct 406 ms 8076 KB Output is correct
38 Correct 419 ms 7760 KB Output is correct
39 Correct 11 ms 856 KB Output is correct
40 Correct 7 ms 856 KB Output is correct
41 Correct 7 ms 860 KB Output is correct
42 Correct 6 ms 860 KB Output is correct
43 Correct 7 ms 860 KB Output is correct
44 Correct 7 ms 860 KB Output is correct
45 Correct 6 ms 860 KB Output is correct
46 Correct 6 ms 860 KB Output is correct
47 Correct 7 ms 860 KB Output is correct
48 Correct 8 ms 860 KB Output is correct
49 Correct 18 ms 1372 KB Output is correct
50 Correct 393 ms 8088 KB Output is correct
51 Correct 16 ms 1116 KB Output is correct
52 Correct 420 ms 6824 KB Output is correct
53 Correct 18 ms 1368 KB Output is correct
54 Correct 387 ms 7448 KB Output is correct
55 Correct 8 ms 1112 KB Output is correct
56 Correct 8 ms 1116 KB Output is correct
57 Correct 11 ms 1144 KB Output is correct
58 Correct 8 ms 1116 KB Output is correct
59 Correct 7 ms 860 KB Output is correct
60 Correct 388 ms 5892 KB Output is correct
61 Correct 9 ms 856 KB Output is correct
62 Correct 407 ms 7756 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 600 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 3 ms 348 KB Output is correct
8 Correct 3 ms 348 KB Output is correct
9 Correct 4 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 424 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
21 Correct 4 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 2 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 2 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 2 ms 468 KB Output is correct
32 Correct 1 ms 600 KB Output is correct
33 Correct 3 ms 348 KB Output is correct
34 Correct 14 ms 512 KB Output is correct
35 Correct 358 ms 5716 KB Output is correct
36 Correct 422 ms 8020 KB Output is correct
37 Correct 406 ms 8076 KB Output is correct
38 Correct 419 ms 7760 KB Output is correct
39 Correct 11 ms 856 KB Output is correct
40 Correct 7 ms 856 KB Output is correct
41 Correct 7 ms 860 KB Output is correct
42 Correct 6 ms 860 KB Output is correct
43 Correct 7 ms 860 KB Output is correct
44 Correct 7 ms 860 KB Output is correct
45 Correct 6 ms 860 KB Output is correct
46 Correct 6 ms 860 KB Output is correct
47 Correct 7 ms 860 KB Output is correct
48 Correct 8 ms 860 KB Output is correct
49 Correct 18 ms 1372 KB Output is correct
50 Correct 393 ms 8088 KB Output is correct
51 Correct 16 ms 1116 KB Output is correct
52 Correct 420 ms 6824 KB Output is correct
53 Correct 18 ms 1368 KB Output is correct
54 Correct 387 ms 7448 KB Output is correct
55 Correct 8 ms 1112 KB Output is correct
56 Correct 8 ms 1116 KB Output is correct
57 Correct 11 ms 1144 KB Output is correct
58 Correct 8 ms 1116 KB Output is correct
59 Correct 7 ms 860 KB Output is correct
60 Correct 388 ms 5892 KB Output is correct
61 Correct 9 ms 856 KB Output is correct
62 Correct 407 ms 7756 KB Output is correct
63 Correct 589 ms 50024 KB Output is correct
64 Correct 588 ms 49972 KB Output is correct
65 Correct 610 ms 50056 KB Output is correct
66 Correct 341 ms 25800 KB Output is correct
67 Correct 405 ms 33488 KB Output is correct
68 Correct 368 ms 25928 KB Output is correct
69 Correct 419 ms 26508 KB Output is correct
70 Correct 371 ms 25836 KB Output is correct
71 Correct 335 ms 25800 KB Output is correct
72 Correct 415 ms 28804 KB Output is correct
73 Correct 448 ms 30092 KB Output is correct
74 Correct 808 ms 48580 KB Output is correct
75 Correct 603 ms 33352 KB Output is correct
76 Correct 614 ms 40136 KB Output is correct
77 Correct 726 ms 40884 KB Output is correct
78 Correct 310 ms 27088 KB Output is correct
79 Correct 550 ms 31180 KB Output is correct
80 Runtime error 3584 ms 1048576 KB Execution killed with signal 9
81 Halted 0 ms 0 KB -