QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#587603 | #9319. Bull Farm | yxy | WA | 204ms | 5672kb | C++17 | 3.9kb | 2024-09-24 20:47:30 | 2024-09-24 20:47:32 |
Judging History
answer
#include<bits/stdc++.h>
#define N 2097
#define M 31
#define LL long long
#define pii pair<int, int>
#define pll pair<LL, LL>
#define fi first
#define se second
#define pb push_back
#define lbd lower_bound
#define ubd upper_bound
#define IO ios :: sync_with_stdio(false), cin.tie(0)
using namespace std;
const LL INF = 10000000000000000LL;
int n, l, q, t[N][N], a[N], b[N], c[N], id[N], ans[N];
int cnt[N];
string str;
vector<int> g[N];
bool vis[N][N];
struct DSU {
int f[N], n;
void init(int ln) {
n = ln;
for (int i = 1; i <= n; i ++) f[i] = i;
return;
}
int find(int v) {
if (f[v] == v) return v;
return f[v] = find(f[v]);
}
void merge(int x, int y) {
x = find(x); y = find(y);
if (x == y) return;
if (g[x].size() < g[y].size()) swap(x, y);
for (auto v : g[y])
if(find(v) != x) g[x].pb(find(v)), vis[x][find(v)] = 1;
f[y] = x;
return;
}
bool check(int x, int y) {
return find(x) == find(y);
}
} dsu;
void dfs(int now, int u) {
if (vis[now][u]) return;
vis[now][u] = 1;
for (auto v : g[u]) {
v = dsu.find(v);
if (!vis[now][v])
dfs(now, v);
}
}
void update(int ti) {
for (int i = 1; i <= n; i ++) cnt[i] = 0;
for (int i = 1; i <= n; i ++) cnt[t[ti][i]] ++;
int cc = 0;
for (int i = 1; i <= n; i ++) if (cnt[i] > 0) cc ++;
if (cc < n - 1) return;
if (cc == n) {
for (int i = 1; i <= n; i ++)
dsu.merge(i, t[ti][i]);
for (int i = 1; i <= n; i ++) if (dsu.find(i) == i) {
for (auto &v : g[i]) v = dsu.find(v);
sort(g[i].begin(), g[i].end());
g[i].resize(unique(g[i].begin(), g[i].end()) - g[i].begin());
}
} else {
int v = 0, x = 0, y = 0;
for (int i = 1; i <= n; i ++) if (cnt[i] == 2) v = i;
for (int i = 1; i <= n; i ++) if (t[ti][i] == v) {
if (!x) x = i;
else y = i;
}
for (int i = 1; i <= n; i ++) if (cnt[i] == 0) v = i;
for (int i = 1; i <= n; i ++) if (i == dsu.find(i) && vis[i][x] && !vis[i][v])
dfs(i, v);
for (int i = 1; i <= n; i ++) if (i == dsu.find(i) && vis[i][y] && !vis[i][v])
dfs(i, v);
}
return;
}
void solve() {
cin >> n >> l >> q;
dsu.init(n);
for (int i = 1; i <= n; i ++) vis[i][i] = 1;
auto decode = [&] (string str) {
vector<int> st; st.clear();
for (int i = 0; i < (int) str.length(); i += 2) {
auto c = str[i];
auto h = str[i + 1];
st.pb((c - 48) * 50 + h - 48);
}
return st;
};
for (int i = 1; i <= l; i ++) {
cin >> str;
vector<int> tmp = decode(str);
for (int j = 1; j <= n; j ++) t[i][j] = tmp[j - 1];
}
for (int i = 1; i <= q; i ++) {
cin >> str;
vector<int> tmp = decode(str);
a[i] = tmp[0]; b[i] = tmp[1]; c[i] = tmp[2];
id[i] = i;
}
sort(id + 1, id + q + 1, [&] (int x, int y) -> bool {
return c[x] < c[y];
});
int ind = 0;
for (int i = 1; i <= q; i ++) {
int p = id[i];
while (ind < c[p] && ind + 1 <= l) {
ind ++;
update(ind);
}
a[p] = dsu.find(a[p]);
b[p] = dsu.find(b[p]);
ans[p] = (a[p] == b[p]) || (vis[a[p]][b[p]]);
}
string pans; pans.clear();
for (int i = 1; i <= q; i ++) {
if (ans[i]) pans.pb('1');
else pans.pb('0');
}
cout << pans << "\n";
for (int i = 1; i <= n; i ++)
for (int j = 1; j <= n; j ++) vis[i][j] = 0;
for (int i = 1; i <= n; i ++) g[i].clear();
}
int main() {
// IO;
int T;
cin >> T;
while (T --) solve();
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 5672kb
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: 0
Accepted
time: 1ms
memory: 5664kb
input:
1 3 3 6 020202 030301 030201 020102 030203 010201 010303 020303 010202
output:
010101
result:
ok single line: '010101'
Test #3:
score: -100
Wrong Answer
time: 204ms
memory: 5592kb
input:
200 10 10 5000 01060:04020305080709 0103070:060204050908 09070503080401060:02 050308010204090:0607 03010502040607080:09 03080109020504060:07 06050:09040302080107 07080305010409060:02 030809010:0204060507 0:060908070201050304 060700 090:03 09080: 070405 010703 0:0100 080601 030600 070206 0:0:09 08040...
output:
000000000001010000000000100010000000000001000000100000000000010000000000000110000000000001000000000000000100100000101000001000000000000001000000000000110000000000000000000000000000000010000000000000100000000000100000000101001000000000010100000000000000100000000000000000000010000000000000000001011000...
result:
wrong answer 1st lines differ - expected: '011110001101101111111111111111...1111111111111111101111111111111', found: '000000000001010000000000100010...1111111111111110100111111111011'