hdu 5195 线段树

 



线段树还是比较薄弱,搞了好长时间,还可以用优先队列做
O((n+m)logn
  1 #include<cstdio>

  2 #include<iostream>

  3 #include<algorithm>

  4 #include<cstring>

  5 #include<cmath>

  6 #include<queue>

  7 using namespace std;

  8 #define for0n for(i=0;i<n;i++)

  9 #define for1n for(i=1;i<=n;i++)

 10 #define w12 while(scanf("%d%d",&n,&m)!=EOF)

 11 #define cl(a) memset(a,0,sizeof(a))

 12 int n,m,t,Min;

 13 #define lson l,mid,rt<<1

 14 #define rson mid+1,r,rt<<1|1

 15 #define root 1,n,1

 16 #define mid ((l+r)>>1)

 17 const int MAXN=100010;

 18 const int INF = 0x3f3f3f3f;

 19 int head[MAXN],minn[MAXN<<2],in[MAXN];

 20 void pushup(int rt){

 21     minn[rt]=min(minn[rt<<1],minn[rt<<1|1]);

 22 }

 23 void build(int l,int r,int rt){

 24     if(l==r){

 25         minn[rt]=in[l];

 26         return;

 27     }

 28     build(lson);

 29     build(rson);

 30     pushup(rt);

 31 }

 32 int query(int k,int l,int r,int rt) {

 33     if(minn[rt]>k)  return -1;

 34     if (l==r)

 35     {

 36         return l;

 37     }

 38     if(minn[rt<<1|1]<=k)   return query(k,rson);

 39     if(minn[rt<<1]<=k)     return query(k,lson);

 40 }

 41 void update(int pos,int val,int l,int r,int rt)

 42 {

 43     if(l==r)    minn[rt]=val;

 44     else

 45     {

 46         if(pos<=mid)  update(pos,val,lson);

 47         else update(pos,val,rson);

 48         pushup(rt);

 49     }

 50 }

 51 struct Edge

 52 {

 53     int to,next;

 54 }edge[MAXN];

 55 int tot=0;

 56 void init()

 57 {

 58     tot=0;

 59     memset(head,-1,sizeof(head));

 60     cl(in);

 61 }

 62 void addedge(int u,int v)

 63 {

 64     edge[tot].to=v;

 65     edge[tot].next=head[u];

 66     head[u]=tot++;

 67 }

 68 int main()

 69 {

 70     int i,j,k;

 71     #ifndef ONLINE_JUDGE

 72     freopen("1.in","r",stdin);

 73     #endif

 74     while(scanf("%d%d%d",&n,&m,&k)!=EOF)

 75     {

 76         init();

 77         while(m--)

 78         {

 79             int u,v;

 80             scanf("%d%d",&u,&v);

 81             addedge(u,v);

 82             in[v]++;

 83         }

 84         build(root);

 85         for1n

 86         {

 87             int id=query(k,root);        //得到小于k的最大编号的结点

 88             k-=in[id];

 89             in[id]=INF;

 90             update(id,INF,root);

 91             for(j=head[id];j!=-1;j=edge[j].next)

 92             {

 93                 int v=edge[j].to;

 94                 in[v]--;

 95                 update(v,in[v],root);

 96             }

 97             printf("%d",id);

 98             if(i<n)printf(" ");

 99             else printf("\n");

100         }

101     }

102 }

 

2015-07-23:优先队列

  1 #include<cstdio>

  2 #include<iostream>

  3 #include<algorithm>

  4 #include<cstring>

  5 #include<cmath>

  6 #include<queue>

  7 #include<map>

  8 using namespace std;

  9 #define MOD 1000000007

 10 const int INF=0x3f3f3f3f;

 11 const double eps=1e-5;

 12 typedef long long ll;

 13 #define cl(a) memset(a,0,sizeof(a))

 14 #define ts printf("*****\n");

 15 const int MAXN=100010;

 16 int n,m,tt;

 17 priority_queue<int> q;

 18 vector<int> vc[MAXN];

 19 int in[MAXN];

 20 int head[MAXN];

 21 struct Edge

 22 {

 23     int to,next;

 24 }edge[MAXN];

 25 int tot=0;

 26 void init()

 27 {

 28     tot=0;

 29     memset(head,-1,sizeof(head));

 30     cl(in);

 31 }

 32 void addedge(int u,int v)

 33 {

 34     edge[tot].to=v;

 35     edge[tot].next=head[u];

 36     head[u]=tot++;

 37 }

 38 bool vis[MAXN];

 39 int main()

 40 {

 41     int i,j,k;

 42     #ifndef ONLINE_JUDGE

 43     freopen("1.in","r",stdin);

 44     #endif

 45     while(~scanf("%d%d%d",&n,&m,&k))

 46     {

 47         init();

 48         cl(in);

 49         cl(vis);

 50         bool flag=0;

 51         int u,v;

 52         for(i=1;i<=n;i++)

 53         {

 54             vc[i].clear();

 55         }

 56         for(i=0;i<m;i++)

 57         {

 58             scanf("%d%d",&u,&v);

 59             addedge(u,v);

 60             in[v]++;

 61             vc[u].push_back(v);

 62         }

 63         for(i=1;i<=n;i++)

 64         {

 65             if(in[i]<=k)

 66             {

 67                 q.push(i);

 68                 vis[i]=1;

 69             }

 70         }

 71         while(!q.empty())

 72         {

 73             int now=q.top();

 74             q.pop();

 75             if(k<in[now])

 76             {

 77                 vis[now]=0;

 78                 continue;

 79             }

 80             k-=in[now];

 81             for(i=0;i<vc[now].size();i++)

 82             {

 83                 int x=vc[now][i];

 84                 in[x]--;

 85                 if(!vis[x]&&in[x]<=k)

 86                 {

 87                     vis[x]=1;

 88                     q.push(x);

 89                 }

 90             }

 91             if(!flag)

 92             {

 93                 printf("%d",now);

 94                 flag=1;

 95             }

 96             else

 97                 printf(" %d",now);

 98         }

 99         printf("\n");

100     }

101 }
View Code

 

你可能感兴趣的:(HDU)