Coder Social home page Coder Social logo

web3-1's Introduction

web3

web3 int lca(int x, int y) { if (depth[x] > depth[y]) swap(x, y); for(int i = B; i >= 0; i --){ //令x和y高度一致 if (depth[prede[y][i]] >= depth[x]) y = prede[y][i]; } //注意此时有可能出现x == y,那么LCA(x,y) == x,下方的for //就不起作用了。 for(int i = B; i >= 0; i --){ //如果prede[x][i]和prede[y][i]不相同,说明这两者的高度 //都大于所求的LCA(x,y),也就是在LCA(x,y)的下方,此时令 //x和y一同往根部以2^(i)的步数爬升 if (prede[x][i] != prede[y][i]) x = prede[x][i], y = prede[y][i]; } if (x == y) return x; //此时LCA(x,y) = x return prede[x][0]; //此时x和y有共同的父结点 //注意此时有可能出现x == y,那么LCA(x,y) == x,下方的for //就不起作用了。 for(int i = B; i >= 0; i --){ //如果prede[x][i]和prede[y][i]不相同,说明这两者的高度 //都大于所求的LCA(x,y),也就是在LCA(x,y)的下方,此时令 //x和y一同往根部以2^(i)的步数爬升 if (prede[x][i] != prede[y][i]) x = prede[x][i], y = prede[y][i]; //注意此时有可能出现x == y,那么LCA(x,y) == x,下方的for //就不起作用了。 for(int i = B; i >= 0; i --){ //如果prede[x][i]和prede[y][i]不相同,说明这两者的高度 //都大于所求的LCA(x,y),也就是在LCA(x,y)的下方,此时令 //x和y一同往根部以2^(i)的步数爬升 if (prede[x][i] != prede[y][i]) x = prede[x][i], y = prede[y][i]; //注意此时有可能出现x == y,那么LCA(x,y) == x,下方的for //就不起作用了。 for(int i = B; i >= 0; i --){ //如果prede[x][i]和prede[y][i]不相同,说明这两者的高度 //都大于所求的LCA(x,y),也就是在LCA(x,y)的下方,此时令 //x和y一同往根部以2^(i)的步数爬升 if (prede[x][i] != prede[y][i]) x = prede[x][i], y = prede[y][i]; }

web3-1's People

Contributors

zhizhibooty avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.