Cod sursa(job #391207)

Utilizator mathboyDragos-Alin Rotaru mathboy Data 5 februarie 2010 12:01:34
Problema Stramosi Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.98 kb
#include <fstream>
#include <vector>
#include <bitset>
#define DIM 250005
#define pb push_back

using namespace std;

ifstream fin ("stramosi.in"); // citire + scriere cu streamuri pt eficienta (cica)
ofstream fout ("stramosi.out");
int N, M, i, p, q , a;
int st [DIM];

vector <int> listv [DIM], listp [DIM];
bitset <DIM>viz;
int sol [3000005], G [DIM];

void dfs (int node, int lev) {
    
    viz [node] = true;
	int j;
	st [lev] = node;
	for (j = 0; j < listv [node].size (); j++) 
        if (lev >= listv [node][j]) sol [listp [node][j]] = st [lev - listv [node][j]];
	if (!viz [G [node]])
			dfs (G [node], lev + 1);
}

int main () {
	
	int a, b;
	fin >> N >> M;
	for (i = 1; i <= N; i++) {
		fin >> a;
		G [a] = i;
	}
	for (i = 1; i <= M; i++) {
        fin >> a >> b;
        listv [a].pb (b);
        listp [a].pb (i);
    }
	for (i = 1; i <= N; i++)
		if (!viz [i])
			dfs (i, 1);
	for (i = 1; i <= M; i++)
	    fout << sol [i] <<"\n";
	return 0;
}