hdu5044 Tree LCA

http://acm.hdu.edu.cn/showproblem.php?pid=5044

Tree

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 757    Accepted Submission(s): 151


Problem Description
You are given a tree (an acyclic undirected connected graph) with N nodes. The tree nodes are numbered from 1 to N

There are N - 1 edges numbered from 1 to N - 1.

Each node has a value and each edge has a value. The initial value is 0.

There are two kind of operation as follows:

● ADD1 u v k: for nodes on the path from u to v, the value of these nodes increase by k.

● ADD2 u v k: for edges on the path from u to v, the value of these edges increase by k.

After finished M operation on the tree, please output the value of each node and edge.
 

Input
The first line of the input is T (1 ≤ T ≤ 20), which stands for the number of test cases you need to solve.

The first line of each case contains two integers N ,M (1 ≤ N, M ≤105),denoting the number of nodes and operations, respectively.

The next N - 1 lines, each lines contains two integers u, v(1 ≤ u, v ≤ N ), denote there is an edge between u,v and its initial value is 0.

For the next M line, contain instructions “ADD1 u v k” or “ADD2 u v k”. (1 ≤ u, v ≤ N, -105 ≤ k ≤ 105)
 

Output
For each test case, print a line “Case #t:”(without quotes, t means the index of the test case) at the beginning.

The second line contains N integer which means the value of each node.

The third line contains N - 1 integer which means the value of each edge according to the input order.
 

Sample Input
2 4 2 1 2 2 3 2 4 ADD1 1 4 1 ADD2 3 4 2 4 2 1 2 2 3 1 4 ADD1 1 4 5 ADD2 3 2 4
 

Sample Output
Case #1: 1 1 0 1 0 2 2 Case #2: 5 0 0 5 0 4 0
 

Source


题意:给一颗树,m次操作,有两种操作,一种是给u到v的路径点加上k,一种是给u到v的路径边权加上k,输出m次操作后每个点和边的权值。

题解:据说是可以树链剖分,可惜弱还不会啊233。。。。

更好的应该是只求LCA就可以了,参照紫书397的做法,对于每次操作u,v,k,我们记d=LCA(a,b),p为d的父亲节点

对于操作一:

mark[u]+=k;

mark[v]+=k;

mark[t]-=k;

mark[p]-=k;

对于操作二:

mark[u]+=k;

mark[v]+=k;

mark[d]-=2*k;

这里mark[u]=w的意思表示从u到根的路径上每个点(边)都需要加上w,即结点i的值等于根为i的子树的mark值和。所以最后再dfs下树就可以统计出最终的值。

用的是tarjan离线。。扒的kuangbin输入输出外挂。。。

/**
 * @author neko01
 */
#pragma comment(linker, "/STACK:102400000,102400000")
#include <cstdio>
#include <cstring>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
#include <cmath>
#include <set>
#include <map>
using namespace std;
typedef long long LL;
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define pb push_back
#define mp(a,b) make_pair(a,b)
#define clr(a) memset(a,0,sizeof a)
#define clr1(a) memset(a,-1,sizeof a)
#define dbg(a) printf("%d\n",a)
typedef pair<int,int> pp;
const double eps=1e-9;
const double pi=acos(-1.0);
const int INF=0x3f3f3f3f;
const int N=100005;
struct edge{        //树
    int v,next,id;
}e1[N*2];
struct edge2{       //询问
    int v,next,id;
}e2[N*2];
int h1[N],h2[N];
int tot1=0,tot2=0;
int f[N];   //并查集
int fa[N];  //父亲节点
bool vis[N];
int n,m;
struct query{
    int t,u,v,k;
}a[N];
int ans[N];   //每次询问的答案
int b[N];    //以i为终点的边是第b[i]个边
LL mark1[N],mark2[N];
void init()
{
    for(int i=0;i<=n;i++)
        h1[i]=-1,h2[i]=-1,mark1[i]=0,mark2[i]=0,f[i]=i,vis[i]=false;
    tot1=tot2=0;
}
void add1(int u,int v,int w)
{
    e1[tot1].v=v;
    e1[tot1].next=h1[u];
    e1[tot1].id=w;
    h1[u]=tot1++;
}
void add2(int u,int v,int w)
{
    e2[tot2].v=v;
    e2[tot2].next=h2[u];
    e2[tot2].id=w;
    h2[u]=tot2++;
}
int find(int x)
{
    return f[x]==x?x:f[x]=find(f[x]);
}
void tarjan(int u)
{
    vis[u]=true;
    for(int i=h1[u];i!=-1;i=e1[i].next)
    {
        int v=e1[i].v;
        if(!vis[v])
        {
            fa[v]=u;
            b[v]=e1[i].id;
            tarjan(v);
            f[find(v)]=u;
        }
    }
    for(int i=h2[u];i!=-1;i=e2[i].next)
    {
        int v=e2[i].v;
        if(vis[v])
        {
            ans[e2[i].id]=find(v);
        }
    }
}
template <class T>
inline bool scan(T &ret) {
	char c; int sgn;
	if(c=getchar(),c==EOF) return 0; //EOF
	while(c!='-'&&(c<'0'||c>'9')) c=getchar();
	sgn=(c=='-')?-1:1;
	ret=(c=='-')?0:(c-'0');
	while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');
	ret*=sgn;
	return 1;
}
inline void out(long long x) {
	if(x>9) out(x/10);
	putchar(x%10+'0');
}
void solve()
{
    for(int i=1;i<=m;i++)
    {
        int x=a[i].u,y=a[i].v,c=a[i].k;
        int z=ans[i];
        if(a[i].t==1)
        {
            mark1[x]+=c;
            mark1[y]+=c;
            mark1[z]-=c;
            if(fa[z]!=0)
            {
                mark1[fa[z]]-=c;
            }
        }
        else
        {
            mark2[b[x]]+=c;
            mark2[b[y]]+=c;
            if(z!=1)
                mark2[b[z]]-=2*c;
        }
    }
}
void dfs(int u)
{
    for(int i=h1[u];i!=-1;i=e1[i].next)
    {
        int v=e1[i].v;
        if(v!=fa[u])
        {
            dfs(v);
            mark1[u]+=mark1[v];
            mark2[b[u]]+=mark2[b[v]];
        }
    }
}
char s[6];
int main()
{
    int t,cnt=0;
    scanf("%d",&t);
    while(t--)
    {
        scan(n);
        scan(m);
        init();
        for(int i=1;i<n;i++)
        {
            int u,v;
            scan(u);
            scan(v);
            add1(u,v,i);
            add1(v,u,i);
        }
        for(int i=1;i<=m;i++)
        {
            scanf("%s",s);
            if(s[3]=='1') a[i].t=1;
            else a[i].t=2;
            scan(a[i].u);
            scan(a[i].v);
            scan(a[i].k);
            add2(a[i].u,a[i].v,i);
            add2(a[i].v,a[i].u,i);
        }
        fa[1]=0;
        tarjan(1);
        solve();
        dfs(1);
        printf("Case #%d:\n",++cnt);
        for(int i=1;i<=n;i++)
        {
            if(i>1) printf(" ");
            out(mark1[i]);
        }
        puts("");
        for(int i=1;i<n;i++)
        {
            if(i>1) printf(" ");
            out(mark2[i]);
        }
        puts("");
    }
    return 0;
}






没有更多推荐了,返回首页