#include <cstdio> #include <algorithm> #include <vector> #include <queue> using namespace std; using ll = long long int; const int MAXN = 200010; int n; vector<int> outEdges[MAXN], inEdges[MAXN]; ll npairs[MAXN]; bool source[MAXN]; int outdeg[MAXN]; int marked[MAXN]; int markedc = 0; void test(int sink) { for (int i = 0; i < markedc; i++) { source[marked[i]] = false; outdeg[marked[i]] = outEdges[marked[i]].size(); for (auto v : inEdges[marked[i]]) { outdeg[v] = outEdges[v].size(); } } markedc = 0; priority_queue<int> todo; todo.push(-sink); int maxk = sink; while (!todo.empty()) { int t = -todo.top(); todo.pop(); source[t] = true; marked[markedc] = t; markedc++; maxk = max(maxk, t); npairs[maxk]++; for (auto pred : inEdges[t]) { if (!source[pred]) { outdeg[pred]--; if (outdeg[pred] == 0) { todo.push(-pred); } } } } } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { int l; scanf("%d", &l); while (l--) { int x; scanf("%d", &x); outEdges[i].push_back(x); } sort(outEdges[i].begin(), outEdges[i].end()); auto last = unique(outEdges[i].begin(), outEdges[i].end()); outEdges[i].erase(last, outEdges[i].end()); for (auto x : outEdges[i]) { inEdges[x].push_back(i); } } for (int v = 1; v <= n; v++) { outdeg[v] = outEdges[v].size(); } for (int v = 1; v <= n; v++) { test(v); } ll out = 0; for (int k = 1; k <= n; k++) { out += npairs[k] - 1; printf("%lld ", out); } puts(""); }
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 | #include <cstdio> #include <algorithm> #include <vector> #include <queue> using namespace std; using ll = long long int; const int MAXN = 200010; int n; vector<int> outEdges[MAXN], inEdges[MAXN]; ll npairs[MAXN]; bool source[MAXN]; int outdeg[MAXN]; int marked[MAXN]; int markedc = 0; void test(int sink) { for (int i = 0; i < markedc; i++) { source[marked[i]] = false; outdeg[marked[i]] = outEdges[marked[i]].size(); for (auto v : inEdges[marked[i]]) { outdeg[v] = outEdges[v].size(); } } markedc = 0; priority_queue<int> todo; todo.push(-sink); int maxk = sink; while (!todo.empty()) { int t = -todo.top(); todo.pop(); source[t] = true; marked[markedc] = t; markedc++; maxk = max(maxk, t); npairs[maxk]++; for (auto pred : inEdges[t]) { if (!source[pred]) { outdeg[pred]--; if (outdeg[pred] == 0) { todo.push(-pred); } } } } } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { int l; scanf("%d", &l); while (l--) { int x; scanf("%d", &x); outEdges[i].push_back(x); } sort(outEdges[i].begin(), outEdges[i].end()); auto last = unique(outEdges[i].begin(), outEdges[i].end()); outEdges[i].erase(last, outEdges[i].end()); for (auto x : outEdges[i]) { inEdges[x].push_back(i); } } for (int v = 1; v <= n; v++) { outdeg[v] = outEdges[v].size(); } for (int v = 1; v <= n; v++) { test(v); } ll out = 0; for (int k = 1; k <= n; k++) { out += npairs[k] - 1; printf("%lld ", out); } puts(""); } |