Tree(未解决。。。)

Tree

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


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
 

 题意:

先给你一颗树,然后有两种操作:

● 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.

add1把从u到v的点所经过的点的权值都增加k;

add2把从u到v所经过的边都增加k;

大婶们都说,这是树链剖分的水题。。。

我用邻接表搞了很久很久,却还是wa。。。。

邻接表代码先贴着,有空学学数链剖分。。。

wa代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<algorithm>
using namespace std;
#define mem(x,y) memset(x,y,sizeof(x))
const int MAXN=1e5+10;
int head[MAXN<<1];
int edgnum;
struct Edge{
	int from,to,next,val;
};
Edge edg[MAXN<<1];
struct Node{
	int u,v;
};
Node dt[MAXN];
int dis[MAXN];
void initial(){
	mem(head,-1);edgnum=0;mem(dis,0);
}
void add(int u,int v,int w){
	Edge E={u,v,head[u],w};
	edg[edgnum]=E;
	head[u]=edgnum++;
}
/*void adw(int u,int v,int w){
	for(int i=head[u];i!=-1;i=edg[i].next){
		
	}
}*/
void dfs1(int i,int v,int w){
//	printf("%d\n",i);
	if(i==-1)return;
	edg[i].val+=w;
	for(int j=head[v];j!=-1;j=edg[j].next){
		if(edg[j].to==edg[i].from){
			edg[j].val+=w;break;
		}
	}
//	printf("%d %d\n",edg[i].from,edg[i].to);
	if(edg[i].to==v)return;
	dfs1(head[edg[i].to],v,w);
}
void dfs2(int i,int v,int w){
	if(i==-1)return;
	int to=edg[i].to;
	dis[to]+=w;
//	printf("%d\n",to);
	if(edg[i].to==v)return;
	dfs2(head[edg[i].to],v,w);
}
int main()
{
	int t,n,m,kase=0;
	char s[5];
	scanf("%d",&t);
	while(t--){
		int u,v,w;
		initial();
		scanf("%d%d",&n,&m);
		for(int i=1;i<n;i++){
			scanf("%d%d",&dt[i].u,&dt[i].v);
			add(dt[i].u,dt[i].v,0);
			add(dt[i].v,dt[i].u,0);
		}
		while(m--){
			scanf("%s",s);
			scanf("%d%d%d",&u,&v,&w);
			if(strcmp(s,"ADD1")==0){
				dis[u]+=w;
				if(u!=v)dfs2(head[u],v,w);
			}
			else{
				if(u!=v)dfs1(head[u],v,w);
			}
		}
		printf("Case #%d:\n",++kase);
		for(int i=1;i<=n;i++){
			if(i!=1)printf(" ");
			printf("%d",dis[i]);
		}puts("");
		for(int i=1;i<n;i++){
			u=dt[i].u;v=dt[i].v;
			int k=head[u];
			if(i!=1)printf(" ");
			printf("%d",edg[k].val);
		}puts("");
	}
	return 0;
}

  

 

posted @ 2015-12-06 17:28  handsomecui  阅读(206)  评论(0编辑  收藏  举报