## page was renamed from 程序设计练习29——zju2475——Benny's Compiler = 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. == 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. == Output == There is just one line of output for each test case. If file E can be compiled successfully output "Yes", else output "No". == Sample Input == {{{ 4 1 2 2 3 3 1 3 4 1 4 1 2 2 3 3 1 3 4 4 -1 }}} == Sample Output == {{{ No Yes }}} ------ {{{#!cplusplus /*Written by czk*/ #include #include #include using namespace std; int main() { while(1) { int n; cin >> n; if(n<0) break; vector< vector > g(n, vector(n, false) ); /*if replace int with bool, ZOJ will give runtime error! why?*/ vector in(n, 0); vector used(n, false); for(int i = 0; i < n; i++) { int A, B; cin >> A >> B; if(A == B) continue; if(!g[A-1][B-1]) { g[A-1][B-1] = true; in[B-1] ++; } } int E; cin >> E; queue 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"); } } }}} {{{#!cplusplus //2007-02-07 23:33:35 Accepted 2475 C++ 00:00.00 448K //writen by 曹高挺 //白色:T边 //灰色:B边 //从给定的点开始DFS检测到B边则编译失败(这里需要忽略环) #include #include #define WHITE -1 #define GRAY 2 #define BLACK 3 inline int mmax(int a, int b){ return a>b?a:b; } inline int mmin(int a, int b){ return a