QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#565045 | #9319. Bull Farm | limpid | WA | 1ms | 5900kb | C++14 | 4.1kb | 2024-09-15 20:01:36 | 2024-09-15 20:01:36 |
Judging History
answer
#include<bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pii pair<int, int>
using namespace std;
inline int read(){
int f = 1, ans = 0;
char c = getchar();
while(c < '0' || c > '9'){if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9'){ans = ans * 10 + c - '0'; c = getchar();}
return f * ans;
}
const int MAXN = 2e3 + 11;
int cas, N, L, Q;
struct Union{
int f[MAXN];
void Init(){
for(int i = 1; i <= N; i++) f[i] = i;
return;
}
int find(int x){return f[x] == x ? x : f[x] = find(f[x]);}
void merge(int x, int y){
int t1 = find(x), t2 = find(y);
f[t2] = t1;
return;
}
}U;
char str[MAXN * 2];
int A[MAXN][MAXN];
struct Que{
int A, B, C, id;
}Query[MAXN];
bool cmp(Que w1, Que w2){
return w1.C < w2.C;
}
int buc[MAXN], vis[MAXN];
vector<pii> vec;
void Press(int tim){
for(int i = 1; i <= N; i++) buc[i] = 0;
for(int i = 1; i <= N; i++) buc[A[tim][i]]++;
bool flag = 1;
for(int i = 1; i <= N; i++) flag &= (buc[i] == 1);
if(flag){
for(int i = 1; i <= N; i++) U.merge(A[tim][i], i);
return;
}
int cnt = 0;
for(int i = 1; i <= N; i++) cnt += (buc[i] >= 2);
if(cnt > 1) return;
for(int i = 1; i <= N; i++) if(buc[i] > 2) return;
int pos = -1;
for(int i = 1; i <= N; i++) if(buc[i] == 2) pos = i;
assert(cnt == 1);
for(int i = 1; i <= N; i++) if(A[tim][i] == pos) vec.pb(mp(pos, i));
return;
}
int dfn[MAXN], low[MAXN], num, tot1, col[MAXN], sta[MAXN];
vector<int> vec1[MAXN];
void tarjan(int u){
dfn[u] = low[u] = ++num;
sta[++tot1] = u;
for(auto v:vec1[u]){
if(!dfn[v]){
tarjan(v);
low[u] = min(low[u], low[v]);
continue;
}else if(!col[v]) low[u] = min(low[u], dfn[v]);
}
if(low[u] == dfn[u]){
col[u] = ++col[0];
while(sta[tot1] != u){
col[sta[tot1]] = col[0];
tot1--;
}tot1--;
}return;
}
void Clear1(){
col[0] = 0;
for(int i = 1; i <= N; i++) dfn[i] = low[i] = num = tot1 = col[i] = 0, vec1[i].clear();
return;
}
bitset<MAXN> G[MAXN];
vector<int> vec2[MAXN];
int deg[MAXN];
queue<int> Queu;
bool Ans[MAXN];
int main(){
// freopen("1.in", "r", stdin);
cas = read();
while(cas--){
N = read(), L = read(),Q = read(); U.Init();
vec.clear();
for(int i = 1; i <= L; i++){
scanf("%s", str + 1);
for(int j = 1; j <= N; j++){
int c1 = str[2 * j - 1] - '0', c2 = str[2 * j] - '0';
int w = c1 * 50 + c2;
A[i][j] = w;
}
}
for(int i = 1; i <= Q; i++){
Query[i].id = i;
scanf("%s", str + 1);
int c1 = str[1] - '0', c2 = str[2] - '0';
int A = c1 * 50 + c2;
c1 = str[3] - '0', c2 = str[4] - '0';
int B = c1 * 50 + c2;
c1 = str[5] - '0', c2 = str[6] - '0';
int C = c1 * 50 + c2;
Query[i].A = A, Query[i].B = B, Query[i].C = C;
// cerr<<A<<" "<<B<<" "<<C<<endl;
}
sort(Query + 1, Query + Q + 1, cmp);
int pp = 1;
for(int i = 0; i <= L; i++){
Clear1();
for(auto pr: vec){
vec1[U.find(pr.fi)].pb(U.find(pr.se));
}
// for(int j = 1; j <)
for(int j = 1; j <= N; j++) if(U.find(j) == j && !vis[j]) tarjan(j);
// for(int i = 1; i <= N; i++) cerr << U.find(i) << " "; cerr << endl;
// for(int j = 1; j <= N; j++) if(U.find(j) == j) G[col[j]][j] = 1;
for(int j = 1; j <= col[0]; j++) G[j][j] = 1;
for(int j = 1; j <= col[0]; j++) vec2[j].clear(), deg[j] = 0;
// cerr << col[0] << endl;
for(auto pr: vec){
int u = U.find(pr.fi), v = U.find(pr.se);
if(col[u] == col[v]) continue;
vec2[v].pb(u), deg[u]++;
}
for(int j = 1; j <= col[0]; j++) if(!deg[j]) Queu.push(j);
while(!Queu.empty()){
int x = Queu.front(); Queu.pop();
for(auto v:vec2[x]){
deg[v]--;
G[v] |= G[x];
if(!deg[v]) Queu.push(v);
}
}
// cerr<<"i:"<<i<<endl;
while(pp <= Q && Query[pp].C == i){
int a = U.find(Query[pp].A), b = U.find(Query[pp].B), c = Query[pp].C, id = Query[pp].id;
a = col[a], b = col[b];
// cerr<<"a:"<<a<<" b:"<<b<<" pp:"<<pp<<" Q:"<<Q<<" "<<Query[pp + 1].C<<" "<<i<<endl;
Ans[id] = G[a][b];
pp++;
}
if(i != L) Press(i + 1);
}
for(int i = 1; i <= Q; i++) printf("%d", Ans[i]); printf("\n");
}
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3928kb
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: 5900kb
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'