Page 1 of 3

### 10973 - Triangle Counting

Posted: Fri Nov 11, 2005 11:56 pm
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
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
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
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
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
I got Runtime error...... why?
I used bool, is this too big?

Posted: Sun Nov 13, 2005 10:35 am
Wei-Ming Chen wrote:I used bool, is this too big?
No, my solution also uses such array. Posted: Sun Nov 13, 2005 10:51 am
This is my code. Is where wrong?
I don't like RTE....

#include <stdio.h>
int main()
{
bool a;
int b,c,d,e,f,g,h;

Posted: Sun Nov 13, 2005 11:02 am
bool a 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
You are right
I got Ac, thank you

### 10973 - Triangle Counting

Posted: Thu Nov 24, 2005 3:58 pm

Code: Select all

``````#include <iostream>
#include <cstdio>

using namespace std;

// 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);
}

// 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?
triangle++;
}
}
}
}
}

cout << triangle << endl;
}

return 0;
}

void reset()
{
int i, j;

for(i = 1; i <= 3000; i++) {
for(j = 1; j <= 3000; j++) {
}
}
}
``````
I got Runtime Error.
I think 'bool adj' isn't such big that it raises stack overflow.

Posted: Thu Nov 24, 2005 4:00 pm
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
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
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
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