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;
}

luogu P2899 [USACO08JAN]手机网络Cell Phone Network |贪心

上一篇:【CF2A Winner】题解


下一篇:Android android studio常用的一些快捷键以及常用权限