Mai intai trebuie sa te autentifici.
Cod sursa(job #1537985)
| Utilizator | Data | 28 noiembrie 2015 12:52:39 | |
|---|---|---|---|
| Problema | Stramosi | Scor | 100 |
| Compilator | cpp | Status | done |
| Runda | Arhiva de probleme | Marime | 0.83 kb |
#include <iostream>
#include <fstream>
#include <vector>
#define nmax 250005
using namespace std;
ifstream f("stramosi.in");
ofstream g("stramosi.out");
int st[20][nmax], n, m, x, y;
void Gen_stramosi()
{
for(int i=1; i<=n; i++)
f >> st[0][i];
for(int i=1; (1<<i) <=n; i++)
{
for(int j=1; j<=n; j++)
{
st[i][j] = st[i-1][st[i-1][j]]; // al 2^(i-1)-lea stramos a stramosului de pe 2^(i-1)
}
}
return;
}
int querry(int p, int nod)
{
for(int i=0; (1<<i)<=p; i++)
if( (1<<i) & p)
nod = st[i][nod];
return nod;
}
int main()
{
f >> n >> m;
Gen_stramosi();
for(int i=1; i<=m; i++)
{
f >> x >> y;
g << querry(y, x) << "\n";
}
f.close();
g.close();
return 0;
}
