posts - 2,  comments - 3,  trackbacks - 0

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 阅读(561) 评论(0)  编辑 收藏 引用 所属分类: 题解-timus

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2024年11月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

常用链接

留言簿

文章分类(46)

文章档案(33)

ACM

Algorithm Link

BLOG

Format analysis

Forum

Math

mirror

OpenGL

Protocol Analyzer

Recent Contests

Search

WIN32 Programming

最新随笔

搜索

  •  

最新评论

阅读排行榜

评论排行榜