UVa1218 Perfect service[树规]
来源:互联网 发布:双休日算法定节假日吗 编辑:程序博客网 时间:2024/06/05 05:33
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 - 1 lines 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.
0’. A `-1’ indicates the end of the whole inputs.
The next test case starts after the previous ending symbol
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
题意:
对一棵给定的树,求所有使得未被选择的点相连的点里都有且只有一个点被选择时,最小选择的点数。
分析:
第二维表示三种情况,被儿子覆盖,被父亲覆盖,自己覆盖自己。这道题的转移方程和最小点覆盖稍有不同,因为相连的点里只能有一个点被选。感觉这道题对于理清思路很有用。
有坑:说好的每个数据一行,但如果输入节点是0的树不换行,被WA了好几次,简直。
#include<cstdio>#include<iostream>#include<cmath>#include<cstdlib>#include<cstring>#include<string>#include<algorithm>#include<queue>#include<set>#include<map>#include<stack>#include<vector>#include<ctime>#define ll long long #define inf 2e8#define modd 1e9+7#define clr(x) memset(x,0,sizeof(x))#define maxen(x) memset(x,127,sizeof(x))#define maxer(x) memset(x,31,sizeof(x))#define each(i,n) for(int i=1;i<=n;i++)#define minn(a,b,c) min(a,min(b,c))#define maxx(a,b,c) max(a,max(b,c))#ifdef WIN32#define lld "%I64d"#else#define lld "%lld"#endif#define PROC "UVa1218"//for(int i=1;i<=n;i++)//(double) (ll) LL (int)//(double)clock()/CLOCKS_PER_SECusing namespace std;const int Maxn=1e4+5;int sta,fin;int n,f[Maxn][3],fr[Maxn],tov[2*Maxn],des[2*Maxn];int read(){ int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f;}void addedge(int cur){ sta=read(); fin=read(); int tmp1=2*cur-1; int tmp2=2*cur; tov[tmp1]=fr[sta];tov[tmp2]=fr[fin]; fr[sta]=tmp1;fr[fin]=tmp2; des[tmp1]=fin;des[tmp2]=sta;}void init(){ int tmp; n=read(); clr(fr); each(i,n-1) addedge(i); clr(f);}void dfs(int cur,int fa){ int mini=Maxn; for(int i=fr[cur];i;i=tov[i]) if(des[i]!=fa){ dfs(des[i],cur); f[cur][0]+=f[des[i]][0]; f[cur][1]+=min(f[des[i]][1],f[des[i]][2]); f[cur][2]+=f[des[i]][0]; mini=min(mini,f[des[i]][1]-f[des[i]][0]); } if(mini!=Maxn)f[cur][0]+=mini; if(!f[cur][0])f[cur][0]=Maxn; f[cur][1]++;}void work(){ if(n==0)return; if(n==-1)return; dfs(1,1); printf("%d\n",min(f[1][0],f[1][1]));}void debug(){ //}int main(){ freopen(PROC".in","r",stdin); freopen(PROC".out","w",stdout); while(n!=-1){ init(); work(); scanf("%d",&n); } //debug(); return 0;}
- UVa1218 Perfect service[树规]
- UVa1218 Perfect Service
- UVA1218 - Perfect Service
- UVA1218 Perfect Service
- UVa1218 Perfect Service
- UVA1218[Perfect Service] 树上动态规划模型
- Uva1218 Perfect Services
- UVa1218
- POJ 3398 Perfect Service
- POJ-3398-Perfect Service
- UVA 1218Perfect Service
- 1218 - Perfect Service
- UVa 1218 Perfect Service
- Uva-1218 Perfect Service
- UVA-1218 Perfect Service
- UVa-1228 Perfect Service
- UVA 1218 Perfect Service
- POJ 3398 Perfect Service
- ffmpeg命令详解
- Redis使用方法介绍
- MPI (Message Passing Interface) materials
- ansible小结(四)ansible.cfg与默认配置
- HTML 系列教程
- UVa1218 Perfect service[树规]
- ansible小结(五)Dynamic Inventory
- Eclipse使用maven,一步步搭建spring mvc
- Android apk安装及编译过程
- ListView嵌套listView问题
- 面向对象基本概念
- ansible小结(六)Ad-hoc与commands模块
- sublime超实用快捷键
- 卡特兰数和斯特林数(转载)加深理解