Tree
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1329 Accepted Submission(s): 222
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.
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 ≤10 5),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, -10 5 ≤ k ≤ 10 5)
The first line of each case contains two integers N ,M (1 ≤ N, M ≤10 5),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, -10 5 ≤ k ≤ 10 5)
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.
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
2014 ACM/ICPC Asia Regional Shanghai Online
两种操作,一种更新结点值,一种更新路径值,最后输出更改后的结点值和路径值。
对于区间[a,b],区间的每个值加上c,可以用一个数组标记,ans[a]+=c,ans[b+1]-=c;然后下标从a,遍历到b,把所有的ans[]值加上,就等于当前结点修改后的值。注意两点,一是手动扩栈,二是最终的结果用64位。
#pragma comment(linker, "/STACK:102400000,102400000")
#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
#define N 100010
struct pp
{
int u,v;
}ed[N];
struct node
{
int u,v,next;
}bian[N*2];
int e,id,dep[N],son[N],father[N],sz[N],ti[N],mark1[N],mark2[N],top[N],head[N];
__int64 a[N],b[N],ans1[N],ans2[N];
void add(int u,int v)
{
bian[e].u=u;
bian[e].v=v;
bian[e].next=head[u];
head[u]=e++;
}
void dfs1(int u,int fa)
{
int i,v;
dep[u]=dep[fa]+1; son[u]=0; father[u]=fa; sz[u]=1;
for(i=head[u];i!=-1;i=bian[i].next)
{
v=bian[i].v;
if(v==fa) continue;
dfs1(v,u);
sz[u]+=sz[v];
if(sz[son[u]]<sz[v])
son[u]=v;
}
}
void dfs2(int u,int fa)
{
int i,v;
ti[u]=id++;
mark1[id-1]=u;
top[u]=fa;
if(son[u]!=0)
dfs2(son[u],fa);
for(i=head[u];i!=-1;i=bian[i].next)
{
v=bian[i].v;
if(v==father[u]||v==son[u])
continue;
dfs2(v,v);
}
}
void getnode(int u,int v,int k)
{
while(top[u]!=top[v])
{
if(dep[top[u]]>dep[top[v]])
swap(u,v);
a[ti[top[v]]]+=k;
a[ti[v]+1]-=k;
v=father[top[v]];
}
if(ti[u]>ti[v])
swap(u,v);
a[ti[u]]+=k;
a[ti[v]+1]-=k;
}
void getedge(int u,int v,int k)
{
while(top[u]!=top[v])
{
if(dep[top[u]]>dep[top[v]])
swap(u,v);
b[ti[top[v]]]+=k;
b[ti[v]+1]-=k;
v=father[top[v]];
}
if(ti[u]>ti[v])
swap(u,v);
if(u!=v)
{
b[ti[u]+1]+=k;
b[ti[v]+1]-=k;
}
}
int main()
{
int t,cnt=1,n,m,i,u,v,k;
__int64 s;
char str[10];
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
memset(a,0,sizeof(a));
memset(head,-1,sizeof(head));
memset(b,0,sizeof(b));
e=0;
for(i=1;i<n;i++)
{
scanf("%d%d",&ed[i].u,&ed[i].v);
add(ed[i].u,ed[i].v);
add(ed[i].v,ed[i].u);
}
sz[0]=0; id=1; dep[1]=0;
dfs1(1,1);
dfs2(1,1);
for(i=1;i<=m;i++)
{
scanf("%s%d%d%d",str,&u,&v,&k);
if(strcmp(str,"ADD1")==0)
getnode(u,v,k);
else
getedge(u,v,k);
}
for(i=1;i<n;i++)
{
if(dep[ed[i].u]<dep[ed[i].v])
mark2[ti[ed[i].v]]=i;
else
mark2[ti[ed[i].u]]=i;
}
printf("Case #%d:\n",cnt++);
s=0;
for(i=1;i<=n;i++)
{
s+=a[i];
ans1[mark1[i]]=s;
}
for(i=1;i<=n;i++)
{
if(i==1)
printf("%I64d",ans1[i]);
else
printf(" %I64d",ans1[i]);
}
printf("\n");
s=0;
for(i=2;i<=n;i++)
{
s+=b[i];
ans2[mark2[i]]=s;
}
for(i=1;i<n;i++)
{
if(i==1)
printf("%I64d",ans2[i]);
else
printf(" %I64d",ans2[i]);
}
printf("\n");
}
return 0;
}