Link: Ultraviolet A 1510-neon sign
Given N points, any three points are not collocated, and there is a line between the two points to give the color of the line. Ask how many triangles have the same color on the three sides.
Solution: For each vertex, record the number of black edges and the number of red edges of the vertex. Consider the triangle with the vertex as the vertex, select one from the red edge, and choose one from the Black edge, the triangle may not be satisfied. Because a triangle of different colors is filled with two vertices, this is considered twice. You can use the total number to remove different colors.
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;typedef long long ll;const int maxn = 1005;ll n, r[maxn], l[maxn];void init () { scanf("%lld", &n); int x; memset(l, 0, sizeof(l)); memset(r, 0, sizeof(r)); for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { scanf("%d", &x); if (x) { r[i]++; r[j]++; } else { l[i]++; l[j]++; } } }}ll solve () { ll sum = (ll)n * (n-1) * (n-2) / 6; ll del = 0; for (int i = 0; i < n; i++) del += l[i] * r[i]; return sum - del / 2;}int main () { int cas; scanf("%d", &cas); while (cas--) { init(); printf("%lld\n", solve()); } return 0;}