UVA 10973 - Triangle Counting
来源:互联网 发布:网络空间 概念股 编辑:程序博客网 时间:2024/06/05 08:19
一直TLE,看了报告才写出来
方法如下:
for x=1..n, adj[x] is the set { y: y>x and (x,y) is in E }.
w[1..n] is an array of integers, initially filled with zeroes
count = 0
for x = 1 to n:
for each y in adj[x]: w[y] = 1
for each y in adj[x]:
for each z in adj[y]:
count += w[z]
for each y in adj[x]: w[y] = 0
return count
#include <iostream>#include <memory.h>#include <vector>#include <algorithm>#include <cstdio>#include <map>using namespace std;#define MAX 3001struct Edge{int v;Edge *next;Edge(int vv=0,Edge* nnext=NULL):v(vv),next(nnext){}}edges[MAX];int w[MAX];int n,m,ans;void addEdge(int u,int v){Edge *ne=&edges[u];while (ne->next)ne=ne->next;ne->next=new Edge(v,0);}int main(){int t;scanf("%d",&t);while (t--){scanf("%d%d",&n,&m);ans=0;memset(w,0,sizeof(w));memset(edges,0,sizeof(edges));for (int i=0;i<m;++i){int u,v;scanf("%d%d",&u,&v);if(u<v)addEdge(u,v);if(v<u)addEdge(v,u);}for (int x=1;x<=n;++x){Edge * ne;for (ne=edges[x].next;ne;ne=ne->next)w[ne->v]=1;//把所有x能连到的边标为1for(ne=edges[x].next;ne;ne=ne->next){Edge *ne2=edges[ne->v].next;for (;ne2;ne2=ne2->next)ans+=w[ne2->v];//对于每一个x能连接到的点y,遍历y的所有连接点,如果w[y的每个点]为1的话,说明这个点和x也连着 构成了triple}for (ne=edges[x].next;ne;ne=ne->next)w[ne->v]=0;//把所有x能连到的边标为0}printf("%d\n",ans);}return 0;}
- UVA 10973 - Triangle Counting
- UVA 11401 Triangle Counting
- Uva 11401 - Triangle Counting
- UVA 11401 Triangle Counting
- UVA 11401 - Triangle Counting
- UVA 11401 - Triangle Counting
- uva 11401 - Triangle Counting
- UVA - 11401 Triangle Counting
- UVA&&NYoj Triangle Counting
- UVA 11401 Triangle Counting
- UVA - 11401 Triangle Counting
- uva 11401 Triangle Counting
- Triangle Counting+uva+数学题
- UVa 11401 Triangle Counting
- uva 11401 Triangle Counting
- UVa 11401 - Triangle Counting
- UVA 11401Triangle Counting
- UVA 11401:Triangle Counting
- 裸机S3C6410显示控制器(4)- 显示图像到屏幕
- 第四章 Spring.Net 如何管理您的类___IObjectPostProcessor接口
- ServerSocket和Socket通讯实例
- Web开发人员的6种赚钱之道
- Hibernate 自动创建表的方法
- UVA 10973 - Triangle Counting
- SQLyog-Ultimate-10.4.2中文汉化破解版下载
- copy_to_user和copy_from_user两个函数的分析
- NSIS 脚本的基本语法
- VS2010的一个奇葩报错= =、最后。。。。。。
- VC读取EXE文件的图标并保存
- poj 3030 Nasty Hacks
- 人生之所以不同
- SP无法编译