Page 1 of 3
10973 - Triangle Counting
Posted: Fri Nov 11, 2005 11:56 pm
by Sanny
I couldn't find anything better than O(VE) in this problem. Can anybody explain a better algo please?
Regards
Sanny
Posted: Sat Nov 12, 2005 8:51 am
by filigabriel
me neither.
I just calculated by frute force

with some optimizations. My algo depends on number of vertices. Since there isnt any edge into more than 2 triangles, you know there is no more edges than 3 times number of nodes. In general, it's better an algo O(3n*k) than O(n^2 * k).
Wrong Solution
Posted: Sat Nov 12, 2005 10:39 am
by arahimi
Your assumption that the number of edges in such a graph is less than 3*V is wrong. Consider a complete bi-partite graph of V vertices with V/2 vertices on each part. This graph does not have any triangles and has V^2/4 edges.
Posted: Sat Nov 12, 2005 4:13 pm
by Sanny
Has anybody got AC in this problem using STL to keep the edge list or adjacency list?
Regards
Sanny
Posted: Sat Nov 12, 2005 5:18 pm
by filigabriel
Your assumption that the number of edges in such a graph is less than 3*V is wrong
I have to accept you are allright. Very nice sample. When i solved this problem, i was thinking what was the worst case for number of triangules

. Well, at least my algo runs on time. I didn't expect such a case.
Thank you, Arahimi.
Posted: Sun Nov 13, 2005 10:28 am
by Wei-Ming Chen
I got Runtime error...... why?
I used bool[3001][3001], is this too big?
Posted: Sun Nov 13, 2005 10:35 am
by angga888
Wei-Ming Chen wrote:I used bool[3001][3001], is this too big?
No, my solution also uses such array.

Posted: Sun Nov 13, 2005 10:51 am
by Wei-Ming Chen
This is my code. Is where wrong?
I don't like RTE....
#include <stdio.h>
int main()
{
bool a[3001][3001];
int b,c,d,e,f,g,h;
Posted: Sun Nov 13, 2005 11:02 am
by Sanny
bool a[3001][3001] is too big for stack memory. Declare this as global and you'll be fine.
Regards
Sanny
Posted: Sun Nov 13, 2005 11:07 am
by Wei-Ming Chen
You are right
I got Ac, thank you
10973 - Triangle Counting
Posted: Thu Nov 24, 2005 3:58 pm
by h001122
Code: Select all
#include <iostream>
#include <cstdio>
using namespace std;
bool adj[3001][3001] = {false};
// Graph will be transformed into adjacently matrix.
void reset(); // Reset after each test case.
int main()
{
int t, n, m, a, b, i, j, k;
cin >> t;
while(t--) {
int triangle = 0; // The number of triangle
scanf("%d %d", &n, &m);
for(i = 1; i <= m; i++) {
scanf("%d %d", &a, &b);
adj[a][b] = true;
adj[b][a] = true;
}
// Count triangle in inputed graph.
for(i = 1; i <= m; i++) {
for(j = i + 1; j <= m; j++) {
for(k = j + 1; k <= m; k++) {
if(i != j && i != k && j != k) {
// Are I, J and K connected?
if(adj[i][j] && adj[j][k] && adj[i][k]) {
triangle++;
}
}
}
}
}
cout << triangle << endl;
}
return 0;
}
void reset()
{
int i, j;
for(i = 1; i <= 3000; i++) {
for(j = 1; j <= 3000; j++) {
adj[i][j] = false;
}
}
}
I got Runtime Error.
I think 'bool adj[3001][3001]' isn't such big that it raises stack overflow.
Hmm... I don't know why 'Runtime Error'. Please help me!
Posted: Thu Nov 24, 2005 4:00 pm
by Adrian Kuegel
Your for-loops should go up to n, not to m.
Btw, you will get TLE with your method.
Posted: Sat Dec 31, 2005 1:41 pm
by SRX
Adrian Kuegel wrote:Your for-loops should go up to n, not to m.
Btw, you will get TLE with your method.
Can someone give some test cases , thanks .
I can't find my error

Posted: Sat Dec 31, 2005 2:00 pm
by Moha
What is your problem? WA or TLE?
the worst case for TLE is a bipartite graph. it has no triangle.
you can write a generator for this kind of input.
Posted: Sat Dec 31, 2005 2:14 pm
by SRX
Moha wrote:What is your problem? WA or TLE?
the worst case for TLE is a bipartit graph. it has no triangle.
you can write a generator for this kind of input.
sorry , I got wa , I think my method won't get tle