標籤:idt 多少 desc i++ seq service source stream mat
Cell Phone Network
| Time Limit: 1000MS |
|
Memory Limit: 65536K |
| Total Submissions: 6781 |
|
Accepted: 2429 |
Description
Farmer John has decided to give each of his cows a cell phone in hopes to encourage their social interaction. This, however, requires him to set up cell phone towers on his N (1 ≤ N ≤ 10,000) pastures (conveniently numbered 1..N) so they can all communicate.
Exactly N-1 pairs of pastures are adjacent, and for any two pastures A and B (1 ≤ A ≤ N; 1 ≤ B ≤ N; A ≠ B) there is a sequence of adjacent pastures such that A is the first pasture in the sequence and B is the last. Farmer John can only place cell phone towers in the pastures, and each tower has enough range to provide service to the pasture it is on and all pastures adjacent to the pasture with the cell tower.
Help him determine the minimum number of towers he must install to provide cell phone service to each pasture.
Input
* Line 1: A single integer: N
* Lines 2..N: Each line specifies a pair of adjacent pastures with two space-separated integers: A and B
Output
* Line 1: A single integer indicating the minimum number of towers to install
Sample Input
51 35 24 33 5
Sample Output
2
Source
USACO 2008 January Gold【分析】給你一棵樹,當你給一個節點染色時,他的相鄰節點都會被染上色,問最少選擇多少節點染色,可以將整棵樹染上。 這是樹的最小支配集模板題,貪心做的。
#include <iostream>#include <cstring>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <time.h>#include <string>#include <map>#include <stack>#include <vector>#include <set>#include <queue>#define inf 10000000#define mod 10000typedef long long ll;using namespace std;const int N=2e4+10;const int M=50000;int in[N],vis[N];int n,m,k,tim=0;int newpos[N],p[N];vector<int>vec[N],edg[N];bool s[N],se[N];void dfs(int u,int fa){ newpos[tim++]=u; // printf("%d %d\n",tim-1,newpos[tim-1]); for(int i=0;i<edg[u].size();i++){ int v=edg[u][i]; if(v!=fa){ p[v]=u; dfs(v,u); } }}int greedy(){ int ans=0; int i; for(i=n-1; i>=0; i--) { int t=newpos[i]; //printf("%d %d %d\n",t,p[t],p[p[t]]); if(!s[t]) { if(!se[p[t]]) { se[p[t]]=true; ans++; } s[t]=true; s[p[t]]=true; s[p[p[t]]]=true; } } return ans;}int main(){ int u,v,ans=0;; scanf("%d",&n); for(int i=1; i<n; i++) { scanf("%d%d",&u,&v); edg[u].push_back(v); edg[v].push_back(u); } dfs(1,0); printf("%d\n",greedy()); return 0;}
POJ 3659 Cell Phone Network(樹的最小支配集)(貪心)