http://acm.timus.ru/problem.aspx?space=1&num=1280
开始做这题,感觉巨简单,然后直接Topological Sorting,然后随便整,结果各种WR,后来细想一下,应该在建图后,每读入一个点,就判断当前点是否入度为零,不是的话,直接NO,否则删掉当前点的出边,继续读点处理,直到处理完N个点,就OK啦!
Code:
#include<cstdio>
struct Edg{
int n;
int next;
}EE[100001];
int head[1011];
int rd[1011];
int top;
int n;
int m;
int Topological_Sorting(int u){
int i = 0;
int reVal = 0;
if(!rd[u]){
int q = head[u];
while(q != -1){
rd[EE[q].n]--;
q = EE[q].next;
}
reVal = 1;
}
return reVal;
}
int main(int argc, char* argv[], char* env[])
{
int i = 0;
int j = 0;
int I = 0;
int J = 0;
int isCorrect = 0;
while(scanf("%d%d", &n, &m) != EOF){
for(i = 1; i <= n; i++){
head[i] = -1;
rd[i] = 0;
}
top = 0;
while(m-- > 0){
scanf("%d %d", &I, &J);
rd[J]++;
EE[top].n = J;
EE[top].next = head[I];
head[I] = top++;
}
isCorrect = 1;
for(i = 1; i <= n; i++){
scanf("%d", &I);
if(isCorrect){
isCorrect = Topological_Sorting(I);
}
}
printf("%s\n", isCorrect == 1 ? "YES" : "NO");
}
return 0;
}
posted on 2011-07-14 17:16
Lshain 阅读(562)
评论(0) 编辑 收藏 引用 所属分类:
题解-timus