zoukankan      html  css  js  c++  java
  • luogu P2899 [USACO08JAN]手机网络Cell Phone Network |贪心

    include

    include

    include

    include

    include

    include

    define db double

    using namespace std;
    const int N=1e5+10,M=2*N;
    int next[M],head[N],go[M],tot;
    inline void add(int u,int v){
    next[++tot]=head[u];head[u]=tot;go[tot]=v;
    next[++tot]=head[v];head[v]=tot;go[tot]=u;
    }
    struct node{
    int dep,id,f;
    bool operator<(const node &x)const{
    return dep<x.dep;
    }
    }a[M];
    bool vis[N];
    int n;
    void dfs(int x,int fa,int dep){
    a[x]=(node){dep,x,fa};
    for(int i=head[x];i;i=next[i]){
    int v=go[i];
    if(v==fa)continue;
    dfs(v,x,dep+1);
    }
    }
    priority_queueq;
    int main(){
    cin>>n;
    for(int i=1,u,v;i<n;i++){
    scanf("%d%d",&u,&v);
    add(u,v);
    }
    dfs(1,1,1);
    for(int i=1;i<=n;i++)q.push(a[i]);
    int ans=0;
    while(q.size()){
    node t=q.top();q.pop();
    if(vis[t.id])continue;
    vis[t.id]=1;
    vis[t.f]=1;
    ans++;
    for(int i=head[t.f];i;i=next[i]){
    int v=go[i];
    vis[v]=1;
    }
    }
    cout<<ans<<endl;
    }

  • 相关阅读:
    tablespaces
    转 房价
    Duplicate a whole line in Vim
    Dubbo+JStorm
    replace all
    ORACLE 最后表数据更新的时间
    list reverse
    python IDE
    string 方法
    java JIT AOT
  • 原文地址:https://www.cnblogs.com/naruto-mzx/p/11853462.html
Copyright © 2011-2022 走看看