197682025-12-22 13:26:18LacikaKvHírlánccpp17Time limit exceeded 38/80600ms8244 KiB
#include <iostream>
#include <cstring>

using namespace std;
struct diak
{
    int tovaad;
    int maxhossz=0;
};

void KiirDiak(diak *a, int len)
{
   cout << endl;
   for (int i=1; i<len; i++) {
      cout << "{"<< a[i].tovaad <<", "<< a[i].maxhossz << "}";
      if (i<len)
         cout << ", ";
   }
   cout << endl;
}

void ResetSzamolas(bool *szamolt, int len) {
   memset(szamolt,0,(len+1)*sizeof(bool));
}

void MaximizeLoop(diak *a, int epos, int pos, int maxhossz)
{
   if (epos != pos) {
      a[pos].maxhossz = maxhossz;
      MaximizeLoop(a,epos,a[pos].tovaad,maxhossz);
   }
}


bool IsLoopStartPos(int len, diak* a, bool *szamolt, int pos)
{  int cpos = pos;
   do {
      szamolt[cpos] = true;
      cpos = a[cpos].tovaad;
   } while (!szamolt[cpos]);
   return (cpos == pos);
}

void EqualizeLoop(int len, diak* a, bool *szamolt, int spos)
{  if (IsLoopStartPos(len, a, szamolt,spos)) {
//      cout << "Kor:" << spos << endl;
      MaximizeLoop(a, spos, a[spos].tovaad, a[spos].maxhossz);
   } else {
      ResetSzamolas(szamolt,len);
//      cout << "Lepjel:" << a[spos].tovaad << endl;
      EqualizeLoop(len, a, szamolt, a[spos].tovaad);
   }
}

int lanchossz (diak *e, int len, diak *a, bool *szamolt, int pos, int *ringpos)
{
   if (a[pos].maxhossz) {
//      cout << endl;
      return a[pos].maxhossz;
   }
   if (szamolt[pos]) {
      *ringpos = pos;
   } else {
      szamolt[pos] = true;
//      cout << pos << "->" << a[pos].maxhossz << ", ";
      a[pos].maxhossz = lanchossz (e, len, a, szamolt, a[pos].tovaad,ringpos) + 1;
      if (e->maxhossz < a[pos].maxhossz) {
         e->maxhossz = a[pos].maxhossz;
         e->tovaad = pos;
      }
//      cout << pos << "->" << a[pos].maxhossz << ", ";
   }
   return a[pos].maxhossz;
}
void hirmax (diak *a, int len)
{  int i,pos,maxh=0;
   for (i=1; i<len; i++) {
      if (a[i].maxhossz>maxh) {
         maxh=a[i].maxhossz;
         pos=i;
      }
   }
   cout<<pos<<" "<<maxh<<endl;
}

int main()
{
   diak eredm;
   int N;
   std::ios_base::sync_with_stdio (false); // Supposedly makes I/O faster
   /**/
   cin>>N;
   diak a[N+1];
   for (int i=1; i<=N; i++)
   {
     cin>>a[i].tovaad;
   }
   /*/
   N = 11;
   diak a[N+1] = { {0,0}, {3,0}, {1,0}, {2,0}, {3,0}, {2,0}, {5,0}, {6,0}, {5,0}, {10,0}, {11,0}, {10,0} };

   /**/
   bool szamolt[N+1];
   eredm.tovaad = 0;
   eredm.maxhossz = 0;
   int ringpos = 0;
//   KiirDiak(a, N);
   for (int i=1; i<=N; i++) {
      if (a[i].maxhossz == 0) {
//         cout << endl << "Elem:" << i << endl;
         ResetSzamolas(szamolt,N);
         lanchossz(&eredm, N+1, a, szamolt, i,&ringpos);
         MaximizeLoop(a, ringpos, a[ringpos].tovaad, a[ringpos].maxhossz);
//         KiirDiak(a, N+1);
//         cout << endl;
      }
   }
   cout<<eredm.tovaad<<" "<<eredm.maxhossz<<endl;
   return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted1ms316 KiB
subtask220/20
2Accepted2ms316 KiB
3Accepted2ms500 KiB
4Accepted2ms316 KiB
5Accepted2ms552 KiB
6Accepted1ms316 KiB
7Accepted1ms316 KiB
8Accepted2ms316 KiB
9Accepted3ms508 KiB
10Accepted1ms356 KiB
11Accepted1ms316 KiB
12Accepted1ms432 KiB
subtask318/18
13Accepted92ms2172 KiB
14Accepted41ms1988 KiB
15Accepted39ms2360 KiB
16Accepted41ms2508 KiB
17Accepted43ms3892 KiB
18Accepted43ms4148 KiB
19Accepted43ms4148 KiB
20Accepted45ms4148 KiB
21Accepted48ms7220 KiB
22Accepted46ms8244 KiB
subtask40/42
23Accepted1ms316 KiB
24Accepted2ms316 KiB
25Accepted2ms500 KiB
26Accepted2ms316 KiB
27Accepted2ms552 KiB
28Accepted1ms316 KiB
29Accepted1ms316 KiB
30Accepted2ms316 KiB
31Accepted3ms508 KiB
32Accepted1ms356 KiB
33Accepted1ms316 KiB
34Accepted1ms432 KiB
35Accepted92ms2172 KiB
36Accepted41ms1988 KiB
37Accepted39ms2360 KiB
38Accepted41ms2508 KiB
39Accepted43ms3892 KiB
40Accepted43ms4148 KiB
41Accepted43ms4148 KiB
42Accepted45ms4148 KiB
43Accepted48ms7220 KiB
44Accepted46ms8244 KiB
45Time limit exceeded578ms2180 KiB
46Time limit exceeded507ms2192 KiB
47Time limit exceeded544ms2096 KiB
48Time limit exceeded518ms2100 KiB
49Time limit exceeded587ms2612 KiB
50Time limit exceeded586ms2868 KiB
51Time limit exceeded583ms2868 KiB
52Time limit exceeded600ms3008 KiB
53Time limit exceeded588ms3636 KiB
54Time limit exceeded592ms4148 KiB