|
|
back to boardHelp! What wrong with my source in C. Idental program in pascal got TLE(20), but C program got WA(1). C++ Program: #include <stdio.h> int main() { long st[40005], n, l, a, b, i, j;
for (i = 0; i < 40005; ++i) st[i] = -2;
scanf("%ld", &n); for (i = 0; i < n; ++i) { scanf("%ld%ld", &a, &b); st[a] = b; }
scanf("%ld", &l); for (j = 0; j < l; ++j) {
scanf("%ld%ld", &a, &b);
if (j > 0) printf("\n"); i = a; while (i >= 0) { i = st[i]; if (i == b) break; } if (i == b) {printf("2"); continue;} i = b; while (i >= 0) { i = st[i]; if (i == a) break; } if (i == a) {printf("1"); continue;}
printf("0"); }
return 0; } Pascal Programm: var l, i, j, n, a, b: longint; st: array[0..40000] of longint; begin for i := 0 to 40000 do st[i] := -2; readln(n); for i := 1 to n do begin readln(a, b); st[a] := b; end; readln(l); for j := 1 to l do begin readln(a, b); i := a; while i >= 0 do begin i := st[i]; if (i = b) then break; end; if i = b then begin writeln(2); continue; end; i := b; while i >= 0 do begin i := st[i]; if (i = a) then break; end; if i = a then begin writeln(1); continue; end; writeln(0); end; end. I HAVE THE SAME PROBLEM WITH MY CODE PLEASE HELP... #include <fstream> #include <iostream> using namespace std; int main(void) { long int st[40000], n, l, a, b, i, j;
cin >> n; for (i=0; i<n; i++) st[i] = -2; for (i=0; i<n; i++) { cin >> a >> b; st[a] = b; } cin >> l; for (j=0; j<l; j++) { cin >> a >> b;
i = a;
while (i >= 0 && i != b) i = st[i]; if (i == b) { cout << "2\n"; continue; } i = b;
while (i >= 0 && i != a) i = st[i];
if (i == a) { cout << "1\n"; continue; } cout << "0\n"; } return 0; } I correct upper bound of array but again WA on test 1... what the fu.k....???? #include <fstream> #include <iostream> using namespace std; int main(void) { long int st[40001], n, l, a, b, i, j;
cin >> n; for (i=0; i<40001; i++) st[i] = -2; for (i=0; i<n; i++) { cin >> a >> b; st[a] = b; } cin >> l; for (j=0; j<l; j++) { cin >> a >> b;
i = a;
while (i >= 0 && i != b) i = st[i]; if (i == b) { cout << "2\n"; continue; } i = b;
while (i >= 0 && i != a) i = st[i];
if (i == a) { cout << "1\n"; continue; } cout << "0\n"; } return 0; } |
|
|