QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#569204 | #9319. Bull Farm | duoluoluo | WA | 1ms | 9960kb | C++20 | 4.3kb | 2024-09-16 21:16:52 | 2024-09-16 21:16:53 |
Judging History
answer
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <map>
#include <vector>
#include <queue>
#include <bitset>
using namespace std;
const int N = 2010;
int n, m, q, t[N][N], ans[1000010], num[N], fa[N];
int dfn[N], low[N], sta[N], vis[N], cnt, top;
bitset<N> b[N];
vector < vector < int > > colVec;
map<int, bool> e[N];
char s[N << 1];
struct Q {
int a, b, c, id;
} que[1000010];
bool cmp (Q a, Q b) {
return a.c < b.c;
}
int get (int x) {
return x == fa[x] ? x : fa[x] = get(fa[x]);
}
void dfs (int x) {
if (vis[x]) return;
vis[x] = 1;
for (auto y : e[x]) {
dfs(y.first);
b[x] |= b[y.first];
}
}
void tupo () {
for (int i = 1; i <= n; i ++) {
b[i].none();
b[i][i] = 1;
vis[i] = 0;
}
for (int i = 1; i <= n; i ++) {
if (get(i) == i && !vis[i])
dfs(i);
}
}
void tarjan (int x) {
dfn[x] = ++ cnt;
low[x] = cnt;
sta[++ top] = x;
vis[x] = 1;
for (auto [y, bl] : e[x]) {
if (!dfn[y]) {
tarjan(y);
low[x] = min(low[x], low[y]);
} else if (vis[y]) {
low[x] = min(low[x], low[y]);
}
}
if (dfn[x] == low[x]) {
vector < int > vec;
vis[x] = 0;
vec.emplace_back(x);
while (sta[top] != x) {
int y = sta[top--];
vis[y] = 0;
vec.emplace_back(y);
}
top--;
colVec.emplace_back(vec);
}
}
void addedge (int x, int y) {
if (x == y) return;
e[x][y] = 1;
}
void addbutton (int id) {
cnt = top = 0;
colVec.clear();
for (int i = 1; i <= n; i ++) num[i] = dfn[i] = low[i] = sta[i] = vis[i] = 0;
int flag = 0;
for (int i = 1; i <= n; i ++) {
num[t[id][i]] ++;
if (num[t[id][i]] > 2) return;
if (num[t[id][i]] == 2) {
if (flag) return;
flag = i;
}
}
if (flag) {
int d = 0;
for (int i = 1; i <= n; i ++)
if (!num[i])
d = i;
for (int i = 1; i <= n; i ++)
if (t[id][i] == flag) addedge(get(i), get(d));
} else {
for (int i = 1; i <= n; i ++)
addedge(get(i), get(t[id][i]));
}
for (int i = 1; i <= n; ++i) {
if (!dfn[i])
tarjan(i);
}
for (auto vec : colVec) {
int id = vec[0];
for (int x : vec) {
if (e[x].size() > e[id].size())
id = x;
}
for (int x : vec) {
if (x == id) continue;
for (auto [y, bl] : e[x]) {
e[id][y] = 1;
fa[x] = id;
}
}
}
for (int i = 1; i <= n; ++i) {
if (get(i) != i)
e[i].clear();
else {
vector < int > vec;
for (auto [y, bl] : e[i]) {
if (y == get(y))
vec.emplace_back(y);
}
e[i].clear();
for (int x : vec)
e[i][x] = 1;
}
}
}
void init () {
scanf("%d%d%d", &n, &m, &q);
for (int i = 1; i <= n; i ++) {
fa[i] = i;
e[i].clear();
}
for (int i = 1; i <= m; i ++) {
scanf("%s", s + 1);
for (int j = 1; j <= n; j ++)
t[i][j] = (s[(j * 2) - 1] - 48) * 50 + (s[(j * 2)] - 48);
}
for (int i = 1; i <= q; i ++) {
scanf("%s", s + 1);
que[i].a = (s[1] - 48) * 50 + (s[2] - 48);
que[i].b = (s[3] - 48) * 50 + (s[4] - 48);
que[i].c = (s[5] - 48) * 50 + (s[6] - 48);
que[i].id = i;
}
sort(que + 1, que + q + 1, cmp);
int qtop = 0;
for (int i = 1; i <= q; i ++) {
while (qtop < que[i].c) {
qtop ++;
addbutton(qtop);
}
tupo();
int j = i;
while (j <= q) {
if (b[get(que[j].a)][get(que[j].b)]) ans[que[j].id] = 1;
else ans[que[j].id] = 0;
if (que[j + 1].c != que[j].c) break;
j ++;
}
i = j;
}
for (int i = 1; i <= q; i ++) printf("%d", ans[i]);
printf("\n");
}
int main () {
int t = 1;
scanf("%d", &t);
while (t --) init();
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 9960kb
input:
2 5 2 4 0305040201 0404040404 030300 020500 050102 020501 6 2 4 030603010601 010203060504 030202 060402 050602 060401
output:
1011 0100
result:
ok 2 lines
Test #2:
score: -100
Wrong Answer
time: 1ms
memory: 7932kb
input:
1 3 3 6 020202 030301 030201 020102 030203 010201 010303 020303 010202
output:
000100
result:
wrong answer 1st lines differ - expected: '010101', found: '000100'