QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#566062#9319. Bull Farm_CHORE 1ms3932kbC++203.5kb2024-09-15 22:57:322024-09-15 22:57:33

Judging History

你现在查看的是最新测评结果

  • [2024-09-15 22:57:33]
  • 评测
  • 测评结果:RE
  • 用时:1ms
  • 内存:3932kb
  • [2024-09-15 22:57:32]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define i32 int
#define i64 long long
#define i128 __int128
#define pii pair<i32,i32>
#define pll pair<i64,i64>
const int mod = 1e+9+7;
const int maxn = 2024;

i32 read(){
    char c,d;cin>>c>>d;
    return (c-48)*50+d-48;
}



i32 n,m,Q;
i32 t[maxn][maxn];
struct Query{
    i32 a,b,idx;
};
vector<Query> qry[maxn];
bool ans[maxn];

i32 ice[maxn];

vector<i32> from[maxn];
vector<pii> edge;
vector<i32> G[maxn];

stack<i32> stk;
i32 scc[maxn],scc_cnt;
i32 dfs_clk,dfn[maxn],low[maxn];

vector<i32> G2[maxn];
bool vis[maxn];
bitset<maxn> to[maxn];


void init(){
    scc_cnt=dfs_clk=0;
    for(i32 i=1;i<=n;++i){
        from[i].clear();
        scc[i]=dfn[i]=low[i]=0;
        G[i].clear();
        G2[i].clear();
        vis[i]=false;
        to[i].reset();
    }
    while(!stk.empty()) stk.pop();
}

i32 find(i32 u){
    if(u<=0||u>n){
        printf("%d\n",u);
        exit(0);
    }
    return u==ice[u]?u:ice[u]=find(ice[u]);
}
void merge(i32 u,i32 v){
    ice[find(u)] = find(v);
}

void Tarjan(i32 u){
    dfn[u]=low[u]=++dfs_clk;
    stk.push(u);
    for(i32 v:G[u]){
        if(!dfn[v]){
            Tarjan(v);
            low[u]=min(low[u],low[v]);
        }else if(!scc[v]){
            low[u]=min(low[u],dfn[v]);
        }
    }
    if(dfn[u]==low[u]){
        ++scc_cnt;
        i32 t;
        do{
            scc[t=stk.top()] = u;
            stk.pop();
        }while(t!=u);
    }
}
void dfs(i32 u){
    if(vis[u]) return ;
    vis[u] = true;
    to[u][u] = 1;
    for(i32 v:G2[u]){
        dfs(v);
        to[u]|=to[v];
    }
}
void update(i32 button){
    init();
    for(i32 i=1;i<=n;++i){
        from[t[button][i]].push_back(i);
    }
    i32 blank=0;
    for(i32 i=1;i<=n;++i) blank += (from[i].size()==0);
    if(blank==0){
        for(i32 i=1;i<=n;++i) merge(i,t[button][i]);
    }else if(blank==1){
        for(i32 i=1;i<=n;++i)if(from[i].size()==0){
            for(i32 j=1;j<=n;++j)if(from[j].size()==2){
                edge.emplace_back(from[j][0],i);
                edge.emplace_back(from[j][1],i);
            }
        }
    }

    for(auto[u,v]:edge) G[find(u)].push_back(find(v));
    for(i32 i=1;i<=n;++i)if(!scc[i]) Tarjan(i);
    for(auto[u,v]:edge){
        if(scc[find(u)]!=scc[find(v)]) G2[scc[find(u)]].push_back(scc[find(v)]);
    }
    for(i32 i=1;i<=scc_cnt;++i) if(!vis[i]) dfs(i);
}
void Main(){
    cin>>n>>m>>Q;
    for(i32 i=1;i<=n;++i) ice[i] = i;
    for(i32 i=0;i<=m;++i) qry[i].clear();
    edge.clear();

    for(i32 i=1;i<=m;++i){
        for(i32 j=1;j<=n;++j) t[i][j]=read();
    }
    for(i32 i=1;i<=Q;++i){
        i32 a=read(),b=read(),c=read();
        qry[c].push_back({a,b,i});
    }


    for(auto[a,b,idx]:qry[0]){
        ans[idx] = a==b;
    }
    for(i32 c=1;c<=m;++c){
        update(c);
        for(auto[a,b,idx]:qry[c]){
            //printf("[%d,%d] \n",a,b);
            if(scc[find(a)]<0||scc[find(a)]>n||scc[find(b)]<0||scc[find(b)]>n){
                printf("%d %d\n",scc[find(a)],scc[find(b)]);
                exit(0);
            }
            ans[idx] = (to[scc[find(a)]][scc[find(b)]]?1:0);
        }
        //if(c==4) return ;
    }
    for(i32 i=1;i<=Q;++i) cout<<ans[i];
    cout<<'\n';
}
int main(){
	//fac[0]=ifac[0]=1; for(i32 i=1;i<maxn;++i) fac[i]=fac[i-1]*i%mod,ifac[i]=fpow(fac[i],mod-2);
	ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
	i32 T=1;
	cin>>T;
	while(T--){
		Main();
	}
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3932kb

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: 0ms
memory: 3632kb

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
Runtime Error

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:


result: