poj 3659 Cell Phone Network 贪心

来源:互联网 发布:2017中超守门员数据 编辑:程序博客网 时间:2024/05/19 19:14
这个题目本来应该用简单的树形dp来做的
但是一开始就想错了
先用了统计叶子节点的做法
但是因为中间会出现一些已经亮了但是没有建造灯塔的点
最后就用贪心一贪到底了
具体做法见代码
#include <iostream>#include <stdio.h>#include <string.h>using namespace std;const int maxn=11111;struct{    int next,to,ture;}e[maxn<<2];int head[maxn],lon,text[maxn];void edgemake(int from,int to){    e[++lon].next=head[from];    e[lon].to=to;    head[from]=lon;}int cnt[maxn],col[maxn];int ans;int dfs(int t){    for(int k=head[t];k!=-1;k=e[k].next)    {        if(e[k].ture!=1) continue;        int u=e[k].to;        dfs(u);    }    for(int k=head[t];k!=-1;k=e[k].next)    {        int u=e[k].to;        if(e[k].ture!=1) continue;        if(col[u]==1)        {            ans++;            col[t]=3;            return(0);        }    }    for(int k=head[t];k!=-1;k=e[k].next)    {        int u=e[k].to;        if(e[k].ture!=1) continue;        if(col[u]==3)        {            col[t]=2;            return(0);        }    }    col[t]=1;}int dfs1(int t){    text[t]=1;    for(int k=head[t];k!=-1;k=e[k].next)    {        int u=e[k].to;        if(!text[u])        {            e[k].ture=1;            dfs1(u);        }    }}int main(){//    freopen("in.txt","r",stdin);    int n;    scanf("%d",&n);    if(n==1)    {        printf("1\n");        return(0);    }    memset(col,0,sizeof(col));    memset(head,-1,sizeof(head));    memset(text,0,sizeof(text));    memset(e,0,sizeof(e));    lon=0;    for(int i=1;i<n;i++)    {        int from,to;        scanf("%d %d",&from,&to);        edgemake(from,to);        edgemake(to,from);    }    ans=0;    dfs1(1);    dfs(1);    printf("%d\n",ans+(col[1]==1));    return 0;}