ICode9

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

B - Triangle (Easier)

2022-09-10 12:02:01  阅读:220  来源: 互联网

标签:Triangle int Vertex Sample leq edge Input Easier


Problem Statement

You are given a simple undirected graph with $N$ vertices and $M$ edges. The vertices are numbered $1, \dots, N$, and the $i$-th $(1 \leq i \leq M)$ edge connects Vertex $U_i$ and Vertex $V_i$.

Find the number of tuples of integers $a, b, c$ that satisfy all of the following conditions:

  • $1 \leq a \lt b \lt c \leq N$
  • There is an edge connecting Vertex $a$ and Vertex $b$.
  • There is an edge connecting Vertex $b$ and Vertex $c$.
  • There is an edge connecting Vertex $c$ and Vertex $a$.

Constraints

  • $3 \leq N \leq 100$
  • $1 \leq M \leq \frac{N(N - 1)}{2}$
  • $1 \leq U_i \lt V_i \leq N \, (1 \leq i \leq M)$
  • $(U_i, V_i) \neq (U_j, V_j) \, (i \neq j)$
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

$N$ $M$
$U_1$ $V_1$
$\vdots$
$U_M$ $V_M$

Output

Print the answer.


Sample Input 1

5 6
1 5
4 5
2 3
1 4
3 5
2 5

Sample Output 1

2

$(a, b, c) = (1, 4, 5), (2, 3, 5)$ satisfy the conditions.


Sample Input 2

3 1
1 2

Sample Output 2

0

Sample Input 3

7 10
1 7
5 7
2 5
3 6
4 7
1 5
2 4
1 3
1 6
2 7
用邻接矩阵存边,暴力枚举三个数,用邻接矩阵判断他们是否成三元环。
#include<cstdio>
const int N=105;
int n,m,u,v,e[N][N],cnt;
int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=m;i++)
	{
		scanf("%d%d",&u,&v);
		e[u][v]=e[v][u]=1;
	}
	for(int i=1;i<n;i++)
		for(int j=i+1;j<n;j++)
			for(int k=j+1;k<=n;k++)
				if(e[i][j]&&e[j][k]&&e[i][k])
					++cnt;
	printf("%d",cnt); 
}

标签:Triangle,int,Vertex,Sample,leq,edge,Input,Easier
来源: https://www.cnblogs.com/mekoszc/p/16676247.html

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

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

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

ICode9版权所有