Perfect Service - UVa 1218 dp

本文探讨了在一棵树形网络中,如何确定最少数量的服务器来确保每个客户端仅由一个服务器提供服务的问题。通过使用深度优先搜索和动态规划的方法,文章提出了一种高效的解决方案。

A network is composed of N computers connected by N - 1 communication links such that any two computers can be communicated via a unique route. Two computers are said to be adjacent if there is a communication link between them. The neighbors of a computer is the set of computers which are adjacent to it. In order to quickly access and retrieve large amounts of information, we need to select some computers acting as servers to provide resources to their neighbors. Note that a server can serve all its neighbors. A set of servers in the network forms a perfect service if every client (non-server) is served by exactly one server. The problem is to find a minimum number of servers which forms a perfect service, and we call this number perfect service number.

We assume that N (10000) is a positive integer and these N computers are numbered from 1 to N . For example, Figure 1 illustrates a network comprised of six computers, where black nodes represent servers and white nodes represent clients. In Figure 1(a), servers 3 and 5 do not form a perfect service because client 4 is adjacent to both servers 3 and 5 and thus it is served by two servers which contradicts the assumption. Conversely, servers 3 and 4 form a perfect service as shown in Figure 1(b). This set also has the minimum cardinality. Therefore, the perfect service number of this example equals two.


Your task is to write a program to compute the perfect service number.

Input 

The input consists of a number of test cases. The format of each test case is as follows: The first line contains one positive integer, N , which represents the number of computers in the network. The next N - 1lines contain all of the communication links and one line for each link. Each line is represented by two positive integers separated by a single space. Finally, a `0' at the (N + 1) -th line indicates the end of the first test case.

The next test case starts after the previous ending symbol `0'. A `-1' indicates the end of the whole inputs.

Output 

The output contains one line for each test case. Each line contains a positive integer, which is the perfect service number.

Sample Input 

6 
1 3
2 3
3 4
4 5
4 6
0 
2 
1 2
-1

Sample Output 

2 
1

题意:在一个树形的网络上,每个点是一个计算机,每个计算机可以当做服务器,一个计算机要么是服务器,要么和他直接连的计算机有服务器,问至少需要多少个服务器。

思路:dp[i][0 1 2]记录三种状况,当他是服务器时,子树的最小权值;当他不是服务器,并且和他直接相连的有服务器时,子树的最小权值;当他不是服务器,并且和他直接相连的没有服务器时,子树的最小权值。

AC代码如下:

#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
vector <int> vc[10010];
int dp[10010][3],vis[10010],t=0,INF=100000000;
void dfs(int u)
{ int len=vc[u].size(),i,j;
  vis[u]=t;
  dp[u][0]=1;dp[u][1]=0;dp[u][2]=0;
  vector <int > sub;
  for(i=0;i<len;i++)
   if(vis[vc[u][i]]!=t)
   { dfs(vc[u][i]);
     sub.push_back(vc[u][i]);
   }
  bool flag=false;
  len=sub.size();
  if(len==0)
  { dp[u][1]=INF;
    return;
  }
  for(i=0;i<len;i++)
  { dp[u][0]+=min(dp[sub[i]][0],min(dp[sub[i]][1],dp[sub[i]][2]));
    dp[u][2]+=dp[sub[i]][1];
    dp[u][1]+=min(dp[sub[i]][0],dp[sub[i]][1]);
    if(dp[sub[i]][0]<=dp[sub[i]][1])
     flag=true;
  }
  if(!flag)
  { int k=dp[u][1];
    dp[u][1]=k-dp[sub[0]][1]+dp[sub[0]][0];
    for(i=1;i<len;i++)
     dp[u][1]=min(dp[u][1],k-dp[sub[i]][1]+dp[sub[i]][0]);
  }
}
int main()
{ int n,m,i,j,k,u,v;
  while(true)
  { t++;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
     vc[i].clear();
    for(i=1;i<n;i++)
    { scanf("%d%d",&u,&v);
      vc[u].push_back(v);
      vc[v].push_back(u);
    }
    dfs(1);
    printf("%d\n",min(dp[1][0],dp[1][1]));
    scanf("%d",&k);
    if(k==-1)
     break;
  }
}



通过短时倒谱(Cepstrogram)计算进行时-倒频分析研究(Matlab代码实现)内容概要:本文主要介绍了一项关于短时倒谱(Cepstrogram)计算在时-倒频分析中的研究,并提供了相应的Matlab代码实现。通过短时倒谱分析方法,能够有效提取信号在时间与倒频率域的特征,适用于语音、机械振动、生物医学等领域的信号处理与故障诊断。文中阐述了倒谱分析的基本原理、短时倒谱的计算流程及其在实际工程中的应用价值,展示了如何利用Matlab进行时-倒频图的可视化与分析,帮助研究人员深入理解非平稳信号的周期性成分与谐波结构。; 适合人群:具备一定信号处理基础,熟悉Matlab编程,从事电子信息、机械工程、生物医学或通信等相关领域科研工作的研究生、工程师及科研人员。; 使用场景及目标:①掌握倒谱分析与短时倒谱的基本理论及其与傅里叶变换的关系;②学习如何用Matlab实现Cepstrogram并应用于实际信号的周期性特征提取与故障诊断;③为语音识别、机械设备状态监测、振动信号分析等研究提供技术支持与方法参考; 阅读建议:建议读者结合提供的Matlab代码进行实践操作,先理解倒谱的基本概念再逐步实现短时倒谱分析,注意参数设置如窗长、重叠率等对结果的影响,同时可将该方法与其他时频分析方法(如STFT、小波变换)进行对比,以提升对信号特征的理解能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值