답안 #1014587

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1014587 2024-07-05T07:44:17 Z codefox 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++14
17 / 100
3630 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 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 432 KB Output is correct
7 Correct 3 ms 448 KB Output is correct
8 Correct 3 ms 348 KB Output is correct
9 Correct 3 ms 448 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 0 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 0 ms 348 KB Output is correct
18 Correct 1 ms 604 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 3 ms 348 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 2 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 3 ms 348 KB Output is correct
26 Correct 0 ms 436 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 3 ms 348 KB Output is correct
32 Correct 1 ms 344 KB Output is correct
33 Correct 6 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 432 KB Output is correct
7 Correct 3 ms 448 KB Output is correct
8 Correct 3 ms 348 KB Output is correct
9 Correct 3 ms 448 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 0 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 0 ms 348 KB Output is correct
18 Correct 1 ms 604 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 3 ms 348 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 2 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 3 ms 348 KB Output is correct
26 Correct 0 ms 436 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 3 ms 348 KB Output is correct
32 Correct 1 ms 344 KB Output is correct
33 Correct 6 ms 344 KB Output is correct
34 Correct 15 ms 600 KB Output is correct
35 Correct 368 ms 5764 KB Output is correct
36 Correct 403 ms 7888 KB Output is correct
37 Correct 384 ms 8016 KB Output is correct
38 Correct 425 ms 7764 KB Output is correct
39 Correct 7 ms 856 KB Output is correct
40 Correct 8 ms 860 KB Output is correct
41 Correct 7 ms 860 KB Output is correct
42 Correct 6 ms 860 KB Output is correct
43 Correct 129 ms 36688 KB Output is correct
44 Correct 115 ms 36692 KB Output is correct
45 Correct 9 ms 856 KB Output is correct
46 Correct 7 ms 860 KB Output is correct
47 Correct 12 ms 2396 KB Output is correct
48 Correct 14 ms 2648 KB Output is correct
49 Correct 20 ms 1816 KB Output is correct
50 Correct 435 ms 8076 KB Output is correct
51 Correct 86 ms 21328 KB Output is correct
52 Correct 361 ms 7096 KB Output is correct
53 Correct 21 ms 2392 KB Output is correct
54 Correct 396 ms 7468 KB Output is correct
55 Correct 9 ms 1124 KB Output is correct
56 Correct 9 ms 1116 KB Output is correct
57 Correct 13 ms 1096 KB Output is correct
58 Correct 8 ms 1116 KB Output is correct
59 Correct 8 ms 844 KB Output is correct
60 Correct 405 ms 5824 KB Output is correct
61 Correct 29 ms 6480 KB Output is correct
62 Correct 452 ms 7696 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 432 KB Output is correct
7 Correct 3 ms 448 KB Output is correct
8 Correct 3 ms 348 KB Output is correct
9 Correct 3 ms 448 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 0 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 0 ms 348 KB Output is correct
18 Correct 1 ms 604 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 3 ms 348 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 2 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 3 ms 348 KB Output is correct
26 Correct 0 ms 436 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 3 ms 348 KB Output is correct
32 Correct 1 ms 344 KB Output is correct
33 Correct 6 ms 344 KB Output is correct
34 Correct 15 ms 600 KB Output is correct
35 Correct 368 ms 5764 KB Output is correct
36 Correct 403 ms 7888 KB Output is correct
37 Correct 384 ms 8016 KB Output is correct
38 Correct 425 ms 7764 KB Output is correct
39 Correct 7 ms 856 KB Output is correct
40 Correct 8 ms 860 KB Output is correct
41 Correct 7 ms 860 KB Output is correct
42 Correct 6 ms 860 KB Output is correct
43 Correct 129 ms 36688 KB Output is correct
44 Correct 115 ms 36692 KB Output is correct
45 Correct 9 ms 856 KB Output is correct
46 Correct 7 ms 860 KB Output is correct
47 Correct 12 ms 2396 KB Output is correct
48 Correct 14 ms 2648 KB Output is correct
49 Correct 20 ms 1816 KB Output is correct
50 Correct 435 ms 8076 KB Output is correct
51 Correct 86 ms 21328 KB Output is correct
52 Correct 361 ms 7096 KB Output is correct
53 Correct 21 ms 2392 KB Output is correct
54 Correct 396 ms 7468 KB Output is correct
55 Correct 9 ms 1124 KB Output is correct
56 Correct 9 ms 1116 KB Output is correct
57 Correct 13 ms 1096 KB Output is correct
58 Correct 8 ms 1116 KB Output is correct
59 Correct 8 ms 844 KB Output is correct
60 Correct 405 ms 5824 KB Output is correct
61 Correct 29 ms 6480 KB Output is correct
62 Correct 452 ms 7696 KB Output is correct
63 Correct 586 ms 49952 KB Output is correct
64 Correct 658 ms 49936 KB Output is correct
65 Correct 664 ms 50064 KB Output is correct
66 Correct 379 ms 25852 KB Output is correct
67 Correct 484 ms 34416 KB Output is correct
68 Correct 390 ms 25800 KB Output is correct
69 Runtime error 3630 ms 1048576 KB Execution killed with signal 9
70 Halted 0 ms 0 KB -