1
7-4 Chemical Equation (30 分)
# include <iostream>
# include <vector>
# include <string>
# include <algorithm>
# pragma warning ( disable: 4996 )
using namespace std;
int N, M, K, r;
vector< int > R, P, rea, path;
vector < vector< int > > react[ 110 ] ;
bool use[ 110 ] = { false } ;
bool dfs ( int i)
{
if ( i == M)
{
for ( int j = 0 ; j < M; j++ )
{
for ( int t = 0 ; t < react[ P[ j] ] [ path[ j] ] . size ( ) ; t++ )
{
if ( t != 0 ) printf ( " + " ) ;
printf ( "%02d" , react[ P[ j] ] [ path[ j] ] [ t] ) ;
}
printf ( " -> %02d\n" , P[ j] ) ;
}
return 1 ;
}
for ( int t = 0 ; t < react[ P[ i] ] . size ( ) ; t++ )
{
int j;
for ( j = 0 ; j < react[ P[ i] ] [ t] . size ( ) && use[ react[ P[ i] ] [ t] [ j] ] == 1 ; j++ ) ;
if ( j == react[ P[ i] ] [ t] . size ( ) )
{
for ( j = 0 ; j < react[ P[ i] ] [ t] . size ( ) ; j++ )
use[ react[ P[ i] ] [ t] [ j] ] = 0 ;
path. push_back ( t) ;
if ( dfs ( i + 1 ) )
return 1 ;
else {
for ( j = 0 ; j < react[ P[ i] ] [ t] . size ( ) ; j++ )
use[ react[ P[ i] ] [ t] [ j] ] = 1 ;
path. pop_back ( ) ;
}
}
}
return 0 ;
}
bool cmp ( vector< int > a, vector< int > b) {
for ( int i = 0 ; ; i++ ) {
if ( a[ i] != b[ i] )
return a[ i] < b[ i] ;
}
}
int main ( )
{
scanf ( "%d" , & N) ;
string s;
for ( int t = 0 ; t < N; t++ )
{
scanf ( "%d" , & r) ;
R. push_back ( r) ;
use[ r] = 1 ;
}
scanf ( "%d" , & M) ;
for ( int t = 0 ; t < M; t++ )
{
scanf ( "%d" , & r) ;
P. push_back ( r) ;
if ( find ( R. begin ( ) , R. end ( ) , r) != R. end ( ) )
{
rea. push_back ( r) ;
react[ r] . push_back ( rea) ;
rea. clear ( ) ;
}
}
scanf ( "%d\n" , & K) ;
while ( K-- )
{
getline ( cin, s) ;
int flag = 0 ;
sscanf ( s. c_str ( ) , "%d" , & r) ;
rea. push_back ( r) ;
s = s. substr ( 3 ) ;
while ( s[ 0 ] != '-' )
{
sscanf ( s. c_str ( ) , "+ %d" , & r) ;
s = s. substr ( 5 ) ;
rea. push_back ( r) ;
}
sscanf ( s. c_str ( ) , "-> %d" , & r) ;
react[ r] . push_back ( rea) ;
rea. clear ( ) ;
}
for ( int i = 0 ; i < M; i++ )
{
sort ( react[ P[ i] ] . begin ( ) , react[ P[ i] ] . end ( ) , cmp) ;
}
dfs ( 0 ) ;
return 0 ;
}
2
7-3 Postfix Expression (25 分)
#include <iostream>
#include <cstring>
#include <unordered_map>
using namespace std ;
const int N = 25 ;
int n;
string val[ N] ;
bool has_father[ N] ;
unordered_map< int , int > L, R;
string dfs ( int u)
{
string res;
if ( L. count ( u) )
res = dfs ( L[ u] ) + res;
if ( R. count ( u) )
res = res + dfs ( R[ u] ) ;
if ( ! L. count ( u) && R. count ( u) )
res = val[ u] + res;
else
res = res + val[ u] ;
return "(" + res + ")" ;
}
int main ( )
{
cin >> n;
for ( int i = 1 ; i <= n; i++ )
{
int lc, rc;
cin >> val[ i] >> lc >> rc;
if ( lc != - 1 ) L[ i] = lc, has_father[ lc] = true ;
if ( rc != - 1 ) R[ i] = rc, has_father[ rc] = true ;
}
int root = 1 ;
while ( has_father[ root] )
root++ ;
cout << dfs ( root) << endl;
return 0 ;
}
dfs+剪枝
7-1 Forever (20 分)
# include <cstdio>
# include <iostream>
# include <vector>
# include <cmath>
# include <algorithm>
using namespace std;
struct Node {
int A;
int n;
Node ( int _A, int _n)
{
A = _A;
n = _n;
}
} ;
int K, M;
vector< Node > result;
bool cmp ( const Node & a, const Node & b)
{
return a. n!= b. n? a. n< b. n: a. A< b. A;
}
int gcd ( int a, int b)
{
if ( b== 0 )
return a;
return gcd ( b, a% b) ;
}
bool isPrime ( int n)
{
if ( n<= 1 ) return false ;
for ( int i= 2 ; i* i<= n; i++ )
if ( n% i== 0 )
return false ;
return true ;
}
int getDigitSum ( int num)
{
int m = 0 ;
while ( num!= 0 )
{
m += num% 10 ;
num /= 10 ;
}
return m;
}
void DFS ( int depth, int m, int num)
{
if ( depth== K&& m== M)
{
int n = getDigitSum ( num+ 1 ) ;
int G = gcd ( m, n) ;
if ( G> 2 && isPrime ( G) )
{
result. push_back ( { num, n} ) ;
}
return ;
}
if ( m+ 9 * ( K- depth) < M|| depth> K)
return ;
for ( int i= 0 ; i<= 9 ; i++ )
{
DFS ( depth+ 1 , m+ i, num* 10 + i) ;
}
}
int main ( )
{
int N;
scanf ( "%d" , & N) ;
for ( int i= 0 ; i< N; ++ i)
{
scanf ( "%d %d" , & K, & M) ;
printf ( "Case %d\n" , i+ 1 ) ;
result. clear ( ) ;
DFS ( 0 , 0 , 0 ) ;
sort ( result. begin ( ) , result. end ( ) , cmp) ;
for ( auto & item : result)
{
printf ( "%d %d\n" , item. n, item. A) ;
}
if ( result. empty ( ) )
{
printf ( "No Solution\n" ) ;
}
}
return 0 ;
}
7-4 Heap Paths (30 分)
# include <iostream>
using namespace std;
const int MAX_N = 1010 ;
int heap[ MAX_N] ;
int path[ MAX_N] ;
int n;
bool isMinHeap, isMaxHeap;
void DFS ( int v, int step) {
path[ step] = heap[ v] ;
if ( v * 2 > n) {
for ( int i = 0 ; i <= step; i++ ) {
if ( i) {
if ( path[ i] > path[ i - 1 ] ) isMinHeap = true ;
else if ( path[ i] < path[ i - 1 ] ) isMaxHeap = true ;
printf ( " " ) ;
}
printf ( "%d" , path[ i] ) ;
}
cout << endl;
return ;
}
if ( v * 2 + 1 <= n) {
DFS ( v * 2 + 1 , step + 1 ) ;
}
if ( v * 2 <= n) {
DFS ( v * 2 , step + 1 ) ;
}
}
int main ( ) {
cin >> n;
for ( int i = 1 ; i <= n; i++ ) {
cin >> heap[ i] ;
}
DFS ( 1 , 0 ) ;
if ( isMaxHeap && isMinHeap) puts ( "Not Heap" ) ;
else if ( isMaxHeap) puts ( "Max Heap" ) ;
else puts ( "Min Heap" ) ;
return 0 ;
}
Eulerian Path
# include <iostream>
using namespace std;
const int maxn= 511 ;
int g[ maxn] [ maxn] ;
int d[ maxn] ;
int n, m;
bool st[ maxn] ;
int dfs ( int u)
{
st[ u] = true ;
int res= 1 ;
for ( int i= 1 ; i<= n; i++ )
if ( ! st[ i] && g[ u] [ i] )
res+= dfs ( i) ;
return res;
}
int main ( )
{
cin >> n >> m;
for ( int i= 0 ; i< m; i++ )
{
int a, b;
cin >> a >> b;
g[ a] [ b] = g[ b] [ a] = true ;
d[ a] ++ ;
d[ b] ++ ;
}
int cnt= dfs ( 1 ) ;
for ( int i= 1 ; i<= n; i++ )
{
if ( i!= 1 )
cout << " " ;
cout << d[ i] ;
}
cout << endl;
if ( cnt== n)
{
int s= 0 ;
for ( int i= 1 ; i<= n; i++ )
if ( d[ i] % 2 )
s++ ;
if ( s== 0 )
cout << "Eulerian" << endl;
else if ( s== 2 )
cout << "Semi-Eulerian" << endl;
else
cout << "Non-Eulerian" << endl;
}
else
cout << "Non-Eulerian" << endl;
return 0 ;
}
Complete Binary Tree
# include <iostream>
# include <cstring>
using namespace std;
const int maxn= 22 ;
int n;
int l[ maxn] , r[ maxn] ;
bool has_father[ maxn] ;
int maxk, maxid;
int dfs ( int u, int k)
{
if ( u== - 1 )
return 0 ;
if ( k> maxk)
{
maxk= k;
maxid= u;
}
dfs ( l[ u] , k* 2 ) ;
dfs ( r[ u] , k* 2 + 1 ) ;
}
int main ( )
{
cin >> n;
memset ( l, - 1 , sizeof l) ;
memset ( r, - 1 , sizeof r) ;
string a, b;
for ( int i= 0 ; i< n; i++ )
{
cin >> a >> b;
if ( a!= "-" )
l[ i] = stoi ( a) , has_father[ l[ i] ] = true ;
if ( b!= "-" )
r[ i] = stoi ( b) , has_father[ r[ i] ] = true ;
}
int root= 0 ;
while ( has_father[ root] )
root++ ;
dfs ( root, 1 ) ;
if ( maxk== n)
{
cout << "YES" << " " << maxid;
}
else
{
cout << "NO" << " " << root;
}
return 0 ;
}