干货收集

树的Prufer编码及应用:

http://www.mengchunlei.net/2015/06/21/树的prufer编码/

http://blog.csdn.net/bingshangjiguang/article/details/6016676

BZOJ 1005:

http://ydcydcy1.blog.163.com/blog/static/216089040201302303451866/

代码:

struct node{
  int d,num; //结点度数与序号
} T[maxn];

struct cmp{
 bool operator() (const node a,const node b) {return a.num>b.num;}
};

int n,u,v,size,p[maxn];

vector<int> G[maxn];

void build_prufer() //由树得出prufer序列
{
 size=0;
 
 priority_queue<node,vector<node>,cmp> Q;
 rep(i,1,n) if (T[i].d==1) Q.push(T[i]);
 
 while (size<(n-2))
 {
  node k=Q.top();Q.pop();

  rep(i,0,sz(G[k.num])-1) 
   if (T[G[k.num][i]].d) 
   {
    T[G[k.num][i]].d--,T[k.num].d--,p[++size]=G[k.num][i];
    if (T[G[k.num][i]].d==1) Q.push(T[G[k.num][i]]);
    break;
   }
 }
}

void prufer_tree() //由prufer序列建树
{
 rep(i,1,n) T[i].d=1;
 rep(i,1,size) T[p[i]].d++;
 
 priority_queue<node,vector<node>,cmp> Q;
 rep(i,1,n) if (T[i].d==1) Q.push(T[i]);
 
 int x=0,y;

 while (x<size)
 {
  x++;
  node k=Q.top();Q.pop();

  addedge(p[x],k.num);
  T[p[x]].d--,T[k.num].d--;
  if (T[p[x]].d==1) Q.push(T[p[x]]);
 }
 
 x=0,y=0;
 rep(i,1,n) 
  if (T[i].d) 
   if (x==0) x=i; else y=i;

 addedge(x,y);
}

牛顿迭代法快速求平方根:

初设\(x=n\)\(x_{k+1}=(x_k+n/x_k)/2\)

posted @ 2016-08-16 20:33  Krew  阅读(313)  评论(0编辑  收藏  举报