随笔分类 - LCA
摘要:这个题搞的我好纠结呀!一直是WA,后来才知道自己记录的时候搞错了,ans应该为-1而不是0,因为当时同一个点时,距离为0;这里要用到LCA,我们的距离为两个子节点到根的距离和减去最近祖先的距离的2倍;#include<cstdio>#include<iostream>#include<string.h>#include<vector>using namespace std;class node{public: int en; int len; };vector<node>tree[10024],Qes[10024];int dist[
阅读全文
摘要:该题一下子就爆栈了,我还只能用G++可以提交,c++还是过不了;该题是一道LCA,这里就不重复了,(前面已经说了);#include<cstdio>#include<iostream>#include<string.h>#include<vector>using namespace std;class node{public: int en; int len; };vector<node>tree[40024],Qes[40024];int dis[40024],ans[40024],set[40024],visit[40024],a
阅读全文
摘要:这是一道找最近祖先的问题,我用的是纯暴力的方法。#include<iostream>#include<cstdio>using namespace std;int father[10024];int father1[10024];int father2[10024];int main(){ int n,T,f,s; scanf( "%d",&T ); while( T-- ) { scanf( "%d" ,&n ); for( int i=0;i<=n;i++ ) father[i]=i; for( int
阅读全文

浙公网安备 33010602011771号