ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Counting Triangles

2021-07-24 22:00:42  阅读:164  来源: 互联网

标签:int seed z2 z4 Counting z1 Triangles z3


题目描述

Goodeat finds an undirected complete graph with n vertices. Each edge of the graph is painted black or white. He wants you to help him find the number of triangles (a, b, c) (a < b < c), such that the edges between (a, b), (b, c), (c, a) have the same color. To avoid the input scale being too large, we use the following code to generate edges in the graph.

namespace GenHelper
{
    unsigned z1,z2,z3,z4,b,u;
    unsigned get()
    {
        b=((z1<<6)^z1)>>13;
        z1=((z1&4294967294U)<<18)^b;
        b=((z2<<2)^z2)>>27;
        z2=((z2&4294967288U)<<2)^b;
        b=((z3<<13)^z3)>>21;
        z3=((z3&4294967280U)<<7)^b;
        b=((z4<<3)^z4)>>12;
        z4=((z4&4294967168U)<<13)^b;
        return (z1^z2^z3^z4);
    }
    bool read() {
      while (!u) u = get();
      bool res = u & 1;
      u >>= 1; return res;
    }
    void srand(int x)
    {
        z1=x;
        z2=(~x)^0x233333333U;
        z3=x^0x1234598766U;
        z4=(~x)+51;
      	u = 0;
    }
}
using namespace GenHelper;
bool edge[8005][8005];
int main() {
  int n, seed;
  cin >> n >> seed;
  srand(seed);
  for (int i = 0; i < n; i++)
    	for (int j = i + 1; j < n; j++)
        	edge[j][i] = edge[i][j] = read();
 	return 0;
}


The edge array in the above code stores the color of the edges in the graph. edge[i][j]=1 means that the edge from i to j is black, otherwise it is white (∀0≤i≠j≤n−1\forall 0 \le i \neq j \le n-1∀0≤i​=j≤n−1).

Ensure that there is an approach that does not depend on the way the data is generated.
 

输入描述:


The first line contains two integers n(n≤8000),seed(seed≤109)n(n \le 8000), seed (seed \le 10^9)n(n≤8000),seed(seed≤109), denote the number of vertices and the seed of random generator. 

输出描述:

Output a line denoting the answer. 

示例1

输入

复制

10 114514

输出

复制

35

说明

There're 35 triangles that all three edges have the same color. 

思路:

注意到⼀个神奇的性质:每个三⻆形要么同⾊,要么有两边同⾊另⼀边异⾊。对于后者,三⻆形有恰有两个异⾊ ⻆,⽽前者没有异⾊⻆。 因此异⾊⻆数 /2 即为不符合条件的三⻆个数。⽤总数减去即可。 所以我们可以根据边的颜色先把顶点颜色记录,后面枚举计算异色个数。 而凸多边形里的三角形个数为组合问题:C(n,3)=n*(n-1)(n-2)/6  代码如下: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
namespace GenHelper
{
    unsigned z1,z2,z3,z4,b,u;
    unsigned get()
    {
        b=((z1<<6)^z1)>>13;
        z1=((z1&4294967294U)<<18)^b;
        b=((z2<<2)^z2)>>27;
        z2=((z2&4294967288U)<<2)^b;
        b=((z3<<13)^z3)>>21;
        z3=((z3&4294967280U)<<7)^b;
        b=((z4<<3)^z4)>>12;
        z4=((z4&4294967168U)<<13)^b;
        return (z1^z2^z3^z4);
    }
    bool read() {
      while (!u) u = get();
      bool res = u & 1;
      u >>= 1; return res;
    }
    void srand(int x)
    {
        z1=x;
        z2=(~x)^0x233333333U;
        z3=x^0x1234598766U;
        z4=(~x)+51;
        u = 0;
    }
}
using namespace GenHelper;
ll n,num,ans;
int seed;
ll s[8005][2];
int main(){
    cin>>n>>seed;
    srand(seed);
    for(int i=0;i<n;i++){
        for(int j=i+1;j<n;j++){
            int x=read();//1:black, 0:white
            s[i][x]++;//记录顶点颜色 
            s[j][x]++;
        }
    }
    for(int i=0;i<n;i++){
        num+=s[i][0]*s[i][1];//异色角数量 
    }
    ans=n*(n-1)*(n-2)/6-num/2;//总数:n*(n-1)*(n-2)/6-异色角数量/2 
    cout<<ans<<endl;
    return 0;
}

标签:int,seed,z2,z4,Counting,z1,Triangles,z3
来源: https://blog.csdn.net/srh20/article/details/119063939

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有