QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#99513 | #6329. Colorful Graph | Sorting# | WA | 513ms | 7552kb | C++ | 5.3kb | 2023-04-22 20:01:15 | 2023-04-22 20:01:19 |
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 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 ;
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 ) ;
}
}
int tp = 0 ;
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 ( int i = 1 ; i <= tp ; ++ i ) {
for ( auto e : ( flow :: v[ i ] ) ) {
if ( e.flow > 0 && ( tp < e.to && e.to <= 2 * tp ) ) {
unite ( i , e.to - 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 ;
}
详细
Test #1:
score: 100
Accepted
time: 3ms
memory: 5156kb
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: 1ms
memory: 5276kb
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: 2ms
memory: 5136kb
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: 513ms
memory: 7552kb
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:
1750 153 153 153 1383 1749 1748 153 153 153 1342 373 1747 157 153 1746 153 1745 153 153 153 153 135 153 153 1744 1743 996 153 1742 153 153 1741 1740 153 153 153 1739 111 1738 153 153 1737 153 1736 153 1735 1060 1734 1733 153 1732 153 355 153 153 850 1731 153 1730 153 564 1484 1205 153 1729 57 554 15...
result:
wrong answer 10 and 3 are not connected