QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#99519#6329. Colorful GraphSorting#WA 500ms7496kbC++5.4kb2023-04-22 20:25:112023-04-22 20:25:13

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-04-22 20:25:13]
  • 评测
  • 测评结果:WA
  • 用时:500ms
  • 内存:7496kb
  • [2023-04-22 20:25:11]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std ;
typedef long long ll ;
typedef unsigned long long ull ;
typedef pair < int , int > pii ;
typedef vector<int> vi;
#define fi first
#define se second
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

#define MAXN 15007
#define inf 1000000007

namespace flow {
    int n ;
    int source ;
    int sink ;
    struct tuhla {
        int to ;
        int flow , cap ;
        int rev ;
        tuhla ( ) { to = flow = cap = rev = 0 ; }
        tuhla ( int _to , int _cap , int _rev ) {
            to = _to , cap = _cap , flow = 0 , rev = _rev ;
        }
    };
    vector < tuhla > v[ MAXN ] ;
    int sz[ MAXN ] ;
    int lvl[ MAXN ] ;
    int pos[ MAXN ] ;
    void add_edge ( int x , int y , int cap ) {
        v[ x ].push_back ( tuhla ( y , cap , sz[ y ] ) ) ;
        v[ y ].push_back ( tuhla ( x , 0 , sz[ x ] ) ) ;
        ++ sz[ x ] , ++ sz[ y ] ;
    }
    bool bfs ( int st ) {
        queue < int > q ;
        for ( int i = 1 ; i <= n ; ++ i ) {
            lvl[ i ] = pos[ i ] = 0 ;
        }
        lvl[ st ] = 1 ;
        q.push ( st ) ;
        while ( !q.empty ( ) ) {
            int x = q.front ( ) ;
            q.pop ( ) ;
            for ( int i = 0 ; i < sz[ x ] ; ++ i ) {
                if ( lvl[ v[ x ][ i ].to ] == 0 && v[ x ][ i ].flow != v[ x ][ i ].cap ) {
                    lvl[ v[ x ][ i ].to ] = lvl[ x ] + 1 , q.push ( v[ x ][ i ].to ) ;
                }
            }
        }
        return ( lvl[ sink ] != 0 ) ;
    }
    int dfs ( int vertex , int curr ) {
        if ( curr == 0 || vertex == sink ) { return curr ; }
        for ( ; pos[ vertex ] < sz[ vertex ] ; ++ pos[ vertex ] ) {
            if ( lvl[ v[ vertex ][ pos[ vertex ] ].to ] == lvl[ vertex ] + 1 ) {
                int aux = dfs ( v[ vertex ][ pos[ vertex ] ].to , min ( curr , v[ vertex ][ pos[ vertex ] ].cap - v[ vertex ][ pos[ vertex ] ].flow ) ) ;
                v[ vertex ][ pos[ vertex ] ].flow += aux ;
                v[ v[ vertex ][ pos[ vertex ] ].to ][ v[ vertex ][ pos[ vertex ] ].rev ].flow -= aux ;
                if ( aux > 0 ) { return aux ; }
            }
        }
        return 0 ;
    }
    long long maxflow ( ) {
        long long maxflow = 0 ;
        while ( bfs ( source ) == true ) {
            while ( 1 ) {
                int flow = dfs ( source , inf ) ;
                if ( flow == 0 ) { break ; }
                maxflow += flow ;
            }
        }
        return maxflow ;
    }
}


int n , m ;
vector < int > gr[ MAXN ] , rv[ MAXN ] ;
int used[ MAXN ] , scc[ MAXN ] ;

stack < int > st ;

void dfs ( int x ) {
    used[ x ] = 1 ;
    for ( auto y : gr[ x ] ) {
        if ( used[ y ] == 0 ) {
            dfs ( y ) ;
        }
    }
    st.push ( x ) ;
}

void rev_dfs ( int x , int id ) {
    scc[ x ] = id ;
    for ( auto y : rv[ x ] ) {
        if ( scc[ y ] == 0 ) {
            rev_dfs ( y , id ) ;
        }
    }
}

set < int > s[ MAXN ] ;

int prv[ MAXN ] , rnk[ MAXN ] ;

int get ( int x ) {
    if ( prv[ x ] == -1 ) { return x ; }
    int y = get ( prv[ x ] ) ;
    prv[ x ] = y ;
    return y ;
}

void unite ( int x , int y ) {
    int k1 = get ( x ) , k2 = get ( y ) ;
    if ( k1 == k2 ) { return ; }
    if ( rnk[ k1 ] >= rnk[ k2 ] ) {
        rnk[ k1 ] += ( rnk[ k1 ] == rnk[ k2 ] ) ;
        prv[ k2 ] = k1 ;
    }
    else {
        prv[ k1 ] = k2 ;
    }
}

map < int , int > col ;
int tp = 0 ;

int act[ MAXN ] ;

int mv ( int x ) {
    if ( x > tp ) { return x ; }
    for ( auto e : ( flow :: v[ x ] ) ) {
        if ( e.flow > 0 ) {
            -- e.flow ;
            return mv ( e.to ) ;
        }
    }
}

void solve ( ) {
    cin >> n >> m ;
    for ( int i = 1 , x , y ; i <= m ; ++ i ) {
        cin >> x >> y ;
        gr[ x ].push_back ( y ) ;
        rv[ y ].push_back ( x ) ;
    }
    for ( int i = 1 ; i <= n ; ++ i ) {
        if ( used[ i ] == 0 ) {
            dfs ( i ) ;
        }
    }

    while ( st.empty ( ) == false ) {
        int x = st.top ( ) ; st.pop ( ) ;
        if ( scc[ x ] > 0 ) { continue ; }
        rev_dfs ( x , ++ tp ) ;
    }
    for ( int i = 1 ; i <= n ; ++ i ) {
        for ( auto j : gr[ i ] ) {
            if ( scc[ i ] != scc[ j ] && s[ scc[ i ] ].find ( scc[ j ] ) == s[ scc[ i ] ].end ( ) ) {
                s[ scc[ i ] ].insert ( scc[ j ] ) ;
            }
        }
    }
    flow :: n = flow :: sink = 2 * tp + 2 ;
    flow :: source = 2 * tp + 1 ;
    for ( int i = 1 ; i <= tp ; ++ i ) {
        flow :: add_edge ( flow :: source , i , 1 ) ;
        flow :: add_edge ( tp + i , flow :: sink , 1 ) ;
        for ( auto j : s[ i ] ) {
            flow :: add_edge ( i , j , inf ) ;
            flow :: add_edge ( i , tp + j , 1 ) ;
        }
    }
    ll ans = flow :: maxflow ( ) ;
    // cout << tp - ans << "\n" ;
    for ( int i = 1 ; i <= tp ; ++ i ) {
        prv[ i ] = -1 , rnk[ i ] = 0 ;
    }
    for ( auto e : ( flow :: v[ flow :: source ] ) ) {
        if ( e.flow > 0 ) {
            int hh = mv ( e.to ) ;
            unite ( e.to , hh - tp ) ;
        }
    }
    int mxcol = 0 ;
    for ( int i = 1 ; i <= tp ; ++ i ) {
        int root = get ( i ) ;
        if ( col[ root ] == 0 ) {
            col[ root ] = ++ mxcol ;
        }
    }
    for ( int i = 1 ; i <= n ; ++ i ) {
        cout << col[ get ( scc[ i ] ) ] << " " ;
    }
    cout << "\n" ;
}

int main ( ) {
    ios_base :: sync_with_stdio ( false ) ;
    cin.tie ( NULL ) ;
    int t = 1 ; // cin >> t ; 
    while ( t -- ) { solve ( ) ; }
    return 0 ;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 5160kb

input:

5 5
1 4
2 3
1 3
2 5
5 1

output:

1 1 1 2 1 

result:

ok AC

Test #2:

score: 0
Accepted
time: 3ms
memory: 5212kb

input:

5 7
1 2
2 1
4 3
5 1
5 4
4 1
4 5

output:

2 2 1 1 1 

result:

ok AC

Test #3:

score: 0
Accepted
time: 3ms
memory: 5140kb

input:

8 6
6 1
3 4
3 6
2 3
4 1
6 4

output:

4 4 4 4 3 4 2 1 

result:

ok AC

Test #4:

score: -100
Wrong Answer
time: 500ms
memory: 7496kb

input:

7000 6999
4365 4296
2980 3141
6820 4995
4781 24
2416 5844
2940 2675
3293 2163
3853 5356
262 6706
1985 1497
5241 3803
353 1624
5838 4708
5452 3019
2029 6161
3849 4219
1095 1453
4268 4567
1184 1857
2911 3977
1662 2751
6353 6496
2002 6628
1407 4623
425 1331
4445 4277
1259 3165
4994 1044
2756 5788
5496 ...

output:

1 94 425 187 1 1 1 386 641 246 1 1 1 1 232 1 1235 1 690 1726 494 1166 1 253 89 1 1 1 347 1 580 553 1 1 394 393 1465 1 1 1 226 593 1 1217 1 1064 1 1 1 1 861 1 459 1 104 1564 1 1 720 1 559 1 1 1 1593 1 1 1 245 1 1 1041 1 770 1 572 1 1181 819 1 1 1 432 442 1 454 592 736 588 1 326 1 1 1445 1 1551 633 73...

result:

wrong answer 5 and 1 are not connected