P2279 [HNOI2003]消防局的设立
考场上想出了贪心策略,但是处理细节时有点问题,gg了。从(当前深度最大的节点)叶子节点往上跳k个,在这里设消防局,并从消防局遍历k个距离,标记上。
#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cmath>
#include<ctime>
#include<set>
#include<map>
#include<stack>
#include<cstring>
#define inf 2147483647
#define For(i,a,b) for(register int i=a;i<=b;i++)
#define p(a) putchar(a)
#define g() getchar()
//by war
//2017.11.1
using namespace std;
int n,x,k=;
int d[];
int cnt;
int ans;
bool vis[];
struct node
{
int n;
node *next;
}*e[]; struct nod
{
int deep;
bool operator<(const nod&aa)const
{
return deep>aa.deep;
}
int pos;
}a[]; void in(int &x)
{
int y=;
char c=g();x=;
while(c<''||c>'')
{
if(c=='-')
y=-;
c=g();
}
while(c<=''&&c>='')x=(x<<)+(x<<)+c-'',c=g();
x*=y;
}
void o(int x)
{
if(x<)
{
p('-');
x=-x;
}
if(x>)o(x/);
p(x%+'');
} void push(int x,int y)
{
node *p;
p=new node();
p->n=y;
if(e[x]==NULL)
e[x]=p;
else
{
p->next=e[x]->next;
e[x]->next=p;
}
} void dfs(int x,int dis)
{
if(dis>k)
return;
vis[x]=true;
cnt--;
for(node *i=e[x];i!=NULL;i=i->next)
dfs(i->n,dis+);
} int main()
{
in(n);
cnt=n;
For(i,,n)
{
in(x);
push(x,i);
push(i,x);
a[i].deep=a[x].deep+;
d[i]=x;
a[i].pos=i;
}
a[].pos=;
d[]=;
sort(a+,a+n+);
while(cnt>)
{
For(i,,n)
if(!vis[a[i].pos])
{
x=a[i].pos;
For(j,,k)
x=d[x];
dfs(x,);
ans++;
}
}
o(ans);
return ;
}