zoj 2475 Benny's Compiler
2009-05-08 11:28
211 查看
ZOJ Problem Set - 2475
Benny's Compiler
Time Limit: 1 Second Memory Limit: 32768 KB
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
Author: ZHENG, Lu
Source: Zhejiang Provincial Programming Contest 2005
Submit Status
//1859524 2009-05-08 09:52:20 Time Limit Exceeded 2475 C++ 1001 0 Wpl
//1859528 2009-05-08 09:56:35 Memory Limit Exceeded 2475 C++ 0 32769 Wpl
//1859534 2009-05-08 10:00:46 Wrong Answer 2475 C++ 0 224 Wpl
//1859641 2009-05-08 11:10:30 Accepted 2475 C++ 0 224 Wpl
#include <iostream>
#include <queue>
#define MAX 102
using namespace std;
int edges[MAX][MAX];
int n,start;
bool mark,used[MAX];
void Init()
{
int i,a,b,j;
for(i=1;i<MAX;i++)
for(j=1;j<MAX;j++)
edges[i][j]=-1;
for(i=0;i<n;i++)
{
scanf("%d%d",&a,&b);
if(a!=b)
edges[a][b]=1;
}
}
void DFS(int start)
{
int i;
if(used[start])
{
mark=true;
return;
}
used[start]=true;
for(i=1;i<MAX;i++)
{
if(edges[start][i]==1)
{
DFS(i);
}
}
used[start]=false;
}
int main()
{
int i;
while(scanf("%d",&n)!=EOF&&n>=0)
{
Init();
scanf("%d",&start);
mark=false;
for(i=1;i<MAX;i++)
used[i]=false;
DFS(start);
if(!mark)
printf("Yes\n");
else
printf("No\n");
}
return 0;
}
//1859782 2009-05-08 13:05:57 Accepted 2475 C++ 0 184 Wpl
//邻接表
#include <iostream>
#include <vector>
#define MAX 102
using namespace std;
vector<int>G[MAX];
vector<int>::iterator p;
int n,start;
bool mark,used[MAX];
void Init()
{
int i;
int a,b;
for(i=1;i<MAX;i++)
{
G[i].clear();
used[i]=false;
}
for(i=1;i<=n;i++)
{
scanf("%d%d",&a,&b);
if(a!=b) //a a 或者b b这种不算是环的
G[a].push_back(b);
}
}
void DFS(int start)
{
if(used[start])
{
mark=true;
return;
}
used[start]=true;
/*if(G[start].empty())
return ;
for(p=G[start].begin();p!=G[start].end();p++)
DFS(*p);*/
int i;
for(i=0;i<G[start].size();i++)
DFS(G[start][i]);
used[start]=false;
}
int main()
{
while(scanf("%d",&n)!=EOF&&n>=0)
{
Init();
scanf("%d",&start);
mark=false;
DFS(start);
if(!mark)
printf("Yes\n");
else
printf("No\n");
}
return 0;
}
Benny's Compiler
Time Limit: 1 Second Memory Limit: 32768 KB
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
Author: ZHENG, Lu
Source: Zhejiang Provincial Programming Contest 2005
Submit Status
//1859524 2009-05-08 09:52:20 Time Limit Exceeded 2475 C++ 1001 0 Wpl
//1859528 2009-05-08 09:56:35 Memory Limit Exceeded 2475 C++ 0 32769 Wpl
//1859534 2009-05-08 10:00:46 Wrong Answer 2475 C++ 0 224 Wpl
//1859641 2009-05-08 11:10:30 Accepted 2475 C++ 0 224 Wpl
#include <iostream>
#include <queue>
#define MAX 102
using namespace std;
int edges[MAX][MAX];
int n,start;
bool mark,used[MAX];
void Init()
{
int i,a,b,j;
for(i=1;i<MAX;i++)
for(j=1;j<MAX;j++)
edges[i][j]=-1;
for(i=0;i<n;i++)
{
scanf("%d%d",&a,&b);
if(a!=b)
edges[a][b]=1;
}
}
void DFS(int start)
{
int i;
if(used[start])
{
mark=true;
return;
}
used[start]=true;
for(i=1;i<MAX;i++)
{
if(edges[start][i]==1)
{
DFS(i);
}
}
used[start]=false;
}
int main()
{
int i;
while(scanf("%d",&n)!=EOF&&n>=0)
{
Init();
scanf("%d",&start);
mark=false;
for(i=1;i<MAX;i++)
used[i]=false;
DFS(start);
if(!mark)
printf("Yes\n");
else
printf("No\n");
}
return 0;
}
//1859782 2009-05-08 13:05:57 Accepted 2475 C++ 0 184 Wpl
//邻接表
#include <iostream>
#include <vector>
#define MAX 102
using namespace std;
vector<int>G[MAX];
vector<int>::iterator p;
int n,start;
bool mark,used[MAX];
void Init()
{
int i;
int a,b;
for(i=1;i<MAX;i++)
{
G[i].clear();
used[i]=false;
}
for(i=1;i<=n;i++)
{
scanf("%d%d",&a,&b);
if(a!=b) //a a 或者b b这种不算是环的
G[a].push_back(b);
}
}
void DFS(int start)
{
if(used[start])
{
mark=true;
return;
}
used[start]=true;
/*if(G[start].empty())
return ;
for(p=G[start].begin();p!=G[start].end();p++)
DFS(*p);*/
int i;
for(i=0;i<G[start].size();i++)
DFS(G[start][i]);
used[start]=false;
}
int main()
{
while(scanf("%d",&n)!=EOF&&n>=0)
{
Init();
scanf("%d",&start);
mark=false;
DFS(start);
if(!mark)
printf("Yes\n");
else
printf("No\n");
}
return 0;
}
相关文章推荐
- zoj 2475 Benny's Compiler
- zoj 2475 Benny's Compiler
- ZOJ 2475 Benny's Compiler(DFS)
- ZOJ 2475 Benny's Compiler 有向图判读点是否存在环内
- zoj 2475 Benny's Compiler
- zoj 2475 Catenyms
- ZOJ 2475 dfs 少了一个条件WA了半天
- ZOJ-2475
- ZOJ_Crossed Matchings DP
- Compiler_词法分析_直接转向法
- No CMAKE_C_COMPILER could be found
- poj 1384 & zoj 2014 Piggy-Bank(完全背包)
- ZOJ 3826 Hierarchical Notation Hash+模拟
- How Many Nines(ZOJ - 3950)
- Cong! ZOJ 100AC!
- zoj3201Tree of Tree
- zoj 3299(区间改动+离散化)
- zoj_1184
- SGU 197 Nice Patterns Strike Back || ZOJ 2317 Nice Patterns Strike Back
- ZOJ 3929 Deque and Balls