QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#73070#5160. Kebab PizzaSorting#WA 4ms8928kbC++2.7kb2023-01-21 21:06:322023-01-21 21:06:33

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-01-21 21:06:33]
  • 评测
  • 测评结果:WA
  • 用时:4ms
  • 内存:8928kb
  • [2023-01-21 21:06:32]
  • 提交

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 rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()

const int MAXN = 1e5 + 7 ;

int n , k ;
set < int > v[ MAXN ] ;
bool used[ MAXN ] ;
pii a[ MAXN ] ;

int prv[ MAXN ] , rnk[ MAXN ] ;
bool leaf[ MAXN ] ;

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

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

void solve ( ) {
    cin >> n >> k ;
    for ( int i = 1 ; i <= n ; ++ i ) {
        cin >> a[ i ].fi >> a[ i ].se ;
        used[ a[ i ].fi ] = used[ a[ i ].se ] = true ;
        if ( a[ i ].fi != a[ i ].se ) {
            v[ a[ i ].fi ].insert ( a[ i ].se ) ;
            v[ a[ i ].se ].insert ( a[ i ].fi ) ;
        }
    }
    for ( int i = 1 ; i <= k ; ++ i ) {
        prv[ i ] = -1 ;
        if ( used[ i ] == true && v[ i ].size ( ) == 1 ) { leaf[ i ] = true ; }
    }
    int cycles = 0 ;
    for ( int i = 1 ; i <= k ; ++ i ) {
        if ( used[ i ] == false ) { continue ; }
        int cnt = 0 ;
        for ( auto y : v[ i ] ) {
            if ( leaf[ y ] == false ) { ++ cnt ; }
        }
        if ( cnt > 2 ) {
            cout << "impossible\n" ;
            return ;
        }
    }
    set < pii > foo ;
    for ( int i = 1 ; i <= n ; ++ i ) {
        if ( a[ i ].fi > a[ i ].se ) { swap ( a[ i ].fi , a[ i ].se ) ; }
        if ( a[ i ].fi != a[ i ].se && foo.find ( { a[ i ].fi , a[ i ].se } ) == foo.end ( ) ) {
            foo.insert ( { a[ i ].fi , a[ i ].se } ) ;
            cycles += unite ( a[ i ].fi , a[ i ].se ) ;
        }
    }
    if ( cycles > 1 ) {
        cout << "impossible\n" ;
        return ;
    }
    if ( cycles == 0 ) {
        cout << "possible\n" ;
        return ;
    }
    int rep = -1 ;
    for ( int i = 1 ; i <= k ; ++ i ) {
        if ( used[ i ] == false ) { continue ; }
        int hh = get ( i ) ;
        if ( rep == -1 ) { rep = hh ; }
        if ( rep != hh ) {
            cout << "impossible\n" ;
            return ;
        }
    }
    cout << "possible\n" ;
}

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

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 8240kb

input:

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

output:

possible

result:

ok single line: 'possible'

Test #2:

score: 0
Accepted
time: 4ms
memory: 8496kb

input:

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

output:

possible

result:

ok single line: 'possible'

Test #3:

score: 0
Accepted
time: 1ms
memory: 8912kb

input:

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

output:

impossible

result:

ok single line: 'impossible'

Test #4:

score: 0
Accepted
time: 4ms
memory: 8796kb

input:

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

output:

possible

result:

ok single line: 'possible'

Test #5:

score: 0
Accepted
time: 0ms
memory: 8144kb

input:

4 4
1 2
2 1
3 4
4 3

output:

possible

result:

ok single line: 'possible'

Test #6:

score: 0
Accepted
time: 4ms
memory: 8928kb

input:

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

output:

possible

result:

ok single line: 'possible'

Test #7:

score: -100
Wrong Answer
time: 2ms
memory: 8456kb

input:

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

output:

possible

result:

wrong answer 1st lines differ - expected: 'impossible', found: 'possible'