#include <bits/stdc++.h> #define dbg(x) " [" << #x << ": " << (x) << "] " using namespace std; template<typename A,typename B> ostream& operator<<(ostream& out,const pair<A,B>& p) { return out << "(" << p.first << ", " << p.second << ")"; } template<typename T> ostream& operator<<(ostream& out,const vector<T>& c) { out << "{"; for(auto it = c.begin(); it != c.end(); it++) { if(it != c.begin()) out << ", "; out << *it; } return out << "}"; } vector<vector<int>> g; vector<int> u; vector<int> ck; int dfs(int v,int frb) { if(ck[v] == 1) { return 1; } else if(ck[v] == 2) { return 0; } u[v] = 1; for(int to : g[v]) { if(to != frb) { if(ck[to] == 1) { ck[v] = 1; return 1; } else if(ck[to] == 2) { continue; } else { if(u[to] == 1) { ck[v] = 1; return 1; } else if(!u[to]) { dfs(to,frb); if(ck[to] == 1) { ck[v] = 1; return 1; } } } } } u[v] = 2; ck[v] = 2; return 0; } void solve() { int n; cin >> n; vector<vector<int>> in(n),out(n); vector<int> gr(n); g.resize(n + 1); g[n].push_back(n); u.resize(n + 1); ck.resize(n + 1); for(int i = 0; i < n; i++) { int c; cin >> c; for(int j = 0; j < c; j++) { int to; cin >> to; to--; if(to <= i) { out[i].push_back(to); } else { in[to].push_back(i); gr[i] = max(gr[i],to); } } } for(long long k = 0; k < n; k++) { for(int to : out[k]) { g[k].push_back(to); } for(int from : in[k]) { g[from].push_back(k); } for(int i = 0; i <= k; i++) { if(!g[i].size() || gr[i] > k) { g[i].push_back(n); } } long long ans = (k + 1) * k; for(int i = 0; i <= k; i++) { for(int j = 0; j <= k; j++) { u[j] = 0; ck[j] = 0; } ck[n] = 0; u[n] = 0; for(int j = 0; j <= k; j++) { if(i != j) { ans -= dfs(j,i); } } } for(int i = 0; i <= k; i++) { if(g[i].back() == n) { g[i].pop_back(); } } cout << ans << " "; } cout << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int testNum = 1; //cin >> testNum; for(int i = 1; i <= testNum; i++) { solve(); } 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 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 119 120 121 122 | #include <bits/stdc++.h> #define dbg(x) " [" << #x << ": " << (x) << "] " using namespace std; template<typename A,typename B> ostream& operator<<(ostream& out,const pair<A,B>& p) { return out << "(" << p.first << ", " << p.second << ")"; } template<typename T> ostream& operator<<(ostream& out,const vector<T>& c) { out << "{"; for(auto it = c.begin(); it != c.end(); it++) { if(it != c.begin()) out << ", "; out << *it; } return out << "}"; } vector<vector<int>> g; vector<int> u; vector<int> ck; int dfs(int v,int frb) { if(ck[v] == 1) { return 1; } else if(ck[v] == 2) { return 0; } u[v] = 1; for(int to : g[v]) { if(to != frb) { if(ck[to] == 1) { ck[v] = 1; return 1; } else if(ck[to] == 2) { continue; } else { if(u[to] == 1) { ck[v] = 1; return 1; } else if(!u[to]) { dfs(to,frb); if(ck[to] == 1) { ck[v] = 1; return 1; } } } } } u[v] = 2; ck[v] = 2; return 0; } void solve() { int n; cin >> n; vector<vector<int>> in(n),out(n); vector<int> gr(n); g.resize(n + 1); g[n].push_back(n); u.resize(n + 1); ck.resize(n + 1); for(int i = 0; i < n; i++) { int c; cin >> c; for(int j = 0; j < c; j++) { int to; cin >> to; to--; if(to <= i) { out[i].push_back(to); } else { in[to].push_back(i); gr[i] = max(gr[i],to); } } } for(long long k = 0; k < n; k++) { for(int to : out[k]) { g[k].push_back(to); } for(int from : in[k]) { g[from].push_back(k); } for(int i = 0; i <= k; i++) { if(!g[i].size() || gr[i] > k) { g[i].push_back(n); } } long long ans = (k + 1) * k; for(int i = 0; i <= k; i++) { for(int j = 0; j <= k; j++) { u[j] = 0; ck[j] = 0; } ck[n] = 0; u[n] = 0; for(int j = 0; j <= k; j++) { if(i != j) { ans -= dfs(j,i); } } } for(int i = 0; i <= k; i++) { if(g[i].back() == n) { g[i].pop_back(); } } cout << ans << " "; } cout << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int testNum = 1; //cin >> testNum; for(int i = 1; i <= testNum; i++) { solve(); } return 0; } |