USACO 2021 Silver January P1: Dance Mooves

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

#define f first
#define s second

ll N, K;

vector<pair<int, int>> swaps;
vector<int> jumpTable;

vector<int> myGroup;
vector<vector<int>> groups;
vector<vector<int>> trace;
vector<int> ans;

// STEP 1: simulate to get the trace for each node
void simulation(vector<ll> &a) {
    // include itself
    for (int i = 1; i <= N; i++) {
        trace[i].push_back(i);
    }

    // simulate first K cycles
    for(int i = 0; i < swaps.size(); i++) {

        int l = swaps[i].f;
        int r = swaps[i].s;

        trace[a[l]].push_back(r);
        trace[a[r]].push_back(i);

        swap(a[l], a[r]);
    }
}

// STEP 2: create jump table
void make_table(vector<ll> &a) {
    // create jumpTable
    for (int i = 1; i < a.size(); i++) {
        jumpTable[a[i]] = i;
    }
}


// STEP 3: find groups of cows using DFS
void findCycles(vector<ll> &a) {
    int currentID = 0;

    for(int i = 1; i <= N ; i++) {
        if (myGroup[i] != -1) {
            continue;
        }

        vector<int> groupIds;
        myGroup[i] = currentID;
        groupIds.push_back(i);
        int j = jumpTable[i];
        int currPhase = 0;
        while (j != i) {
            currPhase++;
            myGroup[j] = currentID;
            groupIds.push_back(j);
            j = jumpTable[j];
        }

        groups.push_back(groupIds);
        currentID++;
    }
}



int main() {
    cin >> N >> K;

    swaps.resize(K);
    trace.resize(N+1);
    jumpTable.resize(N+1);
    myGroup.resize(N+1, -1);
    ans.resize(N+1, 0);

    for (int i = 0; i < K; i++) {
        int a, b;
        cin >> a >> b;
        swaps[i].f = a;
        swaps[i].s = b;
    }

    vector<ll> a(N+1);
    iota(a.begin(), a.end(), 0);

    simulation(a);
    make_table(a);

    findCycles(a);

    map<int, int> finAns;
    for(int groupId = 0; groupId < groups.size(); groupId++) {
        // everything
        unordered_set<int> currSet;
        for (int j = 0; j < groups[groupId].size(); j++) {
            // merge
            for (auto k : trace[groups[groupId][j]]) {
                currSet.insert(k);
            }
        }
        finAns[groupId] = currSet.size();
        continue;
    }

    for (int i = 1; i <= N; i++) {
        cout << max(finAns[myGroup[i]], 1) << endl;
    }

}