QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#73071 | #5160. Kebab Pizza | Sorting# | WA | 2ms | 9436kb | C++ | 2.4kb | 2023-01-21 21:27:36 | 2023-01-21 21:27:37 |
Judging History
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 ;
}
}
for ( int i = 1 ; i <= n ; ++ i ) {
if ( a[ i ].fi != a[ i ].se ) {
cycles += unite ( a[ i ].fi , a[ i ].se ) ;
}
}
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: 2ms
memory: 8036kb
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: 1ms
memory: 8112kb
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: 8424kb
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: -100
Wrong Answer
time: 0ms
memory: 9436kb
input:
8 4 1 1 1 2 2 1 2 2 3 3 3 4 4 3 4 4
output:
impossible
result:
wrong answer 1st lines differ - expected: 'possible', found: 'impossible'