ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1329. Galactic History

Getting TLE40
Posted by Alex_SyktSU 3 Feb 2006 21:03
If somebody know how to make my variant working faster(if it's possible), please write to nonename@narod.ru.
 I think in the worst case it needs O(n*l), but I also think  it's quite clear to understand. May be using memory will be faster, but haven't tried. Other variant is that algorithm is wrong.



  int i,j,n,l;
  int work[40005];

  cin >> n;

  work[0]=-1;
  for (i=0;i<n;i++){
    int tmp;
    cin >> tmp;
    cin >> work[tmp+1];
  }
  cin >> l;
  int a,b,fl=0;
  for (i=0;i<l;i++){
    fl=0;
    cin >> a >> b;
    int wka=a;
    int wkb=b;
    while (wka+wkb+2){
        wka=work[wka+1];
        wkb=work[wkb+1];
      if (wka==b){
        fl=2;
        break;
      }
      if (wkb==a){
        fl=1;
        break;
      }
    }
   cout << fl << endl;
  }