版本1和2间的区别
于2006-03-11 16:43:39修订的的版本1
大小: 1885
编辑: czk
备注:
于2006-04-21 19:10:57修订的的版本2
大小: 3081
编辑: czk
备注:
删除的内容标记成这样。 加入的内容标记成这样。
行号 2: 行号 2:
http://acm.zju.edu.cn/show_problem.php?pid=2475
行号 50: 行号 52:
http://acm.zju.edu.cn/show_problem.php?pid=2475
行号 52: 行号 53:
{{{#!cplusplus
#include <iostream>
#include <vector>
#include <queue>
using namespace std;

int main()
{
  while(1) {
    int n;
    cin >> n;
    if(n<0) break;

    vector< vector<int> > g(n, vector<int>(n, false) );
    vector<int> in(n, 0);
    vector<int> used(n, false);

    for(int i = 0; i < n; i++) {
      int A, B;
      cin >> A >> B;
      if(!g[A-1][B-1]) {
 g[A-1][B-1] = true;
 in[B-1] ++;
      }
    }
    int E;
    cin >> E;

    queue<int> v;
    v.push(E-1);
    while(!v.empty()) {
      int next = v.front();
      v.pop();
      for(int i = 0; i < n; i++) {
 if(g[next][i] && !used[i]) {
   used[i] = true;
   v.push(i);
 }
      }
    }

    for(int i = 0; i < n; i++)
      if(in[i] == 0 || !used[i])
 v.push(i);
    while(!v.empty()) {
      int next = v.front();
      v.pop();
      for(int i = 0; i < n; i++) {
 if(g[next][i]) {
   g[next][i] = false;
   in[i]--;
   if(in[i] == 0)
     v.push(i);
 }
      }
    }
    bool result = true;
    for(int i = 0; i < n; i++) {
      if(used[i] && in[i] != 0) {
 result = false;
 break;
      }
    }
    cout << (result?"Yes\n":"No\n");
  }
}

}}}

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

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

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