QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#565717#9319. Bull FarmmitthuWA 1ms7924kbC++143.3kb2024-09-15 22:04:582024-09-15 22:04:59

Judging History

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

  • [2024-09-15 22:04:59]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:7924kb
  • [2024-09-15 22:04:58]
  • 提交

answer

#include<bits/stdc++.h>
const int maxn=1e5+5;
const int maxm=2e3+5;
const int maxq=1e6+5;
using namespace std;
int T,n,l,Q,t[maxm][maxm],in[maxm];
int fa[maxm],ans[maxq];
char s[maxn];
queue<int>q; 
vector<pair<int,int> >edge;
vector<int>to[maxm];
bitset<maxm>f[maxm];
int find(int x){
    return (fa[x]==x)?x:fa[x]=find(fa[x]);
}
void merge(int x,int y){
    x=find(x);y=find(y);
    if(x==y)return;
    fa[x]=y;
}
int work(){
    int res=0;
    for(int i=1;i<=n;i++)res+=(in[i]==1);
    if(res==n)return 1;
    if(res==n-2){
        int res1=0;
        for(int i=1;i<=n;i++)res1+=(in[i]==2);
        if(res1==1)return 2;
    }
    return 0;
}
void suo(int u, int v) {
    u = find(u), v = find(v);
    for (int i = 1; i <= n; i++) {
        int x = find(i);
        if (f[u][x] && f[x][v]) merge(x, u);
    }
    merge(u,v);
}
struct node{
    int a,b,c,num;
    bool operator<(const node &x)const{
        return c<x.c;
    }
}p[maxq];
void solve(){
    scanf("%d %d %d",&n,&l,&Q);
    for(int i=0;i<=n;i++)f[i].reset(),to[i].clear(),in[i]=0;
    edge.clear();
    for(int i=1;i<=l;i++){
        scanf("%s",s+1);
        for(int j=1,k=1;j<=n+n;j+=2,k++)
            t[i][k]=(s[j]-'0')*50+(s[j+1]-'0');
    }
    for(int i=1;i<=Q;i++){
        scanf("%s",s+1);
        p[i].num=i;
        p[i].a=(s[1]-'0')*50+(s[2]-'0');
        p[i].b=(s[3]-'0')*50+(s[4]-'0');
        p[i].c=(s[5]-'0')*50+(s[6]-'0'); 
    }
    for(int i=1;i<=n;i++)fa[i]=i;
    sort(p+1,p+Q+1);
    int pos=1;
    while(p[pos].c==0){
        if(p[pos].a!=p[pos].b)ans[p[pos].num]=0;
        else ans[p[pos].num]=1;
        pos++;
    }
    for(int i=1;i<=l;i++){
        for(int j=1;j<=n;j++)in[j]=0;
        for(int j=1;j<=n;j++)in[t[i][j]]++;
        int res=work();
        if(res==2){
            int x=0,y=0;
            for(int j=1;j<=n;j++)
                if(in[j]==0)y=j;
                else if(in[j]==2)x=j;
            for (int j=1;j<=n;j++)
                if(t[i][j]==x&&y!=j) {
                    edge.push_back({j,y});
                    suo(j,y);
                }
        }
        if(res==1)
            for(int j=1;j<=n;j++)merge(j, t[i][j]);
        for (int j = 1; j <= n; j++) to[j].clear(), f[j].reset(),in[j]=0;
        for (auto E : edge) {
            int u = E.first, v = E.second;
            u = find(u), v = find(v);
            if (u != v) to[u].push_back(v),in[v]++;
        }
        for (int j = 1; j <= n; j++) f[find(j)][find(j)]=1;
        for(int j=1;j<=n;j++)if(!in[find(j)])q.push(find(j)), in[find(j)] = 1;
        while(!q.empty()){
            int x=q.front();q.pop();
            for (auto v : to[x]){
                f[v] |= f[x];
                in[v]--;
                if(!in[v])q.push(v);
            }
        }
        while (p[pos].c == i&&pos<=Q){
            int x=find(p[pos].a),y=find(p[pos].b);
            // cout<<p[pos].a<<" "<<p[pos].b<<" "<<x<<" "<<y<<" "<<f[y][x]<<endl;
            if(f[y][x])ans[p[pos].num]=1;
            else ans[p[pos].num]=0;
            pos++;
        }
    }
    for(int i=1;i<=Q;i++)printf("%d",ans[i]);puts("");
}
int main(){
//freopen("1.in","r",stdin);
//    freopen("1.out","w",stdout);
    scanf("%d",&T);
    for(;T;T--)solve();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 7872kb

input:

1
3 3 6
020202
030301
030201
020102
030203
010201
010303
020303
010202

output:

110111

result:

wrong answer 1st lines differ - expected: '010101', found: '110111'