Poj 1094 拓扑排序 水题

Sad..这么水的题WA了无数发,题目要看仔细啊,留下来做个警告把

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <climits>
#include <string>
#include <iostream>
#include <map>
#include <cstdlib>
#include <list>
#include <set>
#include <queue>
#include <stack>

using namespace std;

typedef long long LL;
const int maxn = 30;

int N,M,fa[maxn],incnt[maxn];
bool vis[maxn],sorted[maxn];
int mp[maxn][maxn];

int toposort(vector<int> &ans) {
    int tmp[maxn];
    bool flag = true;
    ans.clear();
    memset(vis,0,sizeof(vis));
    for(int i = 1;i <= N;i++) tmp[i] = incnt[i];
    for(int k = 1;k <= N;k++) {
        int x,ncnt = 0;
        for(int i = 1;i <= N;i++) if(!vis[i] && tmp[i] == 0) {
            x = i; ncnt++;
        }
        if(ncnt > 1) flag = false; //就算不能判断出来,不能直接return,还要继续判断是否有环
        if(ncnt == 0) return -1;
        vis[x] = true;
        ans.push_back(x);
        for(int i = 1;i <= N;i++) if(!vis[i] && mp[x][i] == 1) {
            tmp[i]--;
        }
    }
    return flag;
}

int main() {
    while(scanf("%d%d",&N,&M),N) {
        vector<int> ans;
        memset(mp,-1,sizeof(mp));
        memset(incnt,0,sizeof(incnt));
        bool determined = false,inconsistency = false;
        int cnt = 1,kase;
        for(int i = 1;i <= M;i++) {
            char buf[10]; scanf("%s",buf);
            int a = buf[0] - ‘A‘ + 1,b = buf[2] - ‘A‘ + 1;
            if(inconsistency || determined) continue;
            if(mp[a][b] == -1) incnt[b]++;
            mp[a][b] = true;
            int ret = toposort(ans);
            if(ret == 1) determined = true,kase = i;
            if(ret == -1) inconsistency = true;
            if(inconsistency) {
                printf("Inconsistency found after %d relations.\n",i);
                continue;
            }
        }
        if(!inconsistency) {
            if(determined) {
                printf("Sorted sequence determined after %d relations: ",kase);
                for(int i = 0;i < ans.size();i++) printf("%c",ans[i] - 1 + ‘A‘);
                puts(".");
            } else puts("Sorted sequence cannot be determined.");
        }
    }
    return 0;
}

  

Poj 1094 拓扑排序 水题

时间: 2024-10-23 20:50:21

Poj 1094 拓扑排序 水题的相关文章

poj 1094 拓扑排序

Description An ascending sorted sequence of distinct values is one in which some form of a less-than operator is used to order the elements from smallest to largest. For example, the sorted sequence A, B, C, D implies that A < B, B < C and C < D.

HDU 1285 确定比赛名次 拓扑排序水题

Problem Description 有N个比赛队(1<=N<=500),编号依次为1,2,3,....,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排名,但现在裁判委员会不能直接获得每个队的比赛成绩,只知道每场比赛的结果,即P1赢P2,用P1,P2表示,排名时P1在P2之前.现在请你编程序确定排名. Input 输入有若干组,每组中的第一行为二个数N(1<=N<=500),M:其中N表示队伍的个数,M表示接着有M行的输入数据.接下来的M行数据中,每行也有两个整

HDU 3342 -- Legal or Not【裸拓扑排序 &amp;&amp;水题】

Legal or Not Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5906    Accepted Submission(s): 2734 Problem Description ACM-DIY is a large QQ group where many excellent acmers get together. It is

poj 1002:487-3279(水题,提高题 / hash)

487-3279 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 236746   Accepted: 41288 Description Businesses like to have memorable telephone numbers. One way to make a telephone number memorable is to have it spell a memorable word or phras

poj 4084:拓扑排序

poj 4084:拓扑排序 很好的题目,恶心的算法 描述 给出一个图的结构,输出其拓扑排序序列,要求在同等条件下,编号小的顶点在前. 输入 若干行整数,第一行有2个数,分别为顶点数v和弧数a,接下来有a行,每一行有2个数,分别是该条弧所关联的两个顶点编号. v<=100, a<=500 输出 若干个空格隔开的顶点构成的序列(用小写字母). 样例输入 6 8 1 2 1 3 1 4 3 2 3 5 4 5 6 4 6 5 样例输出 v1 v3 v2 v6 v4 v5 解题方案 显然这是有向图,然

HDU 1862 EXCEL排序 (排序水题)

Problem Description Excel可以对一组纪录按任意指定列排序.现请你编写程序实现类似功能. Input 测试输入包含若干测试用例.每个测试用例的第1行包含两个整数 N (<=100000) 和 C,其中 N 是纪录的条数,C 是指定排序的列号.以下有 N 行,每行包含一条学生纪录.每条学生纪录由学号(6位数字,同组测试中没有重复的学号).姓名(不超过8位且不包含空格的字符串).成绩(闭区间[0, 100]内的整数)组成,每个项目间用1个空格隔开.当读到 N=0 时,全部输入结

poj 2369 Permutations 置换水题

找循环节求lcm就够了,若答案是12345应该输出1,被坑了下. #include<iostream> #include<cstdio> #include<cstring> #include<string> #include<algorithm> #include<cmath> using namespace std; #define INF 0x3FFFFFF #define MAXN 2222 #define eps 1e-6 i

CodeForces 22D Segments 排序水题

题目链接:点击打开链接 右端点升序,取右端点 暴力删边 #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <iostream> #include <map> #include <set> #include <math.h> using namespace std; #define inf 10

拓扑排序 --- 模板题

确定比赛名次 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 10536    Accepted Submission(s): 4120 Problem Description 有 N个比赛队(1<=N<=500),编号依次为1,2,3,....,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排 名,但现在裁判委员会不能