|
|
back to boardGetting TLE40 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; } |
|
|