hdu4585 & BestCoder Round #1 项目管理(vector应用)

主题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4858

项目管理

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 760    Accepted Submission(s): 262


Problem Description
我们建造了一个大项目!

这个项目有n个节点,用非常多边连接起来,而且这个项目是连通的。
两个节点间可能有多条边,只是一条边的两端必定是不同的节点。
每一个节点都有一个能量值。

如今我们要编写一个项目管理软件。这个软件呢有两个操作:
1.给某个项目的能量值加上一个特定值。


2.询问跟一个项目相邻的项目的能量值之和。(假设有多条边就算多次,比方a和b有2条边。那么询问a的时候b的权值算2次)。


Input
第一行一个整数T(1 <= T <= 3),表示測试数据的个数。
然后对于每一个測试数据。第一行有两个整数n(1 <= n <= 100000)和m(1 <= m <= n + 10),分别表示点数和边数。

然后m行,每行两个数a和b。表示a和b之间有一条边。
然后一个整数Q。

然后Q行,每行第一个数cmd表示操作类型。假设cmd为0,那么接下来两个数u v表示给项目u的能量值加上v(0 <= v <= 100)。
假设cmd为1,那么接下来一个数u表示询问u相邻的项目的能量值之和。



全部点从1到n标号。

 
Output
对每一个询问,输出一行表示答案。

 
Sample Input
1 3 2 1 2 1 3 6 0 1 15 0 3 4 1 1 1 3 0 2 33 1 2
 
Sample Output
4 15 15
 
Author
CLJ
 
Source

代码例如以下:
G++提交超时, C++AC;
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
using namespace std;
const int N=100017;
int c[N];
vector<int>p[N];
int main()
{
	int T;
	int n, m, a, b;
	int Q;
	int i, j;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d%d",&n,&m);
		for(i = 1; i <= n; i++)
			p[i].clear();
		for(i = 1; i <= m; i++)
		{
			scanf("%d%d",&a,&b);
			p[a].push_back(b);//由于要求相邻的,分别加入
			p[b].push_back(a);
		}
		scanf("%d",&Q);
		int op, u, v;
		memset(c,0,sizeof(c));
		for(i = 1; i <= Q; i++)
		{
			scanf("%d",&op);
			
			if(op == 0)
			{
				scanf("%d%d",&u,&v);
				c[u]+=v;
			}
			else if(op == 1)
			{
				scanf("%d",&u);
				int ans = 0;
				int size = p[u].size();
				for(i = 0; i < size; i++)
				{
					ans+=c[p[u][i]];
				}
				printf("%d
",ans);
			}
		}
	}
	return 0;
}




版权声明:本文博客原创文章。博客,未经同意,不得转载。

原文地址:https://www.cnblogs.com/hrhguanli/p/4624482.html