10608 : why this code gives T.L.E
Posted: Fri Sep 17, 2004 8:36 pm
Using this algo i got A.C for 459 , 793 ,10227 . But why TLE for 10608
Can anyone help?
thanks in advance.
Can anyone help?
thanks in advance.
Using Adjacency matrix will certainly get MLE/TLE, but the usage of adjacency list will suffice. There might be 30000 different vertices but there wont be cases where you have to handle 30000*30000 memory.tomal wrote: Both the adjacency matrix and adjacency list representation may lead to a 30000*30000 memory requirements as there are 30000 vertexes (30000 people);
Code: Select all
for(k=1;k<=n;k++)if(a[k]==a[j])a[k]=a[i];
Code: Select all
/*Q10608: Friends*/
/*2005.5.25 W.A.*/
#include<stdio.h>
int main()
{
int i,j,n,a,b,c[30001],x,y,num,t[30001],max;
scanf("%d",&n);
while(n--){
for(i=0;i<30001;i++){ c[i]=0; t[i]=0; }
scanf("%d %d",&a,&b);
num=0;
while(b--){
scanf("%d %d",&x,&y);
if(c[x]){
if(!c[y]){c[y]=c[x]; t[c[x]]++;}
else if(c[x]!=c[y]){
t[c[x]]+=t[c[y]];
for(j=1;j<=a;j++)
if(c[j]==c[y]) c[j]=c[x];
}
}
else{
if(c[y]){
if(!c[x]){c[x]=c[y]; t[c[y]]++;}
else if(c[x]!=c[y]){
t[c[y]]+=t[c[x]];
for(j=1;j<=a;j++)
if(c[j]==c[x]) c[j]=c[y];
}
}
else{
num++;
c[x]=c[y]=num; t[num]=2;
}
}
}
max=0;
for(i=1;i<=num;i++)
if(t[i]>max) max=t[i];
printf("%d\n",max);
}
return 0;
}
Code: Select all
#include <cstdio>
#include <cstdlib>
int parent[30001];
int count[30001];
int main() {
int t, m, n, a, b, Max;
scanf("%d\n", &t);
for (int i=0;i<t;i++) {
scanf("%d %d\n", &m, &n);
for (int j=1;j<=m;j++) {
parent[j] = j;
count[j] = 0;
}
for (int j=0;j<n;j++) {
scanf("%d %d\n", &a, &b);
while (parent[a] != a) {
a = parent[a];
}
while (parent[b] != b) {
b = parent[b];
}
if (a<b) {
parent[b] = a;
}
else {
parent[a] = b;
}
}
for (int j=1;j<=m;j++) {
count[parent[j]]++;
}
/*for (int j=1;j<=m;j++) {
printf("%d ", count[j]);
}
putchar('\n');*/
Max = 0;
for (int j=1;j<=m;j++) {
if (count[j] > Max) {
Max = count[j];
}
}
printf("%d\n", Max);
}
}
Code: Select all
1
10 5
1 2
2 3
4 5
5 7
3 4
Code: Select all
6