版本6和7间的区别
于2007-02-07 23:41:07修订的的版本6
大小: 4694
编辑: 89
备注:
于2008-02-23 15:34:13修订的的版本7
大小: 4694
编辑: localhost
备注: converted to 1.6 markup
未发现区别!

Benny's Compiler

http://acm.zju.edu.cn/show_problem.php?pid=2475

Time limit: 1 Seconds

Memory limit: 32768K

These days Benny has designed a new compiler for C programming language. His compilation system provides a compiler driver that invokes the language preprocessor, compiler, assembler and linker. C source file (with .C suffix) is translated to relocatable object module first, and then all modules are linked together to generate an executable object file.

The translator (preprocessor, compiler and assembler) works perfectly and can generate well optimized assembler code from C source file. But the linker has a serious bug -- it cannot resolve global symbols when there are circular references. To be more specific, if file 1 references variables defined in file 2, file 2 references variables defined in file 3, ... file n-1 references variables defined in file n and file n references variables defined in file 1, then Benny's linker walks out because it doesn't know which file should be processed first.

Your job is to determine whether a source file can be compiled successfully by Benny's compiler.

1. Input

There are multiple test cases! In each test case, the first line contains one integer N, and then N lines follow. In each of these lines there are two integers Ai and Bi, meaning that file Ai references variables defined in file Bi (1 <= i <= N). The last line of the case contains one integer E, which is the file we want to compile.

A negative N denotes the end of input. Else you can assume 0 < N, Ai, Bi, E <= 100.

2. Output

There is just one line of output for each test case. If file E can be compiled successfully output "Yes", else output "No".

3. Sample Input

4
1 2
2 3
3 1
3 4
1

4
1 2
2 3
3 1
3 4
4

-1

4. Sample Output

No
Yes


   1 /*Written by czk*/
   2 #include <iostream>
   3 #include <vector>
   4 #include <queue>
   5 using namespace std;
   6 
   7 int main() 
   8 {
   9   while(1) {
  10     int n;
  11     cin >> n;
  12     if(n<0) break;
  13 
  14     vector< vector<int> > g(n, vector<int>(n, false) ); /*if replace int with bool, ZOJ will give runtime error! why?*/
  15     vector<int> in(n, 0);
  16     vector<int> used(n, false);
  17 
  18     for(int i = 0; i < n; i++) {
  19       int A, B;
  20       cin >> A >> B;
  21       if(A == B)
  22         continue;
  23       if(!g[A-1][B-1]) {
  24         g[A-1][B-1] = true;
  25         in[B-1] ++;
  26       }
  27     }
  28     int E;
  29     cin >> E;
  30 
  31     queue<int> v;
  32     v.push(E-1);
  33     while(!v.empty()) {
  34       int next = v.front();
  35       v.pop();
  36       for(int i = 0; i < n; i++) {
  37         if(g[next][i] && !used[i]) {
  38           used[i] = true;
  39           v.push(i);
  40         }
  41       }
  42     }
  43 
  44     for(int i = 0; i < n; i++)
  45       if(in[i] == 0||!used[i])
  46         v.push(i);
  47     while(!v.empty()) {
  48       int next = v.front();
  49       v.pop();
  50       for(int i = 0; i < n; i++) {
  51         if(g[next][i]) {
  52           g[next][i] = false;
  53           in[i]--;
  54           if(in[i] == 0)
  55             v.push(i);
  56         }
  57       }
  58     }
  59     bool result = true;
  60     for(int i = 0; i < n; i++) {
  61       if(used[i] && in[i] != 0) {
  62         result = false;
  63         break;
  64       }
  65     }
  66     cout << (result?"Yes\n":"No\n");
  67   }
  68 }

   1 //2007-02-07 23:33:35 Accepted 2475 C++ 00:00.00 448K 
   2 //writen by 曹高挺
   3 //白色:T边
   4 //灰色:B边
   5 //从给定的点开始DFS检测到B边则编译失败(这里需要忽略环)
   6 #include <stdio.h>
   7 #include <string.h>
   8 
   9 #define WHITE   -1
  10 #define GRAY    2
  11 #define BLACK   3
  12 
  13 inline int mmax(int a, int b){
  14         return a>b?a:b;
  15 }
  16 
  17 inline int mmin(int a, int b){
  18         return a<b?a:b;
  19 }
  20 
  21 int n;
  22 int color[101];
  23 bool map[101][101];
  24 int start;
  25 int low, up;
  26 
  27 bool GetInfo(){
  28         scanf("%d", &n);
  29         if( n<0 )       return false;
  30         int i;
  31         int from, to;
  32         memset(map, false, sizeof(map));
  33 
  34         low = 200;
  35         up = -1;
  36         for(i=0; i<n; i++){
  37                 scanf("%d %d", &from, &to);
  38                 low = mmin(low, from);
  39                 low = mmin(low, to);
  40                 up = mmax(up, from);
  41                 up = mmax(up, to);
  42                 map[from][to] = true;
  43         }
  44         scanf("%d", &start);
  45         return true;
  46 }
  47 
  48 bool F(int node){
  49         color[node] = GRAY;
  50         int i;
  51         for(i=low; i<=up; i++){
  52                 if(map[node][i] && i!=node){
  53                         //注意文件I引用文件I的变量可以通过编译
  54                         if(color[i] == GRAY){
  55                                 return false;
  56                         }
  57                         if(color[i]==WHITE && !F(i)){
  58                                 return false;
  59                         }
  60                 }
  61         }
  62     color[node] = BLACK;
  63         return true;
  64 }
  65 
  66 bool Solve(){
  67         memset(color, WHITE, sizeof(color));
  68         return F(start);
  69 }
  70 
  71 int main(){
  72         while( GetInfo() ){
  73                 if( Solve() ){
  74                         printf("Yes\n");
  75                 }
  76                 else{
  77                         printf("No\n");
  78                 }
  79         }
  80 
  81         return 1;
  82 }

zju2475 (2008-02-23 15:34:13由localhost编辑)

ch3n2k.com | Copyright (c) 2004-2020 czk.