题目:hdu 5438
Ponds
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 76 Accepted Submission(s): 28
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
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.
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
在判断连通分量是奇数个点的,并把点的值相加,这样需要判断是否在一个连通分量里,那么可以用并查集来处理点,到最后每个点的父节点如果在一个连通图中就是相同的,到最后处理一下就好了,思路是这样的,可是我在删点的时候可能时间复杂度比较高,总是超时,大神说使用队列处理,果然,大神就是大神。。。。
AC代码。
#include <iostream>
#include <cmath>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <stack>
#include <queue>
#include <cstring>
#define LL long long
#define MEM(a,b) memset(a,b,sizeof(a))
using namespace std;
const int N=10005;
vector<int>a[N];
int val[N],deg[N],fa[N],sum[N];
bool vis[N],dead[N];
void init(){
MEM(vis,0);
MEM(dead,0);
MEM(deg,0);
MEM(sum,0);
MEM(val,0);
for(int i=0;i<N;i++){
a[i].clear();
fa[i]=i;
}
}
int Find(int x)
{
return fa[x]==x?x:fa[x]=Find(fa[x]);
}
void dfs(int x)
{
if(dead[x])return;
for(int i=0;i<a[x].size();i++){
int v=a[x][i];
if(dead[v]||vis[v])continue;
vis[v]=true;
int fx=Find(x);
int fv=Find(v);
fa[fx]=fv;
dfs(v);
}
}
int main()
{
int T,p,m;
scanf("%d",&T);
while(T--){
scanf("%d%d",&p,&m);
init();
for(int i=1;i<=p;i++)
scanf("%d",&val[i]);
int x,y;
for(int i=0;i<m;i++){
scanf("%d%d",&x,&y);
deg[x]++;
deg[y]++;
a[x].push_back(y);
a[y].push_back(x);
}
queue<int>Q;
for(int i=1;i<=p;i++)
if(deg[i]<=1)Q.push(i);
while(!Q.empty()){
int t=Q.front();
Q.pop();
dead[t]=true;
for(int i=0;i<a[t].size();i++){
int v=a[t][i];
deg[v]--;
if(deg[v]<=1&&!dead[v])Q.push(v);
}
}
for(int i=1;i<=p;i++)
if(!vis[i]&&!dead[i]){
vis[i]=true;
dfs(i);
}
for(int i=1;i<=p;i++){
sum[Find(i)]++;
}
LL ans=0;
vector<int>v;
for(int i=1;i<=p;i++)
if(!dead[i]&&sum[i]%2==1)v.push_back(i);
for(int i=1;i<=p;i++){
if(!dead[i]){
for(int j=0;j<v.size();j++)
if(Find(i)==v[j])
ans+=val[i];
}
}
cout<<ans<<endl;
}
return 0;
}
超时代码:
#include<iostream>
#include<stdio.h>
#include<string>
#include<string.h>
#include<algorithm>
#include<vector>
#include<deque>
#include<math.h>
#define LL long long
using namespace std;
int T,p[10005],nump,m,deg[10005],fa[10005],sum[10005];
deque<int>a[10005];
int findFa(int x)
{
while(fa[x]!=x)
x=fa[x];
return x;
}
void union1(int x,int y)
{
int fx=findFa(x);
int fy=findFa(y);
if(fx!=fy)
fa[fx]=fy;
}
void dfs()
{
int num=0;
for(int i=1;i<=nump;i++){
if(deg[i]!=1) num++;
else{
deg[i]--;
deg[a[i][0]]--;
a[i][0]=0;
a[a[i][0]]=0;
for(deque<int>::iterator it=a[i].begin();it!=a[i].end();it++){
if(*it==a[i][0])a[a[i][0]].erase(it);
}
a[i].pop_front();
}
}
if(num==nump) return;
dfs();
}
int main()
{
scanf("%d",&T);
while(T--){
for(int i=0;i<10005;i++)
a[i].clear();
memset(deg,0,sizeof(deg));
memset(sum,0,sizeof(sum));
scanf("%d%d",&nump,&m);
for(int i=1;i<=nump;i++)
scanf("%d",&p[i]);
int x,y;
for(int i=0;i<m;i++){
scanf("%d%d",&x,&y);
deg[x]++;
deg[y]++;
a[x].push_back(y);
a[y].push_back(x);
}
dfs();
for(int i=0;i<=nump;i++){
fa[i]=i;
}
for(int i=1;i<=nump;i++){
if(deg[i]>1){
int j=0;
for(int j=0;j<a[i].size();j++)
union1(i,a[i][j]);
}
}
for(int i=1;i<=nump;i++){
sum[findFa(i)]++;
}
LL ans=0;
vector<int>v;
for(int i=1;i<=nump;i++)
if(deg[i]>1&&sum[i]%2==1)v.push_back(i);
for(int i=1;i<=nump;i++){
if(deg[i]>1){
for(int j=0;j<v.size();j++)
if(findFa(i)==v[j])
ans+=p[i];
}
}
cout<<ans<<endl;
}
return 0;
}