197632025-12-22 12:05:26LacikaKvHírlánccpp17Wrong answer 0/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;
   /**/
   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
1Wrong answer1ms316 KiB
subtask20/20
2Wrong answer8ms316 KiB
3Wrong answer8ms316 KiB
4Wrong answer8ms512 KiB
5Wrong answer8ms316 KiB
6Wrong answer8ms316 KiB
7Wrong answer8ms508 KiB
8Wrong answer9ms512 KiB
9Wrong answer9ms316 KiB
10Wrong answer2ms316 KiB
11Wrong answer1ms316 KiB
12Wrong answer2ms508 KiB
subtask30/18
13Wrong answer171ms2100 KiB
14Wrong answer86ms1956 KiB
15Wrong answer79ms2348 KiB
16Wrong answer79ms2612 KiB
17Wrong answer83ms4004 KiB
18Wrong answer83ms4148 KiB
19Wrong answer90ms4148 KiB
20Wrong answer82ms4148 KiB
21Wrong answer86ms7220 KiB
22Wrong answer86ms8244 KiB
subtask40/42
23Wrong answer1ms508 KiB
24Wrong answer8ms316 KiB
25Wrong answer8ms316 KiB
26Wrong answer8ms512 KiB
27Wrong answer8ms316 KiB
28Wrong answer8ms316 KiB
29Wrong answer8ms508 KiB
30Wrong answer9ms512 KiB
31Wrong answer9ms316 KiB
32Wrong answer2ms316 KiB
33Wrong answer1ms316 KiB
34Wrong answer2ms508 KiB
35Wrong answer171ms2100 KiB
36Wrong answer86ms1956 KiB
37Wrong answer79ms2348 KiB
38Wrong answer79ms2612 KiB
39Wrong answer83ms4004 KiB
40Wrong answer83ms4148 KiB
41Wrong answer90ms4148 KiB
42Wrong answer82ms4148 KiB
43Wrong answer86ms7220 KiB
44Wrong answer86ms8244 KiB
45Time limit exceeded584ms2728 KiB
46Time limit exceeded583ms2612 KiB
47Time limit exceeded583ms2812 KiB
48Time limit exceeded600ms2728 KiB
49Time limit exceeded575ms3004 KiB
50Time limit exceeded575ms2988 KiB
51Time limit exceeded575ms3080 KiB
52Time limit exceeded600ms2988 KiB
53Time limit exceeded583ms3384 KiB
54Time limit exceeded584ms4148 KiB