Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

救命啊

Posted by zhangxiao1124 at 2008-02-21 09:14:04 on Problem 2763
我快被这道题搞死了,谁能给我组错误数据啊

如果嫌麻烦谁能告诉我怎么随机生成一棵树啊?

如果有位活菩萨愿意帮我查一下错那就太好了!!!!


Euler[]就是欧拉序列
rmq就是求Lca
tree就是用线段数维护区间内各点到根的delta

#include<iostream>
using namespace std;

const int maxn=100005;
const int T=1;
int n,q,s,w,d[maxn],r[maxn],b[maxn],father[maxn],p[maxn][5],Euler[maxn*T];
struct edge{int a,b,c;};
edge e[maxn],E[maxn];

int compare(const void *x,const void *y)
{
     return ((edge*)x)->a-((edge*)y)->a;
}

struct RMQ
{
     int rmq[maxn*T*2];
     void build(int root,int left,int right)
     {
          int mid=(left+right)/2;
          if (left==right) {rmq[root]=Euler[left]; return;}
          build(root*2,left,mid);
          build(root*2+1,mid+1,right);
          rmq[root]=min(rmq[root*2],rmq[root*2+1]);
     }
     int query(int root,int l,int r,int i,int j)
     {
          int m=(l+r)/2;
          if (l==i && r==j) return rmq[root];
          if (j<=m) return query(root*2,l,m,i,j);
          if (m<i) return query(root*2+1,m+1,r,i,j);
          return min(query(root*2,l,m,i,m),query(root*2+1,m+1,r,m+1,j));
     }
};
RMQ rmq;

struct Interval_Tree
{
     int tree[maxn*T*2];
     void modify(int root,int l,int r,int i,int j)
     {
          int m=(l+r)/2;
          if (l==i && r==j) tree[root]+=w;
          else if (j<=m) modify(root*2,l,m,i,j);
          else if (m<i) modify(root*2+1,m+1,r,i,j);
          else {
               modify(root*2,l,m,i,m);
               modify(root*2+1,m+1,r,m+1,j);
          }
     }
     int query(int root,int l,int r,int k)
     {
         int m=(l+r)/2;
         if (l==r) return tree[root];
         else if (k<=m) return query(root*2,l,m,k)+tree[root];
         else return query(root*2+1,m+1,r,k)+tree[root];
     }
};
Interval_Tree tree;

void dfs(int root)
{
     int i;
     Euler[++Euler[0]]=root;
     r[root]=Euler[0]; b[root]=Euler[0];
     for (i=p[root][0]; i<=p[root][1]; i++)
     if (e[i].b!=father[root])
     {
        father[e[i].b]=root;
        d[e[i].b]=d[root]+e[i].c;
        dfs(e[i].b);
        Euler[++Euler[0]]=root;
        b[root]=Euler[0];
     }
}

inline int dist(int v) {return d[v]+tree.query(1,1,Euler[0],r[v]);}

int main()
{
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    int i,k,flag,tmp;
    scanf("%d%d%d",&n,&q,&s);
    for (i=1; i<=n-1; i++) scanf("%d%d%d",&e[i].a,&e[i].b,&e[i].c);
    for (i=1; i<=n-1; i++) E[i]=e[i];
    for (i=1; i<=n-1; i++) {e[i+n-1].a=e[i].b;e[i+n-1].b=e[i].a;e[i+n-1].c=e[i].c;}
    qsort(e,2*n-1,sizeof(edge),compare);
    for (i=1; i<=2*(n-1); i++)
    {
        if (e[i].a!=e[i-1].a) p[e[i].a][0]=i;
        if (e[i].a!=e[i+1].a) p[e[i].a][1]=i;
    }
    Euler[0]=0; d[1]=0; father[1]=0; dfs(1);
    rmq.build(1,1,Euler[0]);
    for (i=1; i<=q; i++)
    {
        scanf("%d",&flag);
        if (flag==0) 
        {
           scanf("%d",&k);
           printf("%d\n",dist(s)+dist(k)-2*dist(rmq.query(1,1,Euler[0],min(r[s],r[k]),max(r[s],r[k]))));
           s=k;
        }
        else {
           scanf("%d%d",&k,&w);
           tmp=w; w=w-E[k].c; E[k].c=tmp; 
           if (father[E[k].a]==E[k].b) k=E[k].a;
           else k=E[k].b;
           tree.modify(1,1,Euler[0],r[k],b[k]);
        }
    }
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator