Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 898125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:59:37+00:00 2026-05-15T14:59:37+00:00

why my program Time Limited Error? because of the sort? this is the question

  • 0

why my program Time Limited Error?
because of the sort?
this is the question
link text

#include <cstdio>
#include <cstring>

using namespace std;

int map[22][44];
int book[22];
int total;
int sum;
int way[22];
int tails[22]; 
int tail; 

void init()
{
 memset(map,0,sizeof(map));
 memset(book,0,sizeof(book));
 sum =0;
 memset(way,0,sizeof(way));
 way[1]=1;
 memset(tails,0,sizeof(tails));
}

void sort()
{
 int t;
 for (int i=1;i<=22;i++)
  {
   if (tails[i]==0)
    break;
   else
    {
     for (int j=1;j<=tails[i]-1;j++)
      for (int k=j+1;k<=tails[i];k++)
       {
        if (map[i][j] > map[i][k])
         {
          t = map[i][j];
          map[i][j]=map[i][k];
          map[i][k]=t;
         }
       } 
    }
  }
}

void dfs(int x,int y)
{
 if ((x < 1)||(x > 22))
  return;
 if (book[x]==1)
  return;
 //printf("%d \n",x);
 if (x == total)
  {
   sum++;
   for (int i=1;i<=y-1;i++)
   {
    printf("%d ",way[i]);
   }
   printf("%d",total);
   printf("\n");
   return;
  }
 tail = tails[x];
 for (int i=1;i<=43;i++)
  {
   book[x]=1;
   way[y]=x;
   dfs(map[x][i],y+1);
   book[x]=0;
  } 
}

int main()
{
 int temp1,temp2;
 //freopen("ex.in","r",stdin);
 //freopen("ex.out","w",stdout);
 int c = 0;
 while(scanf("%d",&total)!=EOF)
 {
 c++;
 printf("CASE ");
 printf("%d",c);
 printf(":");
 printf("\n");
 init();
 for (;;)
  {
   scanf("%d%d",&temp1,&temp2);
   if ((temp1 == 0)&&(temp2 == 0))
    break;
   else
    {
     tails[temp1]++;
     tail = tails[temp1];
     map[temp1][tail]=temp2;
     tails[temp2]++;
     tail = tails[temp2];
     map[temp2][tail]=temp1; 
    }
  }
  sort();
  dfs(1,1);
  printf("There are ");printf("%d",sum);printf(" routes from the firestation to streetcorner ");printf("%d",total);printf(".");
  printf("\n");
 }
 return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T14:59:38+00:00Added an answer on May 15, 2026 at 2:59 pm

    Because your sorting algoritm is in worst-case O(n*n), you can use InnoSort for better worst-case complexity O(n*log(n)).

    You are using C++ then use sort function from <algorithm> header to do this simplest.

    Documentation you can find at http://www.sgi.com/tech/stl/sort.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Alright, quick question. A server is running in Eastern Time. PHP program needs to
I'm writing a simple time tracking program to manage my own projects. I'm a
Here is the story. I was doing a program, every time the program is
I'm writing an image processing program to perform real time processing of video frames.
I need a very accurate way to time parts of my program. I could
I've had a tough time setting up my replication server. Is there any program
Years ago I released a program called Banshee Screamer Alarm and at the time
I have a clock feature in a VB.NET program that displays the time including
I ran gprof on a C++ program that took 16.637s , according to time()
I hear people writing these programs all the time and I know what they

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.