#include <cstdio>
#include <vector>
#include <set>
#include <cstring>
#define MAX_N 200010
using namespace std;
int initialScore[MAX_N], x, n, deducted[MAX_N];
bool ign[MAX_N];
vector<int> inClause[MAX_N];
long long wyn[MAX_N];
int queueSpace[2 * MAX_N];
/*
* If
* ((v1 => w1 | v1 => w2 ...) & (...) ...) => (A => B)
* is tautology then A, B is a good pair
*
* We count pairs where
* !(((v1 => w1 | v1 => w2 ...) & (...) ...) => (A => B))
* is satisfiable
*
* This form can be transformed to horn-sat:
* (v1 | !w1 | !w2 | ...) & (...) & ... & !A & B
*
* We set B and add clauses one by one, and count the number of matching As
*/
void hornSat(int B) {
memset(deducted, 0, sizeof (int) * (n + 1));
deducted[B] = initialScore[B];
int trueN = 0;
for (int cl = B; cl <= n; cl++) {
if (initialScore[cl] == deducted[cl]) {
int qs = 0, qe = 1;
queueSpace[qs] = cl;
while(qs != qe) {
trueN++;
int a = queueSpace[qs];
qs++;
for (int nCl : inClause[a]) {
if (ign[nCl]) continue;
deducted[nCl]++;
if (initialScore[nCl] == deducted[nCl] && nCl <= cl) {
queueSpace[qe] = nCl;
qe++;
}
}
}
}
wyn[cl] += trueN - 1;
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &initialScore[i]);
set<int> got;
int dec = 0;
for (int j = 0; j < initialScore[i]; j++) {
scanf("%d", &x);
if (x == i) {
ign[i] = true;
}
if (got.find(x) == got.end()) {
inClause[x].push_back(i);
} else {
dec++;
}
}
initialScore[i] -= dec;
}
for (int b = 1; b <= n; b++) {
hornSat(b);
}
for (int i = 1; i <= n; i++) {
printf("%lld ", wyn[i]);
}
printf("\n");
}
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 | #include <cstdio> #include <vector> #include <set> #include <cstring> #define MAX_N 200010 using namespace std; int initialScore[MAX_N], x, n, deducted[MAX_N]; bool ign[MAX_N]; vector<int> inClause[MAX_N]; long long wyn[MAX_N]; int queueSpace[2 * MAX_N]; /* * If * ((v1 => w1 | v1 => w2 ...) & (...) ...) => (A => B) * is tautology then A, B is a good pair * * We count pairs where * !(((v1 => w1 | v1 => w2 ...) & (...) ...) => (A => B)) * is satisfiable * * This form can be transformed to horn-sat: * (v1 | !w1 | !w2 | ...) & (...) & ... & !A & B * * We set B and add clauses one by one, and count the number of matching As */ void hornSat(int B) { memset(deducted, 0, sizeof (int) * (n + 1)); deducted[B] = initialScore[B]; int trueN = 0; for (int cl = B; cl <= n; cl++) { if (initialScore[cl] == deducted[cl]) { int qs = 0, qe = 1; queueSpace[qs] = cl; while(qs != qe) { trueN++; int a = queueSpace[qs]; qs++; for (int nCl : inClause[a]) { if (ign[nCl]) continue; deducted[nCl]++; if (initialScore[nCl] == deducted[nCl] && nCl <= cl) { queueSpace[qe] = nCl; qe++; } } } } wyn[cl] += trueN - 1; } } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &initialScore[i]); set<int> got; int dec = 0; for (int j = 0; j < initialScore[i]; j++) { scanf("%d", &x); if (x == i) { ign[i] = true; } if (got.find(x) == got.end()) { inClause[x].push_back(i); } else { dec++; } } initialScore[i] -= dec; } for (int b = 1; b <= n; b++) { hornSat(b); } for (int i = 1; i <= n; i++) { printf("%lld ", wyn[i]); } printf("\n"); } |
English