• POJ3678 Labeling Balls 拓扑排序


      题目链接:http://poj.org/problem?id=3687

      题目有很多坑点,首先要求编号越小的节点在结果中拍在越前面,其次是输出节点所在的位置,而不是节点的编号!!

      如果正向建图,那么每次去掉入度为零的节点都要考虑其能到达的所有节点中的最小的,如果有最小的还要比较次小的。。。做起来很麻烦。可以考虑反向建图,那么每次选择入度为零且节点编号最大的节点,那么编号越大,越靠后,编号小的越靠前。

      1 //STATUS:C++_AC_63MS_416KB
      2 #include <functional>
      3 #include <algorithm>
      4 #include <iostream>
      5 //#include <ext/rope>
      6 #include <fstream>
      7 #include <sstream>
      8 #include <iomanip>
      9 #include <numeric>
     10 #include <cstring>
     11 #include <cassert>
     12 #include <cstdio>
     13 #include <string>
     14 #include <vector>
     15 #include <bitset>
     16 #include <queue>
     17 #include <stack>
     18 #include <cmath>
     19 #include <ctime>
     20 #include <list>
     21 #include <set>
     22 #include <map>
     23 using namespace std;
     24 //using namespace __gnu_cxx;
     25 //define
     26 #define pii pair<int,int>
     27 #define mem(a,b) memset(a,b,sizeof(a))
     28 #define lson l,mid,rt<<1
     29 #define rson mid+1,r,rt<<1|1
     30 #define PI acos(-1.0)
     31 //typedef
     32 typedef long long LL;
     33 typedef unsigned long long ULL;
     34 //const
     35 const int N=210;
     36 const int INF=0x3f3f3f3f;
     37 const int MOD=100000,STA=8000010;
     38 const LL LNF=1LL<<60;
     39 const double EPS=1e-8;
     40 const double OO=1e15;
     41 const int dx[4]={-1,0,1,0};
     42 const int dy[4]={0,1,0,-1};
     43 const int day[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
     44 //Daily Use ...
     45 inline int sign(double x){return (x>EPS)-(x<-EPS);}
     46 template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
     47 template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
     48 template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
     49 template<class T> inline T Min(T a,T b){return a<b?a:b;}
     50 template<class T> inline T Max(T a,T b){return a>b?a:b;}
     51 template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
     52 template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
     53 template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
     54 template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
     55 //End
     56 
     57 struct Edge{
     58     int u,v;
     59 }e[N*N];
     60 int first[N],next[N*N],vis[N],out[N],ans[N];
     61 int T,n,m,mt;
     62 
     63 void adde(int a,int b)
     64 {
     65     e[mt].u=a;e[mt].v=b;
     66     next[mt]=first[a],first[a]=mt++;
     67 }
     68 
     69 int topo()
     70 {
     71     int i,j,k=n,ok;
     72     mem(out,0);
     73     while(k--){
     74         mem(vis,0);
     75         for(i=1;i<=n;i++){
     76             for(j=first[i];j!=-1;j=next[j]){
     77                 vis[e[j].v]=1;
     78             }
     79         }
     80         ok=0;
     81         for(i=n;i>=1;i--)
     82             if(!vis[i] && !out[i]){ok=1;break;}
     83         if(ok==0)return 0;
     84         ans[k]=i;
     85         out[i]=1;
     86         first[i]=-1;
     87     }
     88     return 1;
     89 }
     90 
     91 int main()
     92 {
     93  //   freopen("in.txt","r",stdin);
     94     int i,a,b;
     95     scanf("%d",&T);
     96     while(T--)
     97     {
     98         scanf("%d%d",&n,&m);
     99         mem(first,-1);mt=0;
    100         while(m--){
    101             scanf("%d%d",&a,&b);
    102             adde(b,a);
    103         }
    104 
    105         if(topo()){
    106             for(i=0;i<n;i++)vis[ans[i]]=i+1;
    107             printf("%d",vis[1]);
    108             for(i=2;i<=n;i++)
    109                 printf(" %d",vis[i]);
    110         }
    111         else printf("-1");
    112         putchar('\n');
    113     }
    114     return 0;
    115 }
  • 相关阅读:
    Python学习笔记:重复索引处理方法
    爬虫学习笔记:8684公交路线
    爬虫学习笔记:创建随机UserAgent池
    Python学习笔记:pandas.to_csv设置encoding='utf8sig'解决中文乱码问题
    Python学习笔记:利用pd.assign新增一列
    查看端口
    进程与线程的一个简单解释
    设计模式工厂方法模式(Factory Method)
    Java抽象类与接口的区别
    十个JDBC的最佳实践
  • 原文地址:https://www.cnblogs.com/zhsl/p/3096612.html
Copyright © 2020-2023  润新知