QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#566119#9319. Bull FarmIzumihanakoWA 117ms26892kbC++146.2kb2024-09-15 23:05:302024-09-15 23:05:33

Judging History

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

  • [2024-09-15 23:05:33]
  • 评测
  • 测评结果:WA
  • 用时:117ms
  • 内存:26892kb
  • [2024-09-15 23:05:30]
  • 提交

answer

#include <queue>
#include <vector>
#include <bitset>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ;

struct Graph{
    struct Path{
        int pre , to ;
    } p[6005] ;
    int head[2005] , tp , N ;
    int sta[2005] , topp ;
    int dfn[2005] , dfs_c , scc[2005] , scc_cnt , inWhichId[2005] , indeg[2005] ;

    void init( int n_ ){
        for( int i = 0 ; i <= n_ ; i ++ ){
            indeg[i] = head[i] = dfn[i] = scc[i] = 0 ;
            inWhichId[i] = i ;
        }
        dfs_c = tp = topp = scc_cnt = 0 ;
        N = n_ ;
    }
    void In( int t1 , int t2 ) {
        if( t1 == t2 ) return ;
        indeg[t2] ++ ;
        p[++tp] = (Path){ head[t1] , t2 } , head[t1] = tp ;
    }
    int dfs( int u ){
        int lowu = dfn[u] = ++ dfs_c ;
        sta[++topp] = u ;
        for( int i = head[u] ; i ; i = p[i].pre ){
            int v = p[i].to ;
            if( !dfn[v] ) lowu = min( lowu , dfs( v ) ) ;
            else if( !scc[v] ) lowu = min( lowu , dfn[v] ) ;
        }
        if( lowu == dfn[u] ) {
            scc_cnt ++ ;
            while( sta[topp] != u ){
                scc[ sta[topp] ] = scc_cnt ;
                topp -- ;
            } scc[ sta[topp] ] = scc_cnt , topp -- ;
        }
        return lowu ;
    }
    void SCC(){
        for( int i = 1 ; i <= N ; i ++ ){
            if( !dfn[i] ) dfs( i ) ;
        }
        // printf( "SCC( cnt = %d):" , scc_cnt ) ;
        // for( int i = 1 ; i <= N ; i ++ ) printf( "%d %d | " , scc[i] , dfn[i] ) ;
        // puts( ""  ) ;
    }
    void buildnew( Graph &newG ){
        newG.init( scc_cnt ) ;
        for( int u = 1 ; u <= N ; u ++ ){
            for( int i = head[u] ; i ; i = p[i].pre ){
                int v = p[i].to ;
                if( scc[u] == scc[v] ) continue ;
                newG.In( scc[u] , scc[v] ) ;
            }
        }
        for( int i = 1 ; i <= N ; i ++ ){
            newG.inWhichId[i] = scc[ this->inWhichId[i] ] ;
        }
    }
    void build_reverse( Graph &newG ){
        newG.init( scc_cnt ) ;
        for( int u = 1 ; u <= N ; u ++ ){
            for( int i = head[u] ; i ; i = p[i].pre ){
                int v = p[i].to ;
                if( scc[u] == scc[v] ) continue ;
                newG.In( scc[v] , scc[u] ) ;
            }
        }
        for( int i = 1 ; i <= N ; i ++ ){
            newG.inWhichId[i] = scc[ this->inWhichId[i] ] ;
        }
    }
} G[2005] , revG ;

struct Button{
    int p[2005] , invalid ;
    struct Edge{
        int fr , to ;
    } ;
    vector< Edge > edges ;
    void init( char *ss , int N ){
        invalid = 0 ;
        int c[2005] , whereinvalid = 0 ;
        for( int i = 1 ; i <= N ; i ++ ) c[i] = 0 ;
        for( int i = 1 ; i <= N ; i ++ ){
            int c1 = (int)ss[i*2-1] , c2 = (int)ss[i*2] ;
            p[i] = ( c1 - 48 ) * 50 + c2 - 48 ;
            if( !c[ p[i] ] ){
                c[ p[i] ] = i ;
            } else {
                if( !whereinvalid ){
                    whereinvalid = i ; // position
                } else {
                    invalid = 1 ;
                }
            }
        }
        edges.clear() ;
        // printf( "Button:" ) ;
        // for( int i = 1 ; i <= N ; i ++ ) printf( "%d " , p[i] ) ;
        // puts("" ) ;
        if( invalid ) return ;
        else if( whereinvalid ){
            int emptynum = 0 ;
            for( int i = 1 ; i <= N ; i ++ )
                if( !c[i] ) { emptynum = i ; break ; }
            edges.push_back( (Edge){ whereinvalid , emptynum } ) ;
            edges.push_back( (Edge){ c[p[whereinvalid]] , emptynum } ) ;
        } else {
            for( int i = 1 ; i <= N ; i ++ ){
                edges.push_back( (Edge){ i , p[i] } ) ;
            }
        }
    }
} b[2005] ;

struct Query {
    int A , B , C , pos , ans ;
    bool operator < ( const Query &AA ) const {
        return C < AA.C ;
    }
    int decode( int x , int y ){
        return ( x - 48 ) * 50 + y - 48 ;
    }
    void init( char *ss ){
        A = decode( ss[1] , ss[2] ) ;
        B = decode( ss[3] , ss[4] ) ;
        C = decode( ss[5] , ss[6] ) ;        
        // printf( "Query: %d %d %d"  , A , B , C ) ;
        // puts("" ) ;
    }
} q[1000005]  ;
int ans[1000005] ;

int N , L , Q , qpt ;
void solve(){
    qpt = 1 ;
    sort( q + 1 , q + Q + 1 ) ;
    G[0].init( N ) ;
    for( int i = 0 ; i <= L ; i ++ ){
        for( auto e : b[i].edges ){
            // printf( "origin(%d to %d), inGraph(%d to %d)\n" , e.fr , e.to , G[i].inWhichId[e.fr] , G[i].inWhichId[e.to] ) ;
            G[i].In( G[i].inWhichId[e.fr] , G[i].inWhichId[e.to] ) ;
        }
        G[i].SCC() ;
        G[i].buildnew( G[i+1] ) ;
        G[i].build_reverse( revG ) ;

        bitset<2005> f[2005] ;
        queue<int> que ;
        for( int i = 1 ; i <= revG.N ; i ++ ){
            if( revG.indeg[i] == 0 ) que.push( i ) ;
            f[i].reset() ;
            f[i][i] = 1 ;
        }
        while( que.empty() == false ){
            int u = que.front() ;
            que.pop() ;
            for( int i = revG.head[u] ; i ; i = revG.p[i].pre ){
                int v = revG.p[i].to ;
                revG.indeg[v] -- ;
                if( revG.indeg[v] == 0 ){
                    que.push( v ) ;
                }
                f[v] |= f[u] ;
            }
        }

        while( qpt <= Q && q[qpt].C <= i ){
            int u = q[qpt].A , v = q[qpt].B ;
            u = revG.inWhichId[u] , v = revG.inWhichId[v] ;
            q[qpt].ans = f[u][v] ;
            qpt ++ ;
        }
    }
}

int main(){
    int T ;
    scanf( "%d" , &T ) ;
    while( T -- ){
        char ss[20005] ; 
        scanf( "%d%d%d" , &N , &L , &Q ) ;
        for( int i = 1 ; i <= L ; i ++ ){
            scanf( "%s" , ss + 1 ) ;
            b[i].init( ss , N ) ;
        }
        for( int i = 1 ; i <= Q ; i ++ ){
            scanf( "%s" , ss + 1 ) ;
            q[i].init( ss ) ;
            q[i].pos = i ;
        }
        solve() ;
        for( int i = 1 ; i <= Q ; i ++ ) ans[ q[i].pos ] = q[i].ans ;
        for( int i = 1 ; i <= Q ; i ++ ) printf( "%d" , ans[i] ) ;
        puts( "" ) ;
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 4ms
memory: 22728kb

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

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: 117ms
memory: 26892kb

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:

000000000000101110000000000100000000000001000000001100000000001000000101111000000000000001110011000000000000000000000001001000100000000000100000010010000000000000000010101000010000000000000000100000000010000010001000010000000000100000001000010000000000001100100000100000000000000010000100001100101100...

result:

wrong answer 1st lines differ - expected: '011110001101101111111111111111...1111111111111111101111111111111', found: '000000000000101110000000000100...0000000000000101001100100000100'