|
| 1 | +/** |
| 2 | + * @file 11223.cpp |
| 3 | + * @author Macesuted (i@macesuted.moe) |
| 4 | + * @date 2025-10-19 |
| 5 | + * |
| 6 | + * @copyright Copyright (c) 2025 |
| 7 | + * |
| 8 | + */ |
| 9 | + |
| 10 | +#include <bits/stdc++.h> |
| 11 | +using namespace std; |
| 12 | + |
| 13 | +#define endl '\n' |
| 14 | + |
| 15 | +using tiii = tuple<int, int, int>; |
| 16 | + |
| 17 | +void solve(void) { |
| 18 | + int n; |
| 19 | + cin >> n; |
| 20 | + |
| 21 | + string code; |
| 22 | + for (int i = 1; i <= n; i++) { |
| 23 | + string line; |
| 24 | + if (i == 1) getline(cin, line); |
| 25 | + getline(cin, line); |
| 26 | + code.append(line), code.push_back(' '); |
| 27 | + } |
| 28 | + |
| 29 | + unordered_map<string, int> dict; |
| 30 | + dict["double"] = 1; |
| 31 | + dict["string"] = 2; |
| 32 | + |
| 33 | + stringstream ssin(code); |
| 34 | + string s; |
| 35 | + while (ssin >> s) |
| 36 | + if (s == "message") ssin >> s, dict[s] = dict.size() + 1; |
| 37 | + |
| 38 | + int m = dict.size(); |
| 39 | + |
| 40 | + vector<vector<tiii>> message(m + 1); |
| 41 | + |
| 42 | + ssin = stringstream(code); |
| 43 | + while (ssin >> s) { |
| 44 | + assert(s == "message"); |
| 45 | + ssin >> s; |
| 46 | + int mid = dict[s]; |
| 47 | + assert(mid); |
| 48 | + |
| 49 | + ssin >> s; |
| 50 | + assert(s == "{"); |
| 51 | + |
| 52 | + while (ssin >> s && s != "}") { |
| 53 | + int label = (s == "required" ? 1 : s == "optional" ? 2 : 3); |
| 54 | + |
| 55 | + ssin >> s; |
| 56 | + int type = dict[s]; |
| 57 | + assert(type); |
| 58 | + |
| 59 | + ssin >> s; |
| 60 | + ssin >> s; |
| 61 | + assert(s == "="); |
| 62 | + |
| 63 | + int fieldNumber; |
| 64 | + ssin >> fieldNumber; |
| 65 | + |
| 66 | + ssin >> s; |
| 67 | + assert(s == ";"); |
| 68 | + |
| 69 | + message[mid].emplace_back(label, type, fieldNumber); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + for (int i = 1; i <= m; i++) |
| 74 | + sort(message[i].begin(), message[i].end(), [](const tiii& a, const tiii& b) -> bool { return get<2>(a) < get<2>(b); }); |
| 75 | + |
| 76 | + auto _ = [&](int x, int y) -> int { return (x - 1) * m + y; }; |
| 77 | + |
| 78 | + vector<bool> invalid(m * m + 1); |
| 79 | + vector<vector<int>> graph(m * m + 1); |
| 80 | + |
| 81 | + for (int i = 2; i <= m; i++) invalid[_(1, i)] = invalid[_(i, 1)] = true; |
| 82 | + for (int i = 3; i <= m; i++) invalid[_(2, i)] = true; |
| 83 | + |
| 84 | + for (int i = 3; i <= m; i++) |
| 85 | + for (int j = 3; j <= m; j++) { |
| 86 | + if (i == j) continue; |
| 87 | + |
| 88 | + for (size_t ki = 0, kj = 0; ki < message[i].size(); ki++) { |
| 89 | + while (kj < message[j].size() && get<2>(message[i][ki]) != get<2>(message[j][kj])) { |
| 90 | + if (get<0>(message[j][kj]) == 1) invalid[_(i, j)] = true; |
| 91 | + kj++; |
| 92 | + } |
| 93 | + if (kj == message[j].size()) { |
| 94 | + invalid[_(i, j)] = true; |
| 95 | + break; |
| 96 | + } |
| 97 | + |
| 98 | + graph[_(get<1>(message[i][ki]), get<1>(message[j][kj]))].push_back(_(i, j)); |
| 99 | + |
| 100 | + int mi = get<0>(message[i][ki]), mj = get<0>(message[j][kj]); |
| 101 | + if (mi > mj) invalid[_(i, j)] = true; |
| 102 | + |
| 103 | + kj++; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + queue<int> que; |
| 108 | + for (int i = 1; i <= m * m; i++) |
| 109 | + if (invalid[i]) que.push(i); |
| 110 | + while (!que.empty()) { |
| 111 | + int p = que.front(); |
| 112 | + que.pop(); |
| 113 | + for (auto q : graph[p]) |
| 114 | + if (!invalid[q]) invalid[q] = true, que.push(q); |
| 115 | + } |
| 116 | + |
| 117 | + int q; |
| 118 | + cin >> q; |
| 119 | + while (q--) { |
| 120 | + string A, B; |
| 121 | + cin >> A >> B; |
| 122 | + cout << "Wire-format "; |
| 123 | + if (invalid[_(dict[A], dict[B])] || invalid[_(dict[B], dict[A])]) cout << "in"; |
| 124 | + cout << "compatible." << endl; |
| 125 | + } |
| 126 | + |
| 127 | + return; |
| 128 | +} |
| 129 | + |
| 130 | +int main() { |
| 131 | + ios::sync_with_stdio(false), cin.tie(nullptr); |
| 132 | + |
| 133 | + int _ = 1; |
| 134 | + solve(); |
| 135 | + |
| 136 | + return 0; |
| 137 | +} |
0 commit comments