zoukankan      html  css  js  c++  java
  • 2015长春网络赛1001 求连通快数量的问题dfs

    Ponds

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


    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
     
    本题首先要删掉度数小于2的点,这里需要注意一个问题就是如果删掉一个点后产生另一个度数为小于二的点仍然需要删除,直到没有可以删除的点为止;
    这里用vecotr和queue实现起来更方便一些;
    最后再求连通块点数的时候用dfs即可
    #include<stdio.h>
    #include<string.h>
    #include<queue>
    #include<vector>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    const int maxn=10005;
    const int maxm=100005;
    int val[maxn];
    vector<int> g[maxm];
    int ind[maxn];
    bool vis[maxn];
    void dfs(int u,long long &cnt,long long &temp){
         vis[u]=true;
         cnt++;
         temp+=val[u];
               for(int i=0;i<g[u].size();i++){
                 int v=g[u][i];
                 if(vis[v])
                     continue;
    
                     dfs(v,cnt,temp);
                    
               }
    }
    
    int main(){
      int t;
      scanf("%d",&t);
      while(t--){
           int n,m;
           memset(val,0,sizeof(val));
           memset(ind,0,sizeof(ind));
           memset(vis,false,sizeof(vis));
    
           scanf("%d%d",&n,&m);
           for(int i=1;i<=n;i++){
               g[i].clear();
             scanf("%d",&val[i]);
           }
           int u,v;
           for(int i=1;i<=m;i++){
             scanf("%d%d",&u,&v);
             g[v].push_back(u);
             g[u].push_back(v);
             ind[u]++;
             ind[v]++;
           }
           queue<int>q;
          for(int i=1;i<=n;i++ ){
              if(ind[i]<2){
                 q.push(i);
                 
              }
          } 
          while(!q.empty()){
               int u=q.front();
               q.pop();
               vis[u]=true;
               ind[u]=0;
               for(int i=0;i<g[u].size();i++){
                    int v=g[u][i];
                    ind[v]--;
                    if(ind[v]<2&&!vis[v]){
                    q.push(v);
                
                    }
               }
          }
          long long ans=0,cnt=0,temp=0;//注意,这里必须用long long,long int会wa
          for(int i=1;i<=n;i++){
             if(vis[i]==true)
    
                 continue;
                temp=0;
                cnt=0;
                dfs(i,cnt,temp);
                    if(cnt%2==1)
                    ans+=temp;
          }
          printf("%lld
    ",ans);
      }
      return 0;
    }
  • 相关阅读:
    C# DataGridView导出到Excel
    面试时,怎样“谈薪”?
    7月11号 列表类型的内置方法及应用
    6月1号 字符编码
    文件相关知识
    Django框架之第二篇app注册、静态文件配置、form表单提交、pycharm链接数据库、Django使用mysql数据库、表字段的增删改查、表数据的增删改查
    函数相关知识
    Django框架之第三篇(路由层)有名/无名分组、反向解析、路由分发、名称空间、伪静态、图书管理系统表设计
    初识Django
    Django框架之第五篇(模型层)单表操作(增删改查)、单表查询和必知必会13条、单表查询之双下划线、Django ORM常用字段和字段参数和关系字段
  • 原文地址:https://www.cnblogs.com/13224ACMer/p/4809833.html
Copyright © 2011-2022 走看看