How Many Triangles HDU - 5784

主要在这个双指针扫描法上,它的意思是说把左右边界找到,然后在这个区间内的就是符合条件的,(据说比二分优越)

这个开2倍边界挣扎了我好久.

代码是一个钝角/直角贡献了两个锐角的办法,锐角-2*钝/直 再/3就是数量

也可以用 锐角/3 - 钝/直

复杂度(n^2)*log(n)

还有就是这个EPS,根据dls的直播课所说现在x和y上界是1e9,atan2(1e9,1)和atan2(1e9-1,1)的精度是在1/(1e9^2)也就是1e-18级别.

EPS别开的太小.

 

#include <iostream>
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef double DB;
const DB PI = acos(-1.0);
const DB EPS = 1e-13;
int n;
struct NODE
{
    DB x,y;
} a[2003];
DB sign(DB x)
{
    if(abs(x)<= EPS)
        return 0;
    return x<0?-1:1;
}
double node[4003];
int CNT(DB A)
{
    int r = 1,res = 0;
    for(int l=1; l<n; l++)
    {
        int ecnt = 0;
        while( sign(node[l+ecnt]-node[l]) == 0)++ecnt;
        l = l+ecnt-1;//最后一个共线的点
        r = max(l+1,r);//开始第一个
        while(r-l+1 < n && sign(node[r]-node[l]-A)<0)r++;
        res += (r-l-1)*ecnt;
    }
    return res;
}
int main()
{
//    freopen("in.txt","r",stdin);
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);

    while(cin>>n)
    {
        for(int i=1; i<=n; i++)
            cin>>a[i].x>>a[i].y;
        LL ans = 0,notans = 0;
        for(int i=1; i<=n; i++)
        {
            int cnt = 0;
            for(int j=1; j<=n; j++)
            {
                if(i == j)continue;
                node[++cnt] = atan2(a[i].y-a[j].y,a[i].x-a[j].x);
            }
            sort(node+1,node+cnt+1);
            for(int j=cnt+1; j<=2*cnt; j++)
                node[j] = node[j-cnt]+2*PI;//[1,cnt]是在-PI到PI,判定的时候要求差值小于PI,所以+2*PI,变成了[PI,3PI],这样边界就接上了

            int res = CNT(PI/2);
            ans += res;
            notans += CNT(PI)-res;
        }
        cout<<(ans-2*notans)/3<<endl;
    }
}

 

 

In Python, calculating triangles in a multigraph (a graph that can have multiple edges between any two vertices) is a bit more complex than in a simple graph, since you need to account for all possible combinations of three nodes that are connected. Here's an outline using NetworkX library, which provides a convenient way to handle graphs: ```python import networkx as nx # Assuming you have a MultiGraph 'G' G = nx.MultiGraph() # Replace this line with your actual multigraph data def count_triangles(graph): triangle_count = 0 for node1, neighbors1 in graph.adjacency_iter(): # Iterate over nodes and their neighbors for node2 in neighbors1: # For each neighbor of node1 common_neighbors = set(graph.neighbors(node1)) & set(graph.neighbors(node2)) # Find common neighbors if len(common_neighbors) >= 2: # If at least two nodes share a neighbor triangle_count += len(common_neighbors) # Count the shared neighbors as potential triangles return triangle_count // 6 # Since each shared edge contributes to 6 triangles, divide by 6 for uniqueness triangle_count = count_triangles(G) print(f"The number of triangles in the multigraph is {triangle_count}.") ``` This code iterates over all pairs of neighboring nodes and checks if they share at least two common neighbors. It counts these shared neighbors and then divides the total by 6 because each unique triangle contributes three shared edges. Note that this method has a time complexity of O(V^3), where V is the number of vertices in the graph, so it may not be efficient for very large graphs.
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值