记录编号 |
160166 |
评测结果 |
AAAAAAAAAA |
题目名称 |
物质起源 |
最终得分 |
100 |
用户昵称 |
Asm.Def |
是否通过 |
通过 |
代码语言 |
C++ |
运行时间 |
0.009 s |
提交时间 |
2015-04-24 12:35:58 |
内存使用 |
0.64 MiB |
显示代码纯文本
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <cctype>
#include <algorithm>
#include <cmath>
using namespace std;
//#define USEFREAD
#ifdef USEFREAD
#define InputLen 5000000
char *ptr=(char *)malloc(InputLen);
#define getc() (*(ptr++))
#else
#define getc() (getchar())
#endif
#define SetFile(x) (freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout))
template<class T>inline void getd(T &x){
int ch = getc();bool neg = false;
while(!isdigit(ch) && ch != '-')ch = getc();
if(ch == '-')ch = getc(), neg = true;
x = ch - '0';
while(isdigit(ch = getc()))
x = x * 10 - '0' + ch;
if(neg)x = -x;
}
/***********************************************************************/
const int maxn = 402, maxs = 17000;
#include <map>
#include <string>
#include <vector>
#include <queue>
map<string, int> S;
string NAME[maxs];
int Out[maxn][22], p[maxn], q[maxn], N, M, Iter;
vector<int> adj[maxs];
bool old[maxs], tag[maxs], vis[maxn];
inline bool cmp(int a, int b){return NAME[a] < NAME[b];}
#include <iostream>
inline void work(){
int i, t, ch, id, Acnt = 0;
char str[15], *it;
string name;
getd(N);
for(i = 0;i < N;++i){
for(p[i] = 0;;++p[i]){
it = str;
while(!isalpha(ch = getc()));
while(isalpha(ch) || isdigit(ch)){
*(it++) = ch;
ch = getchar();
}
*it = 0;
name = string(str);
if(!S.count(name))S[name] = id = Iter++;
else id = S[name];
NAME[id] = name;
adj[id].push_back(i);
if(ch == '=')break;
}
++p[i];
for(q[i] = 0;;++q[i]){
it = str;
while(!isalpha(ch = getc()));
while(isalpha(ch) || isdigit(ch)){
*(it++) = ch;
ch = getchar();
}
*it = 0;
name = string(str);
if(!S.count(name))S[name] = id = Iter++;
else id = S[name];
NAME[id] = name;
Out[i][q[i]] = id;
if(ch != '+')break;
}
++q[i];
}
queue<int> Q;
getd(M);
while(M--){
cin >> name;
if(!S.count(name))continue;
id = S[name];
old[id] = tag[id] = true;
Q.push(id);
}
vector<int>::iterator v_it;
while(!Q.empty()){
id = Q.front();Q.pop();
for(v_it = adj[id].begin();v_it != adj[id].end();++v_it){
if(!(--p[*v_it])){//可以反应
for(i = 0;i < q[*v_it];++i)if(!tag[t = Out[*v_it][i]]){
tag[t] = true;
++Acnt;
Q.push(t);
}
}
}
}
map<string, int>::iterator M_it;
printf("%d\n", Acnt);
for(M_it = S.begin();M_it != S.end();++M_it)
if(tag[(*M_it).second] && !old[(*M_it).second])puts((*M_it).first.c_str());
}
int main(){
#ifdef DEBUG
freopen("test.txt", "r", stdin);
#else
SetFile(origin);
#endif
#ifdef USEFREAD
fread(ptr,1,InputLen,stdin);
#endif
work();
#ifdef DEBUG
printf("\n%.3lf sec \n", (double)clock() / CLOCKS_PER_SEC);
#endif
return 0;
}