[NOI2009] Transformation sequence (Hungarian maximum match)

description

solution

I even glanced at the question! ! TT
Insert picture description here
after transformationT array is[0, n) [0, n)[0,n ) permutation, there are also conversion rules, distanceDDD also knows that
it is obvious thatiican be foundThe possible transformation objects of i .
Isn't this a maximum match? ?
No solution means that the number of matches cannot reachnnn Bale
minimum matching lexicographic it from the back, so that the front of the latter so can make a better match

code

#include <cstdio>
#include <vector>
#include <cstring>
#include <iostream>
using namespace std;
#define maxn 10005
vector < int > G[maxn];
int n;
bool vis[maxn];
int match[maxn], link[maxn];

bool find( int u ) {
    
    
	for( int i = 0;i < G[i].size();i ++ ) {
    
    
		int v = G[u][i];
		if( vis[v] ) continue;
		vis[v] = 1;
		if( ! match[v] || find( match[v] ) ) {
    
    
			match[v] = u;
			link[u] = v;
			return 1;
		}
	}
	return 0;
}

int main() {
    
    
	scanf( "%d", &n );
	for( int i = 0, d;i < n;i ++ ) {
    
    
		scanf( "%d", &d );
		int x = ( i + d ) % n;
		int y = ( i - d + n ) % n;
		G[i].push_back( min( x, y ) );
		G[i].push_back( max( x, y ) );
	}
	int ans = 0;
	for( int i = n - 1;~ i;i -- ) {
    
    
		memset( vis, 0, sizeof( vis ) );
		if( find( i ) ) ans ++;
	}
	if( ans != n ) return ! printf( "No Answer\n" );
	for( int i = 0;i < n;i ++ )
		printf( "%d ", link[i] );
	return 0;
}

Guess you like

Origin blog.csdn.net/Emm_Titan/article/details/113702999