• POJ 1330 Nearest Common Ancestors LCA


    /********************* Template ************************/
    #include <set>
    #include <map>
    #include <list>
    #include <cmath>
    #include <ctime>
    #include <deque>
    #include <queue>
    #include <stack>
    #include <bitset>
    #include <cstdio>
    #include <string>
    #include <vector>
    #include <cassert>
    #include <cstdlib>
    #include <cassert>
    #include <cstring>
    #include <sstream>
    #include <fstream>
    #include <numeric>
    #include <iomanip>
    #include <iostream>
    #include <algorithm>
    #include <functional>
    using namespace std;
    #define EPS             1e-8
    #define DINF            1e15
    #define MAXN            10050
    #define MOD             1000000007
    #define INF             0x7fffffff
    #define LINF            1LL<<60
    #define PI              3.14159265358979323846
    #define lson            l,m,rt<<1
    #define rson            m+1,r,rt<<1|1
    #define BUG             cout<<"BUG! "<<endl
    #define ABS(a)          ((a)>0?(a):(-a))
    #define LINE            cout<<"------------------ "<<endl
    #define FIN             freopen("in.txt","r",stdin)
    #define FOUT            freopen("out.txt","w",stdout)
    #define mem(a,b)        memset(a,b,sizeof(a))
    #define FOR(i,a,b)      for(int i = a ; i < b ; i++)
    #define read(a)         scanf("%d",&a)
    #define read2(a,b)      scanf("%d%d",&a,&b)
    #define read3(a,b,c)    scanf("%d%d%d",&a,&b,&c)
    #define write(a)        printf("%d
    ",a)
    #define write2(a,b)     printf("%d %d
    ",a,b)
    #define write3(a,b,c)   printf("%d %d %d
    ",a,b,c)
    #pragma comment         (linker,"/STACK:102400000,102400000")
    template<class T> inline T L(T a)               {return (a << 1);}
    template<class T> inline T R(T a)               {return (a << 1 | 1);}
    template<class T> inline T lowbit(T a)          {return (a & -a);}
    template<class T> inline T Mid(T a,T b)         {return ((a + b) >> 1);}
    template<class T> inline T gcd(T a,T b)         {return b ? gcd(b,a%b) : a;}
    template<class T> inline T lcm(T a,T b)         {return a / gcd(a,b) * b;}
    template<class T> inline T Min(T a,T b)         {return a < b ? a : b;}
    template<class T> inline T Max(T a,T b)         {return a > b ? a : b;}
    template<class T> inline T Min(T a,T b,T c)     {return min(min(a,b),c);}
    template<class T> inline T Max(T a,T b,T c)     {return max(max(a,b),c);}
    template<class T> inline T Min(T a,T b,T c,T d) {return min(min(a,b),min(c,d));}
    template<class T> inline T Max(T a,T b,T c,T d) {return max(max(a,b),max(c,d));}
    template<class T> inline T mod(T x,T y)         {y = ABS(y); return x >= 0 ? x % y : x % y + y;}
    template<class T> inline T mul_mod(T a,T b,T n) {
        T ret = 0,tmp = a % n;
        while(b){
            if((b&1) && (ret+=tmp)>=n) ret -= n;
            if((b>>=1) && (tmp<<=1)>=n) tmp -= n;
        }return ret;
    }
    template<class T> inline T pow_mod(T a,T b,T n){
        T ret = 1; a = a % n;
        while(b){
            if (b&1) ret = mul_mod(ret,a,n);
            if (b>>=1) a = mul_mod(a,a,n);
        }return ret;
    }
    template<class T> inline T exGCD(T a, T b, T &x, T &y){
        if(!b) return x = 1,y = 0,a;
        T res = exGCD(b,a%b,x,y),tmp = x;
        x = y,y = tmp - (a / b) * y;
        return res;
    }
    template<class T> inline T reverse_bits(T x){
        x = (x >> 1 & 0x55555555) | ((x << 1) & 0xaaaaaaaa); x = ((x >> 2) & 0x33333333) | ((x << 2) & 0xcccccccc);
        x = (x >> 4 & 0x0f0f0f0f) | ((x << 4) & 0xf0f0f0f0); x = ((x >> 8) & 0x00ff00ff) | ((x << 8) & 0xff00ff00);
        x = (x >>16 & 0x0000ffff) | ((x <<16) & 0xffff0000); return x;
    }
    typedef long long LL;    typedef unsigned long long ULL;
    //typedef __int64 LL;      typedef unsigned __int64 ULL;
    /*********************   By  F   *********************/
    vector<int> G[MAXN];
    int fa[MAXN];
    int lev[MAXN];
    int root;
    int n,u,v,T;
    void init(){
        for(int i = 0 ; i < MAXN; i++) G[i].clear();
        mem(fa,0);
        mem(lev,0);
    }
    void dfs(int u,int dep){
        lev[u] = dep;
        for(int i = 0 ; i < G[u].size() ; i++){
            dfs(G[u][i],dep+1);
        }
    }
    int lca(int a,int b){
        while(lev[a] > lev[b]) a = fa[a];
        while(lev[a] < lev[b]) b = fa[b];
        while(a != b){
            a = fa[a];
            b = fa[b];
        }
        return a;
    }
    int main(){
        //FIN;
        //FOUT;
        scanf("%d",&T);
        while(T--){
            scanf("%d",&n);
            init();
            for(int i = 0 ; i < n-1 ; i++){
                scanf("%d%d",&u,&v);
                G[u].push_back(v);
                fa[v] = u;
            }
            scanf("%d%d",&u,&v);
            root = u;
            while(fa[root] != 0) root = fa[root];
            dfs(root,1);
            printf("%d
    ",lca(u,v));
        }
        return 0;
    }

    水题,没什么可说的

  • 相关阅读:
    python 全栈开发,Day34(基于UDP协议的socket)
    python 全栈开发,Day33(tcp协议和udp协议,互联网协议与osi模型,socket概念,套接字(socket)初使用)
    python 全栈开发,Day32(知识回顾,网络编程基础)
    python 全栈开发,Day30(第一次面向对象考试)
    python 全栈开发,Day30(纸牌游戏,异常和错误,异常处理)
    python 全栈开发,Day29(昨日作业讲解,模块搜索路径,编译python文件,包以及包的import和from,软件开发规范)
    python 全栈开发,Day28(复习,os模块,导入模块import和from)
    python 全栈开发,Day27(复习, defaultdict,Counter,时间模块,random模块,sys模块)
    python 全栈开发,Day26(hashlib文件一致性,configparser,logging,collections模块,deque,OrderedDict)
    css关系选择符
  • 原文地址:https://www.cnblogs.com/Felix-F/p/3572392.html
Copyright © 2020-2023  润新知