hdu 5439 Ponds(长春网络赛——拓扑排序+搜索)

题目链接: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): 2837    Accepted Submission(s): 891

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 valuev.

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 numberp(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

2015 ACM/ICPC Asia Regional Changchun Online

Recommend

hujie   |   We have carefully selected several similar problems for you:  5659 5658 5657 5656 5655

题目大意:给出一个无向图,每一个节点都有一定的权值,最后要求输出每一个奇数圈内的权值和。

解题思路:首先 你要考虑怎么,保证它是一个圈。这样就想到采用拓扑排序的方法,去掉所有入度<2的点。再就是判断这个圈是否含有奇数个点,采用深搜的方法将每个点做一次起始点,对其进行搜索计数!

详见代码。

#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>

using namespace std;

#define N 10010
#define ll long long

ll val[N],V;
int vis[N];//判断这个点有没有删掉
int Vis[N];//判断这个点有没有做为起点进行搜索过
int indir[N],k;
vector<int>G[N];

void dfs(int st)
{
    k++;
    V+=val[st];
    for (int i=0; i<G[st].size(); i++)
    {
        if (!Vis[G[st][i]]&&!vis[G[st][i]])
        {
            Vis[G[st][i]]=1;
            dfs(G[st][i]);
        }
    }
}

int main()
{
    int t,a,b;
    scanf("%d",&t);
    while (t--)
    {
        int n,m;
        memset(vis,0,sizeof(vis));
        memset(Vis,0,sizeof(Vis));
        memset(val,0,sizeof(val));
        memset(indir,0,sizeof(indir));
        scanf("%d%d",&n,&m);
        for (int i=1; i<=n; i++)
            G[i].clear();
        for (int i=1; i<=n; i++)
            scanf("%lld",&val[i]);
        for (int i=1; i<=m; i++)
        {
            scanf("%d%d",&a,&b);
            G[a].push_back(b);
            G[b].push_back(a);
            indir[a]++;
            indir[b]++;
        }
        while (1)
        {
            int i;
            for (i=1; i<=n; i++)
            {
                if (indir[i]<2&&!vis[i])
                {
                    vis[i]=1;
                    for (int j=0; j<G[i].size(); j++)
                    {
                        if (!vis[G[i][j]])
                            indir[G[i][j]]--;
                    }
                    break;
                }
            }
            if (i>n)
                break;
        }
        ll ans=0;
        for (int i=1; i<=n; i++)
        {
            if (!Vis[i]&&!vis[i])
            {
                k=0;
                V=0;
                Vis[i]=1;
                dfs(i);
                if (k%2==1)
                    ans+=V;
            }
        }
        printf ("%lld\n",ans);
    }
    return 0;
}
时间: 2024-10-16 08:25:15

hdu 5439 Ponds(长春网络赛——拓扑排序+搜索)的相关文章

hdu 5438 Ponds(长春网络赛 拓扑+bfs)

题目链接: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): 2237    Accepted Submission(s): 707 Problem Description Betty owns a lot of ponds, som

hdu 5446(2015长春网络赛J题 Lucas定理+中国剩余定理)

题意:M=p1*p2*...pk:求C(n,m)%M,pi小于10^5,n,m,M都是小于10^18. pi为质数 M不一定是质数 所以只能用Lucas定理求k次 C(n,m)%Pi最后会得到一个同余方程组x≡B[0](mod p[0])x≡B[1](mod p[1])x≡B[2](mod p[2])......解这个同余方程组 用中国剩余定理 Sample Input19 5 23 5 Sample Output6 1 # include <iostream> 2 # include <

hdu 5438 Ponds 长春网赛1002

5438 好吉利的题号 不停的删掉度数小于等于1的点并更新各点度数,直至无法删点,然后dfs即可 #include<cstdio> #include<cstring> #include<algorithm> #include<queue> using namespace std; #define ll __int64 const int maxm=100008; const int maxn=10008; struct fuck{ int u,v,w,nex

hdu 5442 (ACM-ICPC2015长春网络赛F题)

题意: 分析: 明天再写…… #include <cstdio> #include <cstring> #include <algorithm> using namespace std; #define d(x) const int MAX_N = (int)(4e4) + 100; //call init_RMQ(f[], n) first. //then call query(a, b) to quest the RMQ of [a, b]. int power[3

[ACM] hdu 1285 确定比赛名次 (拓扑排序)

确定比赛名次 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 10358    Accepted Submission(s): 4046 Problem Description 有N个比赛队(1<=N<=500),编号依次为1,2,3,....,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排名,但现在裁判委员会不能直

hdu 1285 确定比赛排名(拓扑排序)

确定比赛名次                                                                         Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)                                                                                        

HDU 1285 确定比赛名次(拓扑排序+优先队列)

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1285 Problem Description 有N个比赛队(1<=N<=500),编号依次为1,2,3,....,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排名,但现在裁判委员会不能直接获得每个队的比赛成绩,只知道每场比赛的结果,即P1赢P2,用P1,P2表示,排名时P1在P2之前.现在请你编程序确定排名. Input 输入有若干组,每组中的第一行为二个数N(1<=N<

HDU 1285 确定比赛名次(拓扑排序模板)

题意还是比较容易理解的,关键要看到后面的:合条件的排名可能不是唯一的,此时要求输出时编号小的队伍在前: 思路:这道题就是拓扑排序的经典应用了,用队列做的考虑优先编号小的出队就可以了. 拓扑排序: 拓扑排序是对有向无回路图(DAG)顶点的一种排序,它使得如果存在从u到v的有向路径,那么满足序列中u在v前. 所以我们的算法可以描述为这样一个过程: 1.找到整个图中所有的度为0的点,将这些点压进队列(栈)中 2.从队列(栈)中取出一点,输出,将该点及它的边删除,找到它所指向的点,如果改点是一个原点(删

HDU 4815 2013长春现场赛C题

C - Little Tiger vs. Deep Monkey Time Limit:1000MS     Memory Limit:65535KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 4815 Description A crowd of little animals is visiting a mysterious laboratory ? The Deep Lab of SYSU. "Are y