1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
| #include <cstdio> #include <iostream> #include <stack> #include <map>
const int MAXN = 4000;
struct Node { struct Edge *edges; int dfn, low; bool visited; struct Connected *connected; } N[2 * MAXN];
struct Edge { Node *from, *to; Edge *next;
Edge(Node *from, Node *to) : from(from), to(to), next(from->edges) {} };
struct Connected { int size; } connecteds[2 * MAXN];
int n, m; std::string girl[MAXN], boy[MAXN]; std::map<std::string, int> mp;
inline void addEdge(int from, int to) { N[from].edges = new Edge(&N[from], &N[to]); }
void tarjan(Node *x) { static int num = 0, counts = 0; static std::stack<Node *> stk; x->dfn = x->low = ++num; stk.push(x); x->visited = true;
for (Edge *edges = x->edges; edges; edges = edges->next) { if (!edges->to->dfn) { tarjan(edges->to); x->low = std::min(x->low, edges->to->low); } else if (edges->to->visited) { x->low = std::min(x->low, edges->to->dfn); } }
if (x->dfn == x->low) { counts++; Node *y; do { y = stk.top(); stk.pop(); y->visited = false; y->connected = &connecteds[counts]; connecteds[counts].size++; } while (x != y); } }
int main() { scanf("%d", &n);
for (int i = 0; i < n; i++) { std::cin >> girl[i] >> boy[i]; mp[girl[i]] = 2 * i; mp[boy[i]] = 2 * i + 1; addEdge(mp[girl[i]], mp[boy[i]]); }
scanf("%d", &m);
for (int i = 0; i < m; i++) { std::string g, b; std::cin >> g >> b; addEdge(mp[b], mp[g]); }
for (int i = 0; i < 2 * n; i++) { if (!N[i].dfn) { tarjan(&N[i]); } }
for (int i = 0; i < n; i++) { if (N[mp[girl[i]]].connected == N[mp[boy[i]]].connected) puts("Unsafe"); else puts("Safe"); }
return 0; }
|