zoukankan      html  css  js  c++  java
  • P2279 [HNOI2003]消防局的设立

    P2279 [HNOI2003]消防局的设立
    考场上想出了贪心策略,但是处理细节时有点问题,gg了。从(当前深度最大的节点)叶子节点往上跳k个,在这里设消防局,并从消防局遍历k个距离,标记上。

      1 #include<iostream>
      2 #include<cstdio>
      3 #include<queue>
      4 #include<algorithm>
      5 #include<cmath>
      6 #include<ctime>
      7 #include<set>
      8 #include<map>
      9 #include<stack>
     10 #include<cstring>
     11 #define inf 2147483647
     12 #define For(i,a,b) for(register int i=a;i<=b;i++)
     13 #define p(a) putchar(a)
     14 #define g() getchar()
     15 //by war
     16 //2017.11.1
     17 using namespace std;
     18 int n,x,k=2;
     19 int d[1010];
     20 int cnt;
     21 int ans;
     22 bool vis[1010];
     23 struct node 
     24 {
     25     int n;
     26     node *next;
     27 }*e[2010];
     28 
     29 struct nod
     30 {
     31     int deep;
     32     bool operator<(const nod&aa)const
     33     {
     34         return deep>aa.deep;
     35     }
     36     int pos;
     37 }a[1010];
     38 
     39 void in(int &x)
     40 {
     41     int y=1;
     42     char c=g();x=0;
     43     while(c<'0'||c>'9')
     44     {
     45     if(c=='-')
     46     y=-1;
     47     c=g();
     48     }
     49     while(c<='9'&&c>='0')x=(x<<1)+(x<<3)+c-'0',c=g();
     50     x*=y;
     51 }
     52 void o(int x)
     53 {
     54     if(x<0)
     55     {
     56         p('-');
     57         x=-x;
     58     }
     59     if(x>9)o(x/10);
     60     p(x%10+'0');
     61 }
     62 
     63 void push(int x,int y)
     64 {
     65     node *p;
     66     p=new node();
     67     p->n=y;
     68     if(e[x]==NULL)
     69     e[x]=p;
     70     else
     71     {
     72         p->next=e[x]->next;
     73         e[x]->next=p;
     74     }
     75 }
     76 
     77 void dfs(int x,int dis)
     78 {
     79     if(dis>k)
     80     return;
     81     vis[x]=true;
     82     cnt--;    
     83     for(node *i=e[x];i!=NULL;i=i->next)
     84     dfs(i->n,dis+1);
     85 }
     86 
     87 int main()
     88 {
     89     in(n);
     90     cnt=n;
     91     For(i,2,n)
     92     {
     93         in(x);
     94         push(x,i);
     95         push(i,x);
     96         a[i].deep=a[x].deep+1;
     97         d[i]=x;
     98         a[i].pos=i;
     99     }
    100     a[1].pos=1;
    101     d[1]=1;
    102     sort(a+1,a+n+1);
    103     while(cnt>0)
    104     {
    105         For(i,1,n)
    106         if(!vis[a[i].pos])
    107         {
    108             x=a[i].pos;
    109             For(j,1,k)
    110             x=d[x];
    111             dfs(x,0);
    112             ans++;
    113         }
    114     }
    115     o(ans);
    116      return 0;
    117 }
  • 相关阅读:
    [转]nginx+fastcgi+c/c++搭建高性能Web框架
    [转]向facebook学习,通过协程实现mysql查询的异步化
    [转]ReactPHP── PHP版的Node.js
    [转]nodejs npm常用命令
    [LINK]php的三种CLI常量:STDIN,STDOUT,STDERR
    苹果iPhone如何区分港版、国行、水货
    16进制字符串转36进制字符串
    设置apache https服务
    [转]Class 'ThinkLog' not found
    PHP PSR规范
  • 原文地址:https://www.cnblogs.com/war1111/p/7767903.html
Copyright © 2011-2022 走看看