C - Peer Review Editorial by en_translator
Let \(C_i\) be the number of researchers who have no conflict of interest with researcher \(i\).
The answer for research \(i\) is the number of the size-\(3\) subsets of these \(C_i\) researchers, which is the binomial coefficient \(\binom{C_i}{3}\).
Since \(\binom{C_i}{3} = \frac{1}{6} C_i(C_i - 1)(C_i - 2)\), this value can be computed in \(O(1)\) time for any \(C_i\).
To find \(C_i\), it is sufficient to subtract from \((N - 1)\) the number of researchers who have a conflict of interest with researcher \(i\), so these can be computed in a total of \(O(N + M)\) time.
Sample code
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
int main() {
int n, m;
cin >> n >> m;
vector<ll> c(n, n - 1);
rep(i, m) {
int a, b;
cin >> a >> b;
c[a - 1]--; c[b - 1]--;
}
rep(i, n) {
ll ans = c[i] * (c[i] - 1) * (c[i] - 2) / 6;
cout << ans << " \n"[i == n - 1];
}
}
posted:
last update: