QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#566894 | #9319. Bull Farm | Yzm007 | RE | 0ms | 0kb | C++23 | 4.4kb | 2024-09-16 02:58:17 | 2024-09-16 02:58:18 |
answer
#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define per(i,a,b) for(int i=(a);i>=(b);--i)
typedef long long ll;
typedef double db;
typedef pair<int,int> P;
#define fi first
#define se second
#define pb push_back
#define dbg(x) cerr<<(#x)<<":"<<x<<" ";
#define dbg2(x) cerr<<(#x)<<":"<<x<<endl;
#define SZ(a) (int)(a.size())
#define sci(a) scanf("%d",&(a))
#define scll(a) scanf("%lld",&(a))
#define pt(a) printf("%d",a);
#define pte(a) printf("%d\n",a)
#define ptlle(a) printf("%lld\n",a)
#define debug(...) fprintf(stderr, __VA_ARGS__)
const int N=4e3+10,M=5e3+10,X=1e6+10,K=256;
int c,n,l,q,g[K][K];
int to[N][N],par[N],deg[N],Q[N],now[N];
char s[X],ans[X];
vector<array<int,3>>ask[N];
int head[N],cnt;
int low[N],dfn[N],num;//最早非负祖先时间戳 时间戳
int stk[N],top;//用数组模拟栈 栈顶 当前栈顶值
int col[N],tot;//染色 颜色数
bool in[N];//是否在栈中
vector<int>all,F[N];
P E[M];
int find(int x){
return par[x]==x?x:par[x]=find(par[x]);
}
void mer(int x,int y){
//if(!x || !y)while(1);
x=find(x),y=find(y);
if(x==y)return;
par[y]=x;
}
int f(char x,char y){
int nx=x,ny=y;
//if(nx>100 || ny>100)while(1);
return g[nx][ny];
}
struct edge{
int to,next;
}e[M];
void add(int u,int v){
e[++cnt].to=v;
e[cnt].next=head[u];
head[u]=cnt;
}
void dfs(int u){
low[u]=dfn[u]=++num;
in[u]=1;
stk[++top]=u;
for(int i=head[u];i;i=e[i].next){
int v=e[i].to;
if(!dfn[v]){
dfs(v);
low[u]=min(low[u],low[v]);
}
else if(in[v]){
low[u]=min(low[u],dfn[v]);
}
}
if(low[u]==dfn[u]){
tot++;
int now;
do{
now=stk[top--];
col[now]=tot;
in[now]=0;
}while(now!=u);
}
}
void init(int n){
rep(i,1,n){
head[i]=in[i]=dfn[i]=low[i]=deg[i]=now[i]=0;
F[i].clear();
}
}
template<int LEN>void solve(int p){
if(LEN<=n){solve<min(N,LEN<<1)>(p);return;}
if(p){
init(n);
bool no=0;
int two=0;
rep(j,1,n){
int v=to[p][j];
now[v]++;
if(now[v]==2)two++;
if(now[v]==3)no=1;
}
if(two>=2)no=1;
//printf("i:%d no:%1d two:%d\n",i,no,two);
if(!no){
if(!two){
rep(j,1,n){
int v=to[p][j];
if(j==v)continue;
//printf("p:%d j:%d v:%d\n",p,j,v);
mer(j,v);
}
}
else{
int pos=0;
rep(j,1,n){
if(!now[j]){
pos=j;
break;
}
}
rep(j,1,n){
int v=to[p][j];
if(now[v]==2){
E[++c]=P(j,pos);
}
}
}
}
all.clear();
rep(j,1,c){
int u=E[j].fi,v=E[j].se;
int fj=find(u),fp=find(v);
//printf("p:%d j:%d v:%d pos:%d\n",p,j,v,pos);
if(fj==fp)continue;
add(fj,fp);
}
rep(j,1,n){
if(find(j)==j){
//printf("p:%d j:%d\n",p,j);
all.pb(j);
if(!dfn[j])dfs(j);
}
}
vector<bitset<LEN>>can(n+1,bitset<LEN>());
int t=0;
for(auto &u:all){
for(int i=head[u];i;i=e[i].next){
int v=e[i].to;
//printf("u:%d v:%d\n",u,v);
if(col[u]==col[v])continue;
F[col[u]].pb(col[v]);
deg[col[v]]++;
}
}
rep(i,1,tot){
if(!deg[i])Q[t++]=i;
}
for(int s=0;s<t;++s){
int u=Q[s];
for(auto &v:F[u]){
if((--deg[v])==0){
Q[t++]=v;
}
}
}
for(int i=t-1;i>=0;--i){
int u=Q[i];
//if(u>LEN)while(1);
can[u]=bitset<LEN>();
can[u][u]=1;
for(auto &v:F[u]){
can[u]|=can[v];
}
}
for(auto &x:ask[p]){
int a=x[0],b=x[1],id=x[2];
//printf("a:%d fa:%d b:%d fb:%d ca:%d cb:%d\n",a,find(a),b,find(b),col[find(a)],col[find(b)]);
a=col[find(a)],b=col[find(b)];
ans[id]=can[a].test(b)?'1':'0';
}
}
else{
for(auto &x:ask[p]){
int a=x[0],b=x[1],id=x[2];
ans[id]=(a==b)?'1':'0';
}
}
ask[p].clear();
}
void sol(){
sci(n),sci(l),sci(q);
rep(i,1,n)par[i]=i;
rep(i,1,l){
scanf("%s",s+1);
for(int j=1;j<=2*n;j+=2){
to[i][(j+1)/2]=f(s[j],s[j+1]);
//printf("%d ",to[i][(j+1)/2]);
}
//puts("");
}
c=0;
rep(i,1,q){
scanf("%s",s+1);
int a=f(s[1],s[2]),b=f(s[3],s[4]),p=f(s[5],s[6]);
//printf("%d %d %d\n",a,b,c);
ask[p].pb({a,b,i});
}
rep(i,0,l){
solve<1>(i);
}
ans[q+1]='\0';
printf("%s\n",ans+1);
}
int main(){
//freopen("24L.in","r",stdin);
//freopen("2024L.out","w",stdout);
rep(i,0,5000){
int x=48+(i/50),y=48+(i%50);
g[x][y]=i;
}
int T;
sci(T); // t=1
while(T--){
sol();
}
return 0;
}
詳細信息
Test #1:
score: 0
Runtime Error
input:
2 5 2 4 0305040201 0404040404 030300 020500 050102 020501 6 2 4 030603010601 010203060504 030202 060402 050602 060401