URAL 1280. Topological Sorting

1. 题目

http://acm.timus.ru/problem.aspx?space=1&num=1280

1280. Topological Sorting

Time limit: 1.0 second
Memory limit: 64 MB
Michael wants to win the world championship in programming and decided to study N subjects (for convenience we will number these subjects from 1 to N). Michael has worked out a study plan for this purpose. But it turned out that certain subjects may be studied only after others. So, Michael’s coach analyzed all subjects and prepared a list of M limitations in the form “si ui” (1 ≤ si, uiN; i = 1, 2, …, M), which means that subject si must be studied before subject ui.
Your task is to verify if the order of subjects being studied is correct.
Remark. It may appear that it’s impossible to find the correct order of subjects within the given limitations. In this case any subject order worked out by Michael is incorrect.
Limitations
1 ≤ N ≤ 1000; 0 ≤ M ≤ 100000.

Input

The first line contains two integers N and M (N is the number of the subjects, M is the number of the limitations). The nextM lines contain pairs si, ui, which describe the order of subjects: subject si must be studied before ui. Further there is a sequence of N unique numbers ranging from 1 to N — the proposed study plan.

Output

Output a single word “YES” or “NO”. “YES” means that the proposed order is correct and has no contradictions with the given limitations. “NO” means that the order is incorrect.

Samples

input output
5 6
1 3
1 4
3 5
5 2
4 2
1 2
1 3 4 5 2
YES
5 6
1 3
1 4
3 5
5 2
4 2
1 2
1 2 4 5 3
NO
Problem Author: © Sergey G. Volchenkov, 2003(volchenkov@yandex.ru); Vladimir N. Pinaev, 2003(vpinaev@mail.ru; http://www.pic200x.chat.ru); Michael Y. Kopachev, 2003 (mkopachev@krista.ru).
Problem Source: 2003-2004 ACM Central Region of Russia Quarterfinal Programming Contest, Rybinsk, October 15-16, 2003

2. 思路

给出一组课程,一些课程有先修的要求,即必须在学习了特定课程之后才能学习,给出一个学习计划,判断学习计划是否满足各课程的先修要求。

标题剧透拓扑排序,由于只需要判断而不需要求得,也可以不用拓扑排序,直接根据各课程的要求判断即可。

下面的代码中,需要判断的学习计划使用数组plan[] 存储,并为plan[] 建立了一个由数据到索引的查找表lut[] ,使得lut[plan[i]] = i 。对于课程s[i] 和u[i] ,要求s[i] 在u[i] 前修完,则需要满足lut[s[i]] < lut[u[i]] ,否则所给学习计划就不满足要求。

3. 代码

#include <stdio.h>

#define MAX_N 1000
#define MAX_M 100000

void solveE2a();
void inputOrder(int m, int s[], int u[]);
void inputPlan(int n, int plan[]);
int verifyOrder(int m, int n, int s[], int u[], int plan[]);
void generateIndexLut(int n, int array[], int lut[]);
void printArray(int array[], int len);

int main(void) {
    solveE2a();
    return 0;
}


int s[MAX_M], u[MAX_M], plan[MAX_N];
void solveE2a() {
    int m, n, result;

    scanf("%d %d", &n, &m);
    inputOrder(m, s, u);
    inputPlan(n, plan);

    // printArray(s, m);
    // printArray(u, m);
    // printArray(plan, n);

    result = verifyOrder(m, n, s, u, plan);

    if (result) {
        printf("YES\n");
    } else {
        printf("NO\n");
    }
}

void inputOrder(int m, int s[], int u[]) {
    int i;

    for (i = 0; i < m; ++i) {
        scanf("%d %d", &s[i], &u[i]);
    }
}

void inputPlan(int n, int plan[]) {
    int i;

    for (i = 0; i < n; ++i) {
        scanf("%d", &plan[i]);
    }
}

int lut[MAX_N + 1];
int verifyOrder(int m, int n, int s[], int u[], int plan[]) {
    int i;

    generateIndexLut(n, plan, lut);
    // printArray(lut, n + 1);
    for (i = 0; i < m; ++i) {
        if (lut[s[i]] > lut[u[i]]) {
            return 0;
        }
    }
    return 1;
}

void generateIndexLut(int n, int array[], int lut[]) {
    int i;

    for (i = 0; i < n; ++i) {
        lut[array[i]] = i;
    }
}

void printArray(int array[], int len) {
    int i;

    for (i = 0; i < len; ++i) {
        printf("%d ", array[i]);
    }
    printf("\n");
}