Pagini recente » Cod sursa (job #282754) | Cod sursa (job #1573172) | Cod sursa (job #2958528) | Cod sursa (job #709474) | Cod sursa (job #137475)
Cod sursa(job #137475)
#include <stdio.h>
#define NMAX 50100
int niv[NMAX];
char tip[NMAX];
int i, j, k, N, T, cnt, stktop, ok;
int main()
{
freopen("nivele.in", "r", stdin);
freopen("nivele.out", "w", stdout);
scanf("%d", &T);
while (T--)
{
scanf("%d", &N);
for (i = 1; i <= N; i++)
scanf("%d", &niv[i]);
if (N == 1 && niv[1] == 1)
{
// special case: unclear which is the answer (can the root be a leaf ?)
printf("DA\n");
continue;
}
cnt = 1;
stktop = 1;
tip[stktop] = 1;
for (ok = i = 1; i <= N && ok; i++)
{
if (niv[i] <= stktop || stktop <= 0)
{
ok = 0;
break;
}
while (stktop < niv[i] - 1)
{
stktop++;
tip[stktop] = 1;
cnt++;
if (cnt >= N)
{
ok = 0;
break;
}
}
if (!ok)
break;
if (tip[stktop] == 1)
tip[stktop] = 2;
else
{
while (stktop > 0 && tip[stktop] == 2)
stktop--;
if (stktop > 0)
tip[stktop] = 2;
}
}
if (cnt != N - 1 || stktop != 0)
ok = 0;
if (ok)
printf("DA\n");
else
printf("NU\n");
}
return 0;
}