QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#592802 | #9319. Bull Farm | revolutionary_oier | WA | 201ms | 12304kb | C++14 | 4.5kb | 2024-09-27 08:02:07 | 2024-09-27 08:02:08 |
Judging History
answer
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=2e3+10;
const int maxm=2e6+10;
int t,n,L,q,cnt_,l,tt;
char x[maxn<<2];
int fa[maxn],cnt[2],ct[maxn],dfn[maxn],low[maxn],h[maxn],deg[maxn];
bool in[maxn];
int c[maxn][maxn],head[2][maxn];
bitset<maxn>a[maxn],b[maxn];
struct qu{
int S,T,num,ans,op;
}o[maxm];
struct node{
int u,v,nxt;
}e[2][maxm],g[maxm];
inline int decode(char x,char y){
int p=0;
p=(x-'0')*50+y-'0';
return p;
}
inline bool cmp(qu nx,qu ny){return nx.num<ny.num;}
inline bool cmp1(qu nx,qu ny){return nx.op<ny.op;}
inline void ipt(){
tt=0;
scanf("%lld%lld%lld",&n,&L,&q);
for(int i=1;i<=n;i++)b[i]=0,fa[i]=i,b[i][i]=1;
for(int i=1;i<=L;i++){
scanf("%s",x);
for(int j=0;j<2*n;j+=2)c[i][j/2+1]=decode(x[j],x[j+1]);
}
// for(int i=1;i<=L;i++){
// for(int j=1;j<=n;j++)printf("%lld ",c[i][j]);
// printf("\n");
// }
for(int i=1;i<=q;i++){
scanf("%s",x);
o[i].S=decode(x[0],x[1]);
o[i].T=decode(x[2],x[3]);
o[i].num=decode(x[4],x[5]);
o[i].op=i;
if(o[i].num==0){
if(o[i].S==o[i].T)o[i].ans=1;
}
}
sort(o+1,o+q+1,cmp);
}
inline void add0(int u,int v){
e[0][++cnt[0]].v=v;
e[0][cnt[0]].u=u;
e[0][cnt[0]].nxt=head[0][u];
head[0][u]=cnt[0];
}
inline int find(int x){
if(fa[x]==x)return x;
return fa[x]=find(fa[x]);
}
inline bool make_graph(int k){
cnt[0]=0;
for(int i=1;i<=n;i++)head[0][i]=0;
for(int i=1;i<=cnt[1];i++){
int u=find(fa[e[1][i].u]),v=find(fa[e[1][i].v]);
if(u==v)continue;
add0(u,v);
}
for(int i=1;i<=n;i++)ct[i]=0;
for(int i=1;i<=n;i++)ct[c[k][i]]++;
int r=0;
for(int i=1;i<=n;i++){
if(ct[i]>2)return false;
if(ct[i]==2)++r;
}
if(r>1)return false;
if(r==0){
for(int i=1;i<=n;i++)add0(find(fa[i]),find(fa[c[k][i]]));
// printf("k1 = %lld\n",k);
}
else {
// printf("k2 = %lld\n",k);
int s,s1=-1,s2=-1;
for(int i=1;i<=n;i++){
if(ct[i]==0)s=i;
else if(ct[i]==2)r=i;
}
for(int i=1;i<=n;i++){
if(c[k][i]==r){
if(s1==-1)s1=i;
else s2=i;
}
}
add0(find(fa[s1]),find(fa[s]));
add0(find(fa[s2]),find(fa[s]));
}
return true;
}
stack<int>st;
inline void tarjan(int u){
dfn[u]=low[u]=++tt;
st.push(u);
in[u]=true;
for(int i=head[0][u];i;i=e[0][i].nxt){
int v=find(fa[e[0][i].v]);
if(!dfn[v]){
tarjan(v);
low[u]=min(low[v],low[u]);
}
else if(in[v])low[u]=min(low[v],low[u]);
}
if(low[u]==dfn[u]){
int tmp;
while(!st.empty()){
tmp=st.top();
st.pop();
in[tmp]=false;
fa[tmp]=u;
if(u==tmp)break;
b[u]|=b[tmp];
b[tmp]=0;
}
}
}
inline void add1(int u,int v){
e[1][++cnt[1]].v=v;
e[1][cnt[1]].u=u;
e[1][cnt[1]].nxt=head[1][u];
head[1][u]=cnt[1];
}
inline void add(int u,int v){
g[++cnt_].v=v;
g[cnt_].u=u;
g[cnt_].nxt=h[u];
h[u]=cnt_;
}
inline void repair_graph(){
cnt[1]=cnt_=0;
for(int i=1;i<=n;i++)h[i]=head[1][i]=0;
for(int i=1;i<=cnt[0];i++){
int u=find(fa[e[0][i].u]),v=find(fa[e[0][i].v]);
// printf("just_revolutionary_oier\n");
if(u==v)continue;
// printf("just_revolutionary_oier\n");
add1(u,v);
add(v,u);
deg[u]++;
}
}
queue<int>yt;
inline void topo_sort(){
for(int i=1;i<=n;i++)a[i]=0;
for(int i=1;i<=n;i++){
int u=find(fa[i]);
if(in[u])continue;
a[u]=b[u];
if(deg[u]==0)yt.push(find(fa[u]));
}
while(!yt.empty()){
int u=yt.front();
yt.pop();
for(int i=h[u];i;i=g[i].nxt){
int v=find(fa[g[i].v]);
a[v]|=a[u];
--deg[v];
if(deg[v]==0){
in[v]=true;
}
}
}
for(int i=1;i<=n;i++)in[i]=false;
}
inline void query(int k){
while(l<q+1){
if(o[l].num==k){
if(a[find(fa[o[l].S])][find(fa[o[l].T])])o[l].ans=1;
else o[l].ans=0;
}
else if(o[l].num>k)break;
++l;
}
}
inline void solve(){
l=1;
for(int i=1;i<=L;i++){
if(!make_graph(i)){
query(i);
continue;
}
// printf("I reached in Cheng Da community\n");
tt=0;
for(int j=1;j<=n;j++)dfn[j]=low[j]=0;
for(int j=1;j<=n;j++){
int u=find(fa[j]);
if(!dfn[u])tarjan(u);
}
repair_graph();
topo_sort();
query(i);
// for(int j=1;j<=n;j++)printf("%lld ",fa[j]);
// printf("\n");
}
sort(o+1,o+q+1,cmp1);
for(int i=1;i<=q;i++)printf("%lld",o[i].ans);
printf("\n");
}
signed main(){
scanf("%lld",&t);
while(t--){
ipt();
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 12304kb
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: 2ms
memory: 12040kb
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: 201ms
memory: 12292kb
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:
011110001101101111111111111111111101111111110111011110110110111011010111111111111111111101111111111110111111110111111111111101111111111110111111111111111111110001100111111111111111111111111011101111111111111111111111111111111111111111011011110100111110111111110111111100111111101110111111111101111110...
result:
wrong answer 2nd lines differ - expected: '111111011101111011011111111111...1111110111111110111011110101111', found: '111111011101111111111111111111...1111111111111110111111111101111'