日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

hdu 5438 Ponds 拓扑排序

發(fā)布時間:2023/11/27 生活经验 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 hdu 5438 Ponds 拓扑排序 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Ponds

Time Limit: 1 Sec ?

Memory Limit: 256 MB

題目連接

http://acm.hdu.edu.cn/contests/contest_showproblem.php?pid=1001&cid=621

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

HINT

?

題意

給你一個圖,然后要求把度數(shù)小于2的點(diǎn)全部刪去,然后問你奇數(shù)集合的點(diǎn)權(quán)和是多少

注意,你刪去了一個點(diǎn)之后,可能會使得一些點(diǎn)又變成了度數(shù)小于2的

題解:

用類似拓?fù)渑判虻乃枷肴プ鼍蚾k啦

代碼:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>using namespace std;const int N=200100;
long long a[N],ans;
int n,m,T,cnt,ok[N],vis[N],pre[N],nxt[N],to[N],tot[N],col;
vector<int> s[N];
queue<int> q;void dfs(int x,int fa)
{s[col].push_back(x);ok[x]=0;for(int p=pre[x];p!=-1;p=nxt[p]){if((!vis[p])||(!ok[to[p]])) continue;if(p==(fa^1)) continue;dfs(to[p],p);}
}
void makeedge(int x,int y)
{to[cnt]=y;nxt[cnt]=pre[x];pre[x]=cnt++;to[cnt]=x;nxt[cnt]=pre[y];pre[y]=cnt++;
}int main()
{scanf("%d",&T);while(T--){memset(tot,0,sizeof(tot));memset(pre,-1,sizeof(pre));memset(ok,1,sizeof(ok));memset(vis,1,sizeof(vis));ans=0LL;cnt=0;scanf("%d%d",&n,&m);for(int i=1;i<=n;i++){scanf("%I64d",&a[i]);}for(int i=0;i<m;i++){int x,y;scanf("%d%d",&x,&y);tot[x]++;tot[y]++;makeedge(x,y);}while(!q.empty()) q.pop();for(int i=1;i<=n;i++){if(tot[i]<2){q.push(i);}}while(!q.empty()){int x=q.front();q.pop();ok[x]=0;for(int p=pre[x];p!=-1;p=nxt[p]){vis[p]=0;tot[x]--;tot[to[p]]--;if(ok[to[p]]&&tot[to[p]]<2){q.push(to[p]);}}}col=0;for(int i=1;i<=n;i++){col++;s[col].clear();if(ok[i]){dfs(i,cnt+10);if(s[col].size()%2==1){for(int j=0;j<s[col].size();j++){ans+=a[s[col][j]];}}}}printf("%I64d\n",ans);}return 0;
}

?

轉(zhuǎn)載于:https://www.cnblogs.com/qscqesze/p/4805256.html

總結(jié)

以上是生活随笔為你收集整理的hdu 5438 Ponds 拓扑排序的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。