zoukankan      html  css  js  c++  java
  • 拓扑排序

    感觉总是无视拓扑排序也不太好,就来了2道模板题。。不造哪个时候又给忘了。。

    codevs 1833

     1 #include<bits/stdc++.h>
     2 #define inc(i,l,r) for(int i=l;i<=r;i++)
     3 #define dec(i,l,r) for(int i=l;i>=r;i--)
     4 #define link(x) for(edge *j=h[x];j;j=j->next)
     5 #define mem(a) memset(a,0,sizeof(a))
     6 #define inf 1e9
     7 #define ll long long
     8 #define succ(x) (1<<x)
     9 #define NM 10000+5
    10 #define nm 25000+5
    11 using namespace std;
    12 int read(){
    13     int x=0,f=1;char ch=getchar();
    14     while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    15     while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    16     return x*f;
    17 }
    18 struct edge{
    19     int t;
    20     edge *next;
    21 }e[nm],*h[NM],*o=e;
    22 void add(int x,int y){
    23     o++;o->t=y;o->next=h[x];h[x]=o;
    24 }
    25 int n,m,v[NM],s,_x,_y;
    26 queue<int >q;
    27 void bfs(){
    28     inc(i,1,n)if(!v[i])q.push(i);
    29     while(!q.empty()){
    30         int t=q.front();q.pop();
    31         link(t)if(v[j->t]){
    32             v[j->t]--;
    33             if(!v[j->t])q.push(j->t);
    34         }
    35     }
    36 }
    37 int main(){
    38     n=read();m=read();
    39     inc(i,1,m){
    40         _x=read();_y=read();
    41         add(_x,_y);v[_y]++;
    42     }
    43     bfs();
    44     inc(i,1,n)if(v[i])s++;
    45     if(!s)puts("o(∩_∩)o");
    46     else printf("T_T
    %d
    ",s);
    47     return 0;
    48 }
    View Code

    hdu 1285 顺便学了优先队列

     1 //#include<bits/stdc++.h>
     2 #include<cstdio>
     3 #include<algorithm>
     4 #include<iostream>
     5 #include<cstring>
     6 #include<queue>
     7 #define inc(i,l,r) for(int i=l;i<=r;i++)
     8 #define dec(i,l,r) for(int i=l;i>=r;i--)
     9 #define link(x) for(edge *j=h[x];j;j=j->next)
    10 #define mem(a) memset(a,0,sizeof(a))
    11 #define inf 1e9
    12 #define ll long long
    13 #define succ(x) (1<<x)
    14 #define NM 500+5
    15 using namespace std;
    16 int read(){
    17     int x=0,f=1;char ch=getchar();
    18     while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    19     while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    20     return x*f;
    21 }
    22 struct edge{
    23     int t;
    24     edge *next;
    25 }e[NM*NM],*h[NM],*o;
    26 int n,m,v[NM],_x,_y,f;
    27 struct cmp{
    28     bool operator ()(int &a,int &b){
    29         return a>b;
    30     }
    31 };
    32 priority_queue<int,vector<int>,cmp>q;
    33 void add(int x,int y){
    34     o++;o->t=y;o->next=h[x];h[x]=o;
    35 }
    36 void bfs(){
    37     inc(i,1,n)if(!v[i])q.push(i);
    38     while(!q.empty()){
    39         int t=q.top();q.pop();v[t]--;
    40         if(f)putchar(' ');
    41         printf("%d",t);f++;
    42         link(t){
    43             v[j->t]--;
    44             if(v[j->t]==0)q.push(j->t);
    45         }
    46     }
    47 }
    48 int main(){
    49 //    freopen("data.in","r",stdin);
    50 //    freopen("test.out","w",stdout);
    51     while(scanf("%d%d",&n,&m)!=EOF){
    52         mem(v);mem(h);o=e;f=0;
    53     inc(i,1,m){
    54         _x=read();_y=read();
    55         add(_x,_y);v[_y]++;
    56     }
    57     bfs();
    58     printf("
    ");
    59     }
    60     return 0;
    61 }
    View Code

     15noip D1T2貌似也可以拓扑?

  • 相关阅读:
    [转] 32位 PL/SQL Develope r如何连接64位的Oracle 图解
    UML对象图和包图
    推断的距离1970年1一个月1天从日期数
    Cocos2d-x3.0 RenderTexture(一) 保存
    翻转名单
    Session or Cookie?是否有必要使用Tomcat等一下Web集装箱Session
    【Socket规划】套接字Windows台C语言
    配置主机路由表(route)(两)
    springMVC3得知(五岁以下儿童)--MultiActionController
    JQUERY 选择
  • 原文地址:https://www.cnblogs.com/onlyRP/p/5173255.html
Copyright © 2011-2022 走看看