HDU 5438 拓扑排序+DFS

Ponds

Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 3234    Accepted Submission(s): 997

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

题意:一直去度数小于1的点,直到不能再去。

题解:对图拓扑排序后,剩余部分DFS判断是否为奇数个,奇数个直接加权值。 坑点在下面标出。

#include <iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#include <vector>
using namespace std;
const int maxn = 1e4+5;
vector<int> g[maxn];
int v[maxn];
int ans[maxn];
int vis[maxn];
int in[maxn];

int t;
int p,m;
int cnt;
void toposort()
{
    queue<int> q;
    for(int i = 1; i<=p; i++)
        if(in[i] <= 1)   //还有度数为0的点
            q.push(i);
    while(!q.empty())
    {
        int temp = q.front();
        q.pop();
        ans[temp]++;
        for(int j = 0; j<g[temp].size(); j++)
        {
            int vv = g[temp][j];
            if(in[vv]<=1) continue;  //z这里有坑,否则就陷入死循环了。
            in[vv]--;
            if(in[vv] <= 1)
                q.push(vv);
        }
    }
}
void dfs(int s,int& count,long long& sum)
{
    if(ans[s]>0||vis[s]) return;
    vis[s] = 1;
    for(int i = 0; i<g[s].size(); i++)
    {
        int vv = g[s][i];
        if(ans[vv] == 0&& !vis[vv])
        {
            count++;
            sum += v[vv];
            dfs(vv,count,sum);
        }
    }
}
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        memset(ans,0,sizeof(ans));
        memset(in,0,sizeof(in));
        memset(vis,0,sizeof(vis));
        scanf("%d %d",&p,&m);
        for(int i = 1; i<=p; i++)
        {
            scanf("%d",&v[i]);
        }
        int l,r;
        for(int i = 1; i<=p; i++) g[i].clear();
        for(int i = 1; i<=m; i++)
        {
            scanf("%d %d",&l,&r);
            g[l].push_back(r);
            g[r].push_back(l);
            in[l]++;
            in[r]++;
        }
        toposort();
        long long sum = 0,sum1 = 0;
        int count = 0;
        for(int i = 1; i<=p; i++)
        {
            if(ans[i] == 0&&!vis[i])
            {
                sum1 = v[i];
                count = 1;
                dfs(i,count,sum1);
                if(count%2 == 1) sum += sum1;
            }
        }
        printf("%I64d\n",sum);

    }
    return 0;
}
/*
312
7 10
1 20 300 400 500 1000 5000
1 2
1 3
1 4
2 3
2 4
3 4
5 6
6 7
5 7
3 6

2 1
10 20
1 2

3 2
10 100 1000
1 2
1 3

3 1
10 100 1000
1 2
*/
时间: 2024-12-28 15:45:44

HDU 5438 拓扑排序+DFS的相关文章

poj 1270 Following Orders(拓扑排序+dfs)

大致题意:每个样例包含两行,第一行输入n个字符,可能是无序的.第二行输入成对的a b,代表a要在b前面.输出所有的符合这样的序列. 思路:很明显的拓扑排序.要输出所有的序列,那么就从入度为0的点进行dfs,每次选择一个入度为0的点,加入输出序列并把与它相邻的点的入度减一.dfs结束后要把状态再改回来. #include <stdio.h> #include <algorithm> #include <set> #include <map> #include

poj1270Following Orders(拓扑排序+dfs回溯)

题目链接: 啊哈哈,点我点我 题意是: 第一列给出所有的字母数,第二列给出一些先后顺序.然后按字典序最小的方式输出所有的可能性... 思路: 总体来说是拓扑排序,但是又很多细节要考虑,首先要按字典序最小的方式输出,所以自然输入后要对这些字母进行排列,然后就是输入了,用scanf不能读空格,所以怎么建图呢??设置一个变量判断读入的先后顺序,那么建图完毕后,就拓扑排序了,那么多种方式自然就是dfs回溯了..那么这个问题就得到了解决.. 题目: Following Orders Time Limit:

ACM/ICPC 之 拓扑排序+DFS(POJ1128(ZOJ1083))

经典的拓扑排序问题,难点在于字典序输出和建立拓扑图,另外理解题意是最难的难点,没有之一... POJ1128(ZOJ1083)-Frame Stacking 题意:每个图片由同一字母组成的边框表示,每个图片的字母都不同: 在一个最多30*30的区域放置这些图片,问底层向顶层叠加的图片次序,多选时按字典序输出 注:每个图片的四边都会有字符显示,其中顶点显示两边. 题解:题意的理解是难点,题目对图片的范围确定说得有点含糊不清,博主一开始就被出现的五张图片的样例迷惑,理解重心放错了.题目最需要理解的是

HDU 4857 拓扑排序 优先队列

n个数,已经有大小关系,现给m个约束,规定a在b之前,剩下的数要尽可能往前移.输出序列 大小关系显然使用拓扑结构,关键在于n个数本身就有大小关系,那么考虑反向建图,优先选择值最大的入度为零的点,这样得到的序列就是从大到小的,最后倒序输出就行了. 写这题的时候头好痛阿肚子好痛阿,再也不想熬夜了,一点效率都没有. /** @Date : 2017-09-29 19:29:12 * @FileName: HDU 4857 拓扑排序 + 优先队列.cpp * @Platform: Windows * @

HDU 5438 Ponds (拓扑排序+DFS)2015 ACM/ICPC Asia Regional Changchun Online

[题目链接]:click here~~ [题目大意]: 题意:在一个无向图中有 p 个点, m 条边,每个点有一个值 vi .不断的删去度数小于2的点直到不能删为止.求新图中所有点个数为奇数的连通分量的点值的和. 1<p<10^4,1<m<10^5 [思路]删边考虑类似拓扑排序的写法,不过topsort是循环一遍1到n结点入度为0的结点,然后加入到队列中,这里只要改一下度数小于等于1,最后DFS 判断一下 挫挫的代码: /* * Problem: HDU No.5438 * Run

HDU 4324 (拓扑排序) Triangle LOVE

因为题目说了,两个人之间总有一个人喜欢另一个人,而且不会有两个人互相喜欢.所以只要所给的图中有一个环,那么一定存在一个三元环. 所以用拓扑排序判断一下图中是否有环就行了. 1 #include <cstdio> 2 #include <cstring> 3 4 const int maxn = 2000 + 10; 5 char G[maxn][maxn]; 6 int c[maxn]; 7 int n; 8 9 bool dfs(int u) 10 { 11 c[u] = -1;

HDU 5638 拓扑排序+优先队列

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5638 题意: 给你一个DAG图,删除k条边,使得能个得到字典序尽可能小的拓扑排序 题解: 把拓扑排序的算法稍微改一下,如果某个顶点的入度小于k也把它加到优先队列里面去. k减小后队列里面会有些点不满足<=k,直接踢出来就好了. 代码: #include<iostream> #include<cstring> #include<cstdio> #include<

Codeforces Round #292 (Div. 2) D. Drazil and Tiles [拓扑排序 dfs]

传送门 D. Drazil and Tiles time limit per test 2 seconds memory limit per test 256 megabytes Drazil created a following problem about putting 1 × 2 tiles into an n × m grid: "There is a grid with some cells that are empty and some cells that are occupie

hdu 2647 (拓扑排序 邻接表建图的模板) Reward

题目链接http://acm.hdu.edu.cn/showproblem.php?pid=2647 老板给员工发工资,每个人的基本工资都是888,然后还有奖金,然后员工之间有矛盾,有的员工希望比某员工的奖金多,老板满足了所以员工的这种心思,而且老板下午发的总工资最少,问最少是多少?比如 a b 表示a的工资比b要高(高一块钱),当出现a b   b c   c a这种环的时候输出-1 拓扑排序http://www.cnblogs.com/tonghao/p/4721072.html 小指向大