HDU5438-搜索

Ponds

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


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(1T30) 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(1p104) which represents the number of ponds she owns, and the other is the number m(1m105) which represents the number of pipes.

The next line contains p numbers v1,...,vp, where vi(1vi108) 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
 

題目大意:


給你一個n個點m條邊的圖,每個點有個權值,現在你要刪除所有度<=1的點,刪除一個點後與他連接的邊也刪除,問最後不能刪除

後的圖當中,每個奇數點數的連通圖的權值和


題目思路:


對於刪除度小於等於1的點,我們可以bfs來刪除,首先建圖時用個數數組in[i]記錄與點i連接的邊數,然後把度<=1的點入隊,並且標記,然後bfs遍歷圖,當前點沒有被標記時度數--,如果減後的度數爲1就進隊,標記,最後刪除後再用一遍bfs遍歷所有連通圖,沒有被標記的點的圖,記錄點數和權值,最後在加起來就是


AC代碼:

#include<bits/stdc++.h>
using namespace std;

int in[10005],vis[10005];
long long v[10005];
vector<int>G[10005];
int n,m;
int num;
long long ans,res;

void bfs(int u)      //搜索連通圖
{

    queue<int>q;
    q.push(u);
    vis[u] = 1;
    num++;
    res+=v[u];
    while(!q.empty())
    {
        u = q.front();
        q.pop();
        for(int i=0;i<G[u].size();i++)
        {
            int vv = G[u][i];
            if(vis[vv]==0)    //沒有被標記的點
            {
                vis[vv] = 1;
                res+=v[vv];
                num++;
                q.push(vv);
            }
        }
    }

}

int main()
{
    int t;cin>>t;
    while(t--)
    {
        ans = 0;
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)scanf("%lld",&v[i]),ans+=v[i],in[i] = 0,vis[i] = 0,G[i].clear();
        while(m--)
        {
            int u,v;scanf("%d%d",&u,&v);
            G[u].push_back(v);
            G[v].push_back(u);
            in[u]++;
            in[v]++;
        }
        queue<int>q;
        for(int i=1;i<=n;i++)
        {
            if(in[i]==0)ans-=v[i],vis[i] = 1;
            else if(in[i]==1)
            {
                q.push(i);
                vis[i] = 1;
            }
        }
        while(!q.empty())    //刪點
        {
            int u = q.front();
            q.pop();
            for(int i=0;i<G[u].size();i++)
            {

                int vv = G[u][i];
                if(vis[vv]==0)
                {
                    in[vv]--;
                    if(in[vv]==1)
                    {
                        q.push(vv);
                        vis[vv] = 1;
                    }
                }
            }
        }
        ans = 0;
        for(int i=1;i<=n;i++)
        {
            if(vis[i]==0)
            {
                res = 0;
                num = 0;
                bfs(i);
                if(num%2==1)ans+=res;
            }
        }

        cout<<ans<<endl;
    }
    return 0;
}





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章