网址:http://acm.hdu.edu.cn/showproblem.php?pid=5438

Ponds

Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)

Total Submission(s): 376    Accepted Submission(s): 116

Problem Description
Betty owns a lot of ponds, some of them are connected with other ponds by pipes, and there will not be more than one pipe between two ponds. Each pond has a value
v.



Now Betty wants to remove some ponds because she does not have enough money. But each time when she removes a pond, she can only remove the ponds which are connected with less than two ponds, or the pond will explode.



Note that Betty should keep removing ponds until no more ponds can be removed. After that, please help her calculate the sum of the value for each connected component consisting of a odd number of ponds
 
Input
The first line of input will contain a number
T(1≤T≤30)
which is the number of test cases.



For each test case, the first line contains two number separated by a blank. One is the number
p(1≤p≤104)
which represents the number of ponds she owns, and the other is the number
m(1≤m≤105)
which represents the number of pipes.



The next line contains p
numbers v1,...,vp,
where vi(1≤vi≤108)
indicating the value of pond i.



Each of the last m
lines contain two numbers a
and b,
which indicates that pond a
and pond b
are connected by a pipe.
 
Output
For each test case, output the sum of the value of all connected components consisting of odd number of ponds after removing all the ponds connected with less than two pipes.
 
Sample Input
1
7 7
1 2 3 4 5 6 7
1 4
1 5
4 5
2 3
2 6
3 6
2 7
 
Sample Output
21
 
Source
 
Recommend
hujie   |   We have carefully selected several similar problems for you:  5449 5448 5447 5446 5445

将孩子个数小于等于1的点进入队列,修改与该点相连的孩子数目,维护队列,最后通过dfs查看每一堆池塘的数目,奇数加,偶数忽略。。。

思路好像很行,不过还是在开始的时候理解错了题意了,当时不太懂 odd number of ponds是什么意思,所以开始的时候没有dfs

真的该祭奠一下我的时间了啊,结束前开了好几发,但是一直返回WR,之后改的成了RE了,不知道原因,果然最后就把这个题掉了

结束后开始一点点找错,因为代码太乱了,所以就压根没有发现什么错误,之后再PS的帮助下发现了两个可耻的错误,开始的RE是因为入队之后删边的时候用的是队列里的当前点的孩子数目,结果这个数目可能在入队之后修改了(因为判断完之后就会改变数组节点中的值了),实际上可能不再有边了,这样result可能返回的是end(),所以在删除的时候erase(end())肯定就出错了

另外一个就更SB了,最后dfs图的时候竟然把n用成m了,这能对吗。。。

错误代码:

#pragma comment(linker, "/STACK:102400000000,102400000000")
#include<iostream>
#include<stdio.h>
#include<math.h>
#include <string>
#include<string.h>
#include<map>
#include<queue>
#include<set>
#include<utility>
#include<vector>
#include<algorithm>
#include<stdlib.h>
using namespace std;
#define eps 1e-8
#define pii pair<int,int>
#define INF 0x3f3f3f3f
#define rd(x) scanf("%d",&x)
#define rd2(x,y) scanf("%d%d",&x,&y)
#define ll long long int
#define mod 1000000007
#define maxn 1005
#define maxm 1000005
int point[10005];
struct T{
int childnum;
int i;
int mark;
vector <int> vec;
friend bool operator < (T n1, T n2)
{
return n1.childnum > n2.childnum;
}
}node[10005]; bool vis[10005];
int cnt=0;
long long tsum=0;
void dfs(int x){
if(vis[x]==true) return ;
cnt++,tsum += point[ x ];
vis[x]=true;
for(int i=0;i<node[x].childnum;i++)
dfs( node[x].vec[i] );
} int main (){
int Case;
rd(Case);
while(Case--){
memset(vis,0,sizeof(vis));
int n,m,temp1,temp2;
ll sum=0;
vector<int>::iterator result;
rd2(n,m);
for(int i=1;i<=n;i++) node[i].childnum=0,node[i].i=i,node[i].mark=0 ,node[i].vec.clear();
for(int i=1;i<=n;i++){
rd(point[i]);
sum+=point[i];
}
for(int i=0;i<m;i++){
rd2(temp1,temp2);
//bool mark = (find( node[temp1].vec.begin(), node[temp1].vec.end(), temp2 ) == node[temp1].vec.end() ); if(temp1!=temp2){
node[temp1].vec.push_back(temp2);
node[temp1].childnum++;
node[temp2].vec.push_back(temp1);
node[temp2].childnum++;
}
}
priority_queue <T> que;
for(int i=1;i<=n;i++)
if(node[i].childnum<=1){
que.push(node[i]);
node[i].mark=1;
sum-=point[i];
}
//cout<<"***"<<endl;
while(!que.empty()){
// cout<<"&&&"<<que.top().i<<endl;
if(que.top().childnum==1){ //修改为 : node[que.top().i].childnum==1
int x=que.top().vec[0];//cout<<x<<endl;
result = find( node[x].vec.begin(), node[x].vec.end(), que.top().i );
node[x].vec.erase(result);
node[x].childnum--; int i = que.top().i ;
node[i].vec.erase(node[i].vec.begin());
node[i].childnum-- ; if(node[x].childnum<=1 && node[x].mark == 0){
que.push(node[x]);
node[x].mark=1;
sum-=point[x];
}
}
que.pop();
}
for(int i=1;i<=m;i++){ ///修改为 : i<=n
tsum=cnt=0;
if(!vis[i] && node[i].childnum>=1 ){
dfs(i);
if( cnt%2==0 )
sum-=tsum;
}
// cout<<endl;
}
printf("%I64d\n",sum);
} return 0 ;
}

其实当时没有做出来一点都不冤,因为之后看自己的代码简直就是一坨屎,思路根本不清晰,所以导致有点思路之后敲代码还是有点一头雾水,所以以后还是有了思路之后,最起码是能整体的脉络差不多清晰了再敲。

#pragma comment(linker, "/STACK:102400000000,102400000000")
#include<iostream>
#include<stdio.h>
#include<math.h>
#include <string>
#include<string.h>
#include<map>
#include<queue>
#include<set>
#include<utility>
#include<vector>
#include<algorithm>
#include<stdlib.h>
using namespace std;
#define eps 1e-8
#define pii pair<int,int>
#define INF 0x3f3f3f3f
#define rd(x) scanf("%d",&x)
#define rd2(x,y) scanf("%d%d",&x,&y)
#define ll long long int int point[10005];
vector <int> vec[10005];
bool vis[10005];
bool inqueue[10005];
int cnt=0;
long long tsum=0; void dfs(int x)
{
if(vis[x]) return ;
cnt++,tsum += point[ x ],vis[x]=true;
for(int i=0; i<vec[x].size(); i++)
dfs( vec[x][i] );
} void addedge(int temp1,int temp2)
{
vec[temp1].push_back(temp2);
vec[temp2].push_back(temp1);
} int main ()
{
int Case;
rd(Case);
while(Case--)
{
memset(vis,0,sizeof(vis));
memset(inqueue,0,sizeof(inqueue));
int n,m,temp1,temp2;
ll sum=0;
vector<int>::iterator result;
rd2(n,m);
for(int i=1; i<=n; i++)
{
vec[i].clear();
rd(point[i]);
sum+=point[i];
}
for(int i=0; i<m; i++)
{
rd2(temp1,temp2);
addedge(temp1,temp2);
} queue <int> que; ///没必要将点放入队列 建立索引关系就行
for(int i=1; i<=n; i++)
if(vec[i].size()<=1)
{
que.push(i);
vis[i]=true,inqueue[i]=1,sum-=point[i];
} while(!que.empty())
{
if( vec[que.front()].size()==1 )
{
int x=vec[que.front()][0];
result = find( vec[x].begin(), vec[x].end(), que.front() );
vec[x].erase(result); vec[que.front()].clear(); ///将队列中仅有一个元素清空 if(vec[x].size()<=1 && inqueue[x] == 0)
{
que.push(x);
inqueue[x]=1,vis[x]=true,sum-=point[x];
}
}
que.pop();
} for(int i=1; i<=n; i++){
tsum=cnt=0;
if( !vis[i] ){
dfs(i);
if( cnt%2==0 )
sum-=tsum;
}
}
printf("%I64d\n",sum);
} return 0 ;
}

其实在遍历队列中的元素的时候根本没有必要删除边的关系,因为已经在入队的时候为之后的dfs做准备了,只要入队之后便不再遍历了,删除边就没什么用了,只是更改vec[i]中的数目就可以了...

其实开始的思路清晰的话肯定早就A掉了,以后注意。。。

Regional Changchun Online--Ponds的更多相关文章

  1. 2015ACM/ICPC Asia Regional Changchun Online /HDU 5438 图

    Ponds                                   Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 1310 ...

  2. 2013 Asia Regional Changchun C

    Little Tiger vs. Deep Monkey Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K ( ...

  3. Regional Changchun Online--Elven Postman(裸排序二叉树)

    Elven Postman Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Tot ...

  4. Regional Changchun Online--Travel(最小生成树&& 并查集)

    Travel Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total S ...

  5. Regional Changchun Online--Alisha’s Party

    Alisha's Party Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) ...

  6. hdu 5444 Elven Postman(二叉树)——2015 ACM/ICPC Asia Regional Changchun Online

    Problem Description Elves are very peculiar creatures. As we all know, they can live for a very long ...

  7. (并查集)Travel -- hdu -- 5441(2015 ACM/ICPC Asia Regional Changchun Online )

    http://acm.hdu.edu.cn/showproblem.php?pid=5441 Travel Time Limit: 1500/1000 MS (Java/Others)    Memo ...

  8. (二叉树)Elven Postman -- HDU -- 54444(2015 ACM/ICPC Asia Regional Changchun Online)

    http://acm.hdu.edu.cn/showproblem.php?pid=5444 Elven Postman Time Limit: 1500/1000 MS (Java/Others)  ...

  9. 2015 ACM/ICPC Asia Regional Changchun Online HDU 5444 Elven Postman【二叉排序树的建树和遍历查找】

    Elven Postman Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...

随机推荐

  1. 【转载】springMVC表单校验+全注解

    在这篇文章中,我们将学习如何使用Spring表单标签, 表单验证使用 JSR303 的验证注解,hibernate-validators,提供了使用MessageSource和访问静态资源(如CSS, ...

  2. 根据N种规格中的M种规格值生成的全部规格组合的一种算法

    近来在开发SKU模块的时候,遇到这样一个需求,某种商品有N(用未知数N来表示是因为规格的数组由用户制定且随时可以编辑的,所以对程序来说,它是一个未知数)类规格,每一类规格又有M个规格值,各种规格值的组 ...

  3. 简单实现JS Loading功能

    我们经常在浏览网页的时候会看到数据在加载时,出现的LOADING提示.其实这个功能原理是很简单的,就是一个DIV遮盖当前页面,然后Loading就在遮盖DIV层上展示出来,现在我们来动手实现一下. 1 ...

  4. mysql 10进制与35进制之间的转换 注意Power处理bigint的问题

    35进制的目的是防止0和O造成的视觉误差 BEGIN    DECLARE m_StrHex35 VARCHAR(100); -- 返回35进制表示的结果  DECLARE m_Remainder B ...

  5. Spring的依赖注入(DI)三种方式

    Spring依赖注入(DI)的三种方式,分别为: 1.  接口注入 2.  Setter方法注入 3.  构造方法注入 下面介绍一下这三种依赖注入在Spring中是怎么样实现的. 首先我们需要以下几个 ...

  6. js之引用类型

    一.摘要: <javascript高级程序设计第三版>一书中单独有一章对js的引用类型(Object.Array.RegExp.Function:基本包装类型:Boolean.Number ...

  7. 剑指offer系列17---顺时针打印矩阵(不是很懂)

    package com.exe4.offer; import java.util.ArrayList; /** * 17[题目]输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果 ...

  8. 【转】JVM 分代GC策略分析

    我们以Sun HotSpot VM来进行分析,首先应该知道,如果我们没有指定任何GC策略的时候,JVM默认使用的GC策略.Java虚拟机是按照分代的方式来回收垃圾空间,我们应该知道,垃圾回收主要是针对 ...

  9. android学习笔记26——Activity

    Activity ==> android中四大组件:Activity.Service.BroadcastReceiver.ContentProvider Activity组件用于对用户呈现操作界 ...

  10. 1.html5 学习要求,Html 5发展历程

    以下是我在学习html5,项目中用到的关于html5的总结和心得. 1.学习要求 Html4.01,xhtml Css2 Javascript 耐心,动手,毅力. 2.Html 发展历程 Html1. ...