• poj1094(Sorting It All Out)


    题目大意:

        输入n,m。n代表有几个大写字母从'A'开始(A、B...)。m代表m对字母的间的关系。通过判断输出数据间的关系。

    输出有三种:

    一定要理解清楚题目意思!
    
    Sorted sequence determined after 4 relations: ABCD.  
    输入前4个关系后,决定出了一个序列(关系总数可能大于4)
    
    Inconsistency found after 2 relations.
    输入前2个关系后,冲突出现了(环,关系总数可能大于2)
    
    Sorted sequence cannot be determined.
    所有关系输入后,序列和冲突均无出现

    解题思路:
    拓扑排序。最重要的是输出,本来以为题目有问题,后来仔细想了想题目的意思。感觉没有什么不对,弄清楚输出的的意思就不会WA。
    方法:每输入一组数据就判断一下情况,因为题目的意思是输出的时候是看你通过第几组数据就可以将所有数据判断出来结果,所以需要每次输入都判别一下。
    1.如果通过前面t组数据可以判断出n个字母的的顺序即直接输出
    Sorted sequence determined after t relations:....
    后面无论输入什么数据,或者形成环都不需要问。

    2.如果通过前面t组数据可以判断出n个字母形成环即直接输出
    Inconsistency found after t relations.

    3.如果输入不出现任何矛盾的问题并且判断不出来字母的顺序即输出:
    Sorted sequence cannot be determined.

    代码:
      1 #include <algorithm>
      2 #include <iostream>
      3 #include <sstream>
      4 #include <cstdlib>
      5 #include <cstring>
      6 #include <cstdio>
      7 #include <string>
      8 #include <bitset>
      9 #include <vector>
     10 #include <queue>
     11 #include <stack>
     12 #include <cmath>
     13 #include <list>
     14 //#include <map>
     15 #include <set>
     16 using namespace std;
     17 /***************************************/
     18 #define ll long long
     19 #define int64 __int64
     20 /***************************************/
     21 const int INF = 0x7f7f7f7f;
     22 const double eps = 1e-8;
     23 const double PIE=acos(-1.0);
     24 const int d1x[]= {0,-1,0,1};
     25 const int d1y[]= {-1,0,1,0};
     26 const int d2x[]= {0,-1,0,1};
     27 const int d2y[]= {1,0,-1,0};
     28 const int fx[]= {-1,-1,-1,0,0,1,1,1};
     29 const int fy[]= {-1,0,1,-1,1,-1,0,1};
     30 /***************************************/
     31 void openfile()
     32 {
     33     freopen("data.in","rb",stdin);
     34     freopen("data.out","wb",stdout);
     35 }
     36 /**********************华丽丽的分割线,以上为模板部分*****************/
     37 
     38 int map[50][50];
     39 int cnt[50];
     40 int cnt1[50];
     41 int top[50];
     42 int vis[50];
     43 int n,m;
     44 int ce1;
     45 int flag;
     46 int topo(int txt)
     47 {
     48     int i,j,k;
     49     int sum=0;
     50     int d=0;
     51     int ce4=0;
     52     memset(top,0,sizeof(top));
     53     for(i=0; i<n; i++)
     54     {
     55         for(sum=0,j=0; j<n; j++)
     56         {
     57             if (!cnt[j])
     58                 sum++;
     59         }
     60         if (sum>1)
     61         {
     62             ce4=1;
     63         //    if (txt+1==m)
     64              //   printf("Sorted sequence cannot be determined.
    ");
     65          //   return 0;
     66         }
     67         for(j=0; j<n; j++)
     68         {
     69             if (!cnt[j])
     70             {
     71                 cnt[j]=-1;
     72                 top[d++]=j;
     73                 for(k=0; k<n; k++)
     74                 {
     75                     if (map[j][k])
     76                     {
     77                         cnt[k]--;
     78                     }
     79                 }
     80                 break;
     81             }
     82         }
     83     }
     84     if(ce4&&d==n)
     85         return 0;
     86     ce1=txt+1;
     87     if (d==n)
     88     {
     89         flag=1;
     90         return 0;
     91     }
     92     flag=2;
     93     //if(txt+1==m)
     94       //  printf("Inconsistency found after %d relations.
    ",txt+1);
     95     return 0;
     96 }
     97 int main()
     98 {
     99     while(scanf("%d%d",&n,&m)&&(n+m))
    100     {
    101         int i,j;
    102         char a,b,c;
    103         int x,y;
    104         memset(map,0,sizeof(map));
    105         memset(vis,0,sizeof(vis));
    106         memset(cnt,0,sizeof(cnt));
    107         memset(cnt1,0,sizeof(cnt1));
    108         for(i=0; i<n; i++)
    109             cnt[i]=0;
    110         int ce;
    111         ce1=0;
    112         flag=0;
    113         for(i=0; i<m; i++)
    114         {
    115             getchar();
    116             scanf("%c%c%c",&a,&b,&c);
    117             x=a-'A';
    118             y=c-'A';
    119             if (map[x][y]==0)
    120             {
    121                 map[x][y]=1;
    122                 cnt1[y]++;
    123             }
    124             for(j=0; j<n; j++)
    125                 cnt[j]=cnt1[j];
    126             if(!ce1)
    127                 topo(i);
    128         }
    129         if (flag==1)
    130         {
    131             printf("Sorted sequence determined after %d relations: ",ce1);
    132             for(i=0; i<n; i++)
    133                 printf("%c",top[i]+'A');
    134             printf(".
    ");
    135             continue;
    136         }
    137         if (flag==2)
    138         {
    139             printf("Inconsistency found after %d relations.
    ",ce1);
    140             continue;
    141         }
    142         printf("Sorted sequence cannot be determined.
    ");
    143     }
    144     return 0;
    145 }
    View Code
     


    屌丝终有逆袭日,*******。
  • 相关阅读:
    在 TB1 机器上编译并调试 TB 自带sample的方法
    C++编译错误 fatal error C1189: #error : The C++ Standard Library forbids macroizing keywords. Enable warning
    c++生成随机数
    Unity调用讯飞做语音听写(Android Studio版)
    TouchDesigner 编译FlexChop
    Behavior Designer知识点
    清除Unity缓存
    UnityEngine.UI.dll is in timestamps but is not known in assetdatabase
    Unity插件学习记录 -- SW Actions
    Unity3D 使用LineRenderer制作画板功能
  • 原文地址:https://www.cnblogs.com/ZhaoPengkinghold/p/3839641.html
Copyright © 2020-2023  润新知