hojx triangles(题号之后补

description
There are n points given in a space. There are no three points, such that they lie on the same straight line.
Each pair of points is connected by a segment coloured red or black. Each triangle, whose sides have the same colour is called a monochromatic triangle. We are given a list of all red segments and we want to find the number of all monochromatic triangles.
这里写图片描述
Input
The first line is the number of test cases. For each test case, in the first line there is one integer n, 3 &lt= n &lt= 1000, which equals the number of points; in the second line there is one integer m, 0 &lt= m &lt= 250000, which equals the number of red segments; then the following m lines there are two integers p and k separated by a single space, 1 &lt= p, k &lt= n, they are vertices which are ends of a red segment.
Output
For each test case ,print the correct solution in a single line.
Sample Input
1
6
9
1 2
2 3
2 5
1 4
1 6
3 4
4 5
5 6
3 6
Sample Output
2
题解:一个不符合条件的三角形的充要条件这个三角形里有两个点引出的边的颜色是不一样的。因此我们统计这样的点,除以二就是不符合条件的三角形个数。
(最近发现英语是考察课。。。美滋滋但四级挂了QAQ

#include <bits/stdc++.h>
#define N 1005
using namespace std;
int T,n,m,ans;
int r[N];
int main(){
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    scanf("%d", &T);
    while(T--){
        scanf("%d%d", &n, &m);
        memset(r, 0, sizeof(r));
        ans = 0;
        for(int i = 1,a,b; i <= m; i++){
            scanf("%d%d", &a, &b);
            r[a]++;
            r[b]++;
        }
        for(int i = 1; i <= n; i++)
            ans += r[i] * (n - 1 - r[i]);
        ans = n * (n - 1) * (n - 2) / 6 - ans / 2;
        printf("%d\n", ans);
    }
    return 0;
}
发布了87 篇原创文章 · 获赞 7 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/yxr0105/article/details/78863932