大家都很强, 可与之共勉 。
题意:
假设有
n
根柱子,现要按下述规则在这
每次只能在某根柱子的最上面放球。
在同一根柱子中,任何
2
个相邻球的编号之和为完全平方数。
试设计一个算法,计算出在
题解:
转化为最小路径覆盖,即每一根柱子当成一条路。枚举解(或者二分答案),然后按照其和为平方数原则建边,从小数向大数连边。
注意到输出方案时需要重新建图……
# include <bits/stdc++.h>
# define N 5010
class Network {
private :
struct edge {
int to, w, nxt ;
edge ( ) { }
edge ( int to, int w, int nxt ) : to ( to ), w ( w ), nxt ( nxt ) { }
} g [60010 << 1] ;
int head [N], cur [N], ecnt ;
int S, T , dep [N] ;
int to [N] ;
inline int dfs ( int u, int a ) {
if ( u == T || ! a ) return a ;
int flow = 0, v, f ;
for ( int& i = cur [u] ; i ; i = g [i].nxt ) {
v = g [i].to ;
if ( dep [v] == dep [u] + 1 ) {
f = dfs ( v, std :: min ( g [i].w, a - flow ) ) ;
g [i].w -= f, g [i ^ 1].w += f ;
to [u] = v ;
flow += f ;
if ( a == flow ) return a ;
}
}
if ( ! flow ) dep [u] = -1 ;
return flow ;
}
inline bool bfs ( int S, int T ) {
static std :: queue < int > q ;
memset ( dep, 0, sizeof ( int ) * ( T + 1 ) ) ;
dep [S] = 1 ;
q.push ( S ) ;
while ( ! q.empty ( ) ) {
int u = q.front ( ) ; q.pop ( ) ;
for ( int i = head [u] ; i ; i = g [i].nxt ) {
int& v = g [i].to ;
if ( g [i].w && ! dep [v] ) {
dep [v] = dep [u] + 1 ;
q.push ( v ) ;
}
}
}
return dep [T] ;
}
public :
Network ( ) { ecnt = 1 ; }
inline void add_edge ( int u, int v, int w ) {
g [++ ecnt] = edge ( v, w, head [u] ) ; head [u] = ecnt ;
g [++ ecnt] = edge ( u, 0, head [v] ) ; head [v] = ecnt ;
}
inline void clear ( ) {
ecnt = 1 ;
memset ( head, 0, sizeof head ) ;
}
inline int dinic ( int S, int T ) {
this -> S = S, this -> T = T ;
static int rt = 0 ; // static ;
while ( bfs ( S, T ) ) {
memcpy ( cur, head, sizeof ( int ) * ( T + 1 ) ) ;
rt += dfs ( S, 0x3f3f3f3f ) ;
}
return rt ;
}
void display ( int n, int S, int T ) {
clear ( ) ;
for ( int i = 1 ; i <= n ; ++ i ) {
add_edge ( S, i, 1 ) ;
add_edge ( i + 2500, T, 1 ) ;
for ( int j = 1 ; j < i ; ++ j ) {
if ( sqrt ( i + j ) == floor ( sqrt ( i + j ) ) ) {
add_edge ( j, i + 2500, 1 ) ;
}
}
}
dinic ( S, T ) ;
static std :: bitset < N > vis ;
vis.reset ( ) ;
for ( int i = 1 ; i <= n ; ++ i )
if ( ! vis.test ( i ) ) {
int curr = i ;
do {
vis.set ( curr ) ;
printf ( "%d ", curr ) ;
curr = to [curr] - 2500 ;
} while ( curr != -2500 ) ;
puts ( "" ) ;
}
}
} Lazer ;
int main ( ) {
int n ;
scanf ( "%d", & n ) ;
const int S = 5000, T = 5001 ;
for ( int i = 1 ; i ; ++ i ) {
for ( int j = 1 ; j < i ; ++ j )
if ( sqrt ( i + j ) == floor ( sqrt ( i + j ) ) ) {
Lazer.add_edge ( j, i + 2500, 1 ) ;
}
Lazer.add_edge ( S, i, 1 ) ;
Lazer.add_edge ( i + 2500, T, 1 ) ;
if ( i - Lazer.dinic ( S, T ) > n ) {
printf ( "%d\n", i - 1 ) ;
Lazer.display ( i - 1, S, T ) ;
break ;
}
}
return 0 ;
}