zoukankan      html  css  js  c++  java
  • zoj 1060 Sorting It All Out

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1060

    解题思路:拓扑排序

      1 ///////////////////////////////////////////////////////////////////////////
      2 //problem_id: zoj 1060
      3 //user_id: SCNU20102200088
      4 ///////////////////////////////////////////////////////////////////////////
      5 
      6 #include <algorithm>
      7 #include <iostream>
      8 #include <iterator>
      9 #include <iomanip>
     10 #include <cstring>
     11 #include <cstdlib>
     12 #include <string>
     13 #include <vector>
     14 #include <cstdio>
     15 #include <cctype>
     16 #include <cmath>
     17 #include <queue>
     18 #include <stack>
     19 #include <list>
     20 #include <set>
     21 #include <map>
     22 using namespace std;
     23 
     24 ///////////////////////////////////////////////////////////////////////////
     25 #pragma comment(linker,"/STACK:1024000000,1024000000")
     26 
     27 #define lson l,m,rt<<1
     28 #define rson m+1,r,rt<<1|1
     29 ///////////////////////////////////////////////////////////////////////////
     30 
     31 ///////////////////////////////////////////////////////////////////////////
     32 const double EPS=1e-9;
     33 const double PI=acos(-1.0);
     34 const double E=2.7182818284590452353602874713526;
     35 
     36 const int x4[]={-1,0,1,0};
     37 const int y4[]={0,1,0,-1};
     38 const int x8[]={-1,-1,0,1,1,1,0,-1};
     39 const int y8[]={0,1,1,1,0,-1,-1,-1};
     40 ///////////////////////////////////////////////////////////////////////////
     41 
     42 ///////////////////////////////////////////////////////////////////////////
     43 typedef long long LL;
     44 
     45 typedef int T;
     46 T max(T a,T b){ return a>b? a:b; }
     47 T min(T a,T b){ return a<b? a:b; }
     48 T gcd(T a,T b){ return b==0? a:gcd(b,a%b); }
     49 T lcm(T a,T b){ return a/gcd(a,b)*b; }
     50 ///////////////////////////////////////////////////////////////////////////
     51 
     52 ///////////////////////////////////////////////////////////////////////////
     53 //Add Code:
     54 int n,m,rd[30];
     55 char ans[30];
     56 vector<int> v[30];
     57 
     58 int TopSort(int k){
     59     int i,j,res=0,temp[30];
     60     for(i=1;i<=n;i++) temp[i]=rd[i];
     61     bool flag=1;
     62     while(k--){
     63         int cnt=0;
     64         for(i=1;i<=n;i++){
     65             if(temp[i]==0){
     66                 cnt++;
     67                 j=i;
     68             }
     69         }
     70         if(cnt==0) return -1;
     71         if(cnt>=1){
     72             if(cnt>1) flag=0;
     73             for(i=0;i<v[j].size();i++) temp[v[j][i]]--;
     74             ans[res++]=j+'A'-1;
     75             temp[j]=-1;
     76             ans[res]=0;
     77         }
     78     }
     79     if(flag) return res;
     80     return 0;
     81 }
     82 ///////////////////////////////////////////////////////////////////////////
     83 
     84 int main(){
     85     ///////////////////////////////////////////////////////////////////////
     86     //Add Code:
     87     int i,cnt;
     88     char c[5];
     89     bool flag[30];
     90     while(scanf("%d%d",&n,&m)!=EOF){
     91         if(n==0 && m==0) break;
     92         memset(rd,0,sizeof(rd));
     93         memset(flag,0,sizeof(flag));
     94         for(i=0;i<30;i++) v[i].clear();
     95         int num=0,judge=0;
     96         for(i=1;i<=m;i++){
     97             scanf("%s",c);
     98             if(judge==0){
     99                 int a=c[0]-'A'+1,b=c[2]-'A'+1;
    100                 rd[b]++;
    101                 v[a].push_back(b);
    102                 if(!flag[a]){
    103                     num++;
    104                     flag[a]=1;
    105                 }
    106                 if(!flag[b]){
    107                     num++;
    108                     flag[b]=1;
    109                 }
    110                 int ret=TopSort(num);
    111                 if(ret==-1){
    112                     judge=-1;
    113                     cnt=i;
    114                 }
    115                 else if(ret==n){
    116                     judge=1;
    117                     cnt=i;
    118                 }
    119             }
    120         }
    121         if(judge==-1) printf("Inconsistency found after %d relations.
    ",cnt);
    122         else if(judge==0) printf("Sorted sequence cannot be determined.
    ");
    123         else printf("Sorted sequence determined after %d relations: %s.
    ",cnt,ans);
    124     }
    125     ///////////////////////////////////////////////////////////////////////
    126     return 0;
    127 }
    128 
    129 ///////////////////////////////////////////////////////////////////////////
    130 /*
    131 Testcase:
    132 Input:
    133 4 6
    134 A<B
    135 A<C
    136 B<C
    137 C<D
    138 B<D
    139 A<B
    140 3 2
    141 A<B
    142 B<A
    143 26 1
    144 A<Z
    145 0 0
    146 Output:
    147 Sorted sequence determined after 4 relations: ABCD.
    148 Inconsistency found after 2 relations.
    149 Sorted sequence cannot be determined.
    150 */
    151 ///////////////////////////////////////////////////////////////////////////
  • 相关阅读:
    shell脚本根据端口号kill掉进程
    使用netstat -ano 查看机器端口的占用情况(windows环境)
    分享一两个小工具,
    将压缩文件伪装图片格式文件以及将python文件转化为exe文件(测试完,真的有效)
    celery 异步任务 周期任务 定时任务的实现
    wsgi、uwsgi、asgi协议的关系
    centos7忘记密码更改步骤
    工作遇到的坑以及自己的学习悟道之道
    案例小集锦
    asp.net mvc部署
  • 原文地址:https://www.cnblogs.com/linqiuwei/p/3329088.html
Copyright © 2011-2022 走看看