UV 1510-neon sign (count)

Source: Internet
Author: User

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;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.