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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
| #include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10; const int inf = 1 << 30; const long long llinf = 1ll << 60; const double PI = acos(-1);
#define lowbit(x) (x & -x) typedef long long ll; typedef double db; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<db, db> pdd; typedef pair<ll, int> pli;
int n, m, k, q; struct Graph { vector<vector<int>> to; Graph() {} vector<int> vis; vector<int> col; Graph(int n) { to = vector<vector<int>>(n); vis = vector<int>(n); col = vector<int>(n); } bool checkBipartiteGraph(int x, int blocktype = 1, int fa = 0, int nowcol = 1) { vis[x] = nowcol; col[x] = blocktype; bool isBipart = true; for (auto y : to[x]) { if (y == fa) continue; if (!vis[y]) isBipart &= checkBipartiteGraph(y, blocktype, y, 3 - nowcol); else if (vis[y] == nowcol) isBipart = false; } return isBipart; } void fillCol(int x, int col = 4) { vis[x] = col; for (auto y : to[x]) { if (vis[y] == col) continue; fillCol(y, col); } } void add(int x, int y) { to[x].push_back(y); } }; string a; void work() { cin >> n >> m >> q; cin >> a; Graph fullGraph(n), withoutCommom_Graph(n); for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; x--; y--; if (a[x] != a[y]) { withoutCommom_Graph.add(x, y); withoutCommom_Graph.add(y, x); } fullGraph.add(x, y); fullGraph.add(y, x); } int cnt1 = 0; for (int x = 0; x < n; x++) { if (fullGraph.vis[x] == 0) { cnt1++; if (fullGraph.checkBipartiteGraph(x, cnt1) == false) fullGraph.fillCol(x); } } int cnt2 = 0; for (int x = 0; x < n; x++) if (withoutCommom_Graph.vis[x] == 0) withoutCommom_Graph.checkBipartiteGraph(x, ++cnt2); vector<int> connectWithLL(n), connectWithRR(n); for (int x = 0; x < n; x++) for (auto y : fullGraph.to[x]) { int blocktype = withoutCommom_Graph.col[x]; if (a[x] == '(' && a[y] == '(') connectWithLL[blocktype] = 1; if (a[x] == ')' && a[y] == ')') connectWithRR[blocktype] = 1; } for (int t = 0; t < q; t++) { int x, y; cin >> x >> y; x--; y--; if (a[x] == a[y] || a[x] == ')') { cout << 0; continue; } int fullGraphCol_x = fullGraph.col[x], fullGraphCol_y = fullGraph.col[y]; if (fullGraphCol_x != fullGraphCol_y) { cout << 0; continue; } int partGraphCol_x = withoutCommom_Graph.col[x], partGraphCol_y = withoutCommom_Graph.col[y]; if (partGraphCol_x == partGraphCol_y) { cout << 1; continue; } else if (connectWithLL[partGraphCol_x] && connectWithRR[partGraphCol_y]) { if (fullGraph.vis[x] != fullGraph.vis[y] || fullGraph.vis[x] == 4) { cout << 1; continue; } else { cout << 0; continue; } } else { cout << 0; continue; } } } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t = 1; while (t-- > 0) { work(); } }
|