QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#73072 | #5160. Kebab Pizza | Sorting# | WA | 5ms | 9712kb | C++ | 2.4kb | 2023-01-21 21:32:41 | 2023-01-21 21:32:43 |
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 <= k ; ++ i ) {
for ( auto y : v[ i ] ) {
if ( i < y ) {
cycles += unite ( i , y ) ;
}
}
}
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: 4ms
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: 9712kb
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: 9696kb
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: 1ms
memory: 9156kb
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: 5ms
memory: 8192kb
input:
4 4 1 2 2 1 3 4 4 3
output:
possible
result:
ok single line: 'possible'
Test #6:
score: 0
Accepted
time: 2ms
memory: 8088kb
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: 4ms
memory: 8288kb
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'