#include <iostream> #include <set> #include <vector> #include <queue> using namespace std; int doDFS(vector<int>*& edges, int n, int sn) { queue<int> line; line.push(sn); bool* used = new bool[n] {0}; int* vals = new int[n] {0}; int cn, nn; while (line.size()) { cn = line.front(); line.pop(); used[cn] = true; for (int i = 0; i < edges[cn].size(); i++) { nn = edges[cn][i]; if (used[nn]) continue; vals[nn] = vals[cn] + 1; used[nn] = true; line.push(nn); } } int out = 0; for (int i = 0; i < n; i++) { out += vals[i]; } return out; } int main() { int n; cin >> n; vector<int>* edges = new vector<int>[n]; int* pos = new int[n]; int a; for (int i = 0; i < n; i++) { cin >> pos[i]; } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (j == i) continue; if (j<i && pos[j]>pos[i]) { edges[i].push_back(j); } else if (i<j && pos[i]>pos[j]) { edges[i].push_back(j); } } } for (int i = 0; i < n; i++) { cout<<doDFS(edges, n, i)<<" "; } return 0; }
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 | #include <iostream> #include <set> #include <vector> #include <queue> using namespace std; int doDFS(vector<int>*& edges, int n, int sn) { queue<int> line; line.push(sn); bool* used = new bool[n] {0}; int* vals = new int[n] {0}; int cn, nn; while (line.size()) { cn = line.front(); line.pop(); used[cn] = true; for (int i = 0; i < edges[cn].size(); i++) { nn = edges[cn][i]; if (used[nn]) continue; vals[nn] = vals[cn] + 1; used[nn] = true; line.push(nn); } } int out = 0; for (int i = 0; i < n; i++) { out += vals[i]; } return out; } int main() { int n; cin >> n; vector<int>* edges = new vector<int>[n]; int* pos = new int[n]; int a; for (int i = 0; i < n; i++) { cin >> pos[i]; } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (j == i) continue; if (j<i && pos[j]>pos[i]) { edges[i].push_back(j); } else if (i<j && pos[i]>pos[j]) { edges[i].push_back(j); } } } for (int i = 0; i < n; i++) { cout<<doDFS(edges, n, i)<<" "; } return 0; } |