Last active
February 7, 2016 19:09
-
-
Save rogerioagjr/89d19d714970bd2cc9b8 to your computer and use it in GitHub Desktop.
Montar a Árvore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define MAXN 100100 | |
// declaro as variáveis | |
int pai[MAXN], lvl[MAXN]; | |
// declaro a lista de adjacência | |
vector<int> lista[MAXN]; | |
// DFS para montar a árvore | |
int dfs(int v=1){ // partindo do vértice V para baixo | |
// para cada vizinho de v | |
for(int i=0; i<lista[v].size(); i++){ | |
// seja U o vizinho que estou explorando | |
int u=lista[v][i]; | |
// se ele já tiver sido explorado, continuo | |
if(pai[u]) continue; | |
// caso contrário | |
// o pai de U é V | |
pai[u]=v; | |
// e, portanto, seu nível é o nível de V mais 1 | |
lvl[u]=lvl[v]+1; | |
// e chamo a DFS em U | |
dfs(u); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment