急求浙大acm程序!2913和2914题!
来源:学生作业帮 编辑:神马作文网作业帮 分类:综合作业 时间:2024/11/19 09:35:07
急求浙大acm程序!2913和2914题!
求浙大acm程序!
这两个程序!
求浙大acm程序!
这两个程序!
#include
#include
#define N 10000
#define M ( N * 10 )
using namespace std;
struct line {
int num;
struct line* next;
};
struct line* zone[ N ];
struct line get[ M ];
struct mark {
int sign, depth;
} visit[ N ];
int require[ N ];
int depth[ N ];
map< int, int > ref;
map< int, int > cmp;
void bfs ( int s ) {
int queue[ N ], head = 0, tail = 1, i, x;
struct line* p;
queue[ 0 ] = s;
visit[ s ].sign = 1;
visit[ s ].depth = 1;
while ( head != tail ) {
x = queue[ head ];
head = ( head + 1 ) % N;
for ( p = zone[ x ] ; p ; p = p->next ) {
if ( visit[ i = p->num ].sign == 0 ) {
visit[ i ].sign = 1;
visit[ i ].depth = visit[ x ].depth + 1;
queue[ tail ] = i;
tail = ( tail + 1 ) % N;
}
}
}
return;
}
int main () {
int t, i, j, s, n, m, z;
int num, turn, city, top, ans, max;
struct line* p;
scanf ( "%d", &t );
while ( t-- ) {
scanf ( "%d%d", &n, &m );
ref.clear();
cmp.clear();
top = -1;
memset ( zone, 0, sizeof ( zone ) );
memset ( require, 0, sizeof ( require ) );
memset ( depth, 0, sizeof ( depth ) );
for ( i = turn = 0 ; i < n ; ++i ) {
scanf ( "%d%d", &num, &s );
if ( cmp[ num ] == 0 ) {
cmp[ num ] = 1;
ref[ num ] = turn++;
}
city = ref[ num ];
for ( j = 0 ; j < s ; ++j ) {
scanf ( "%d", &z );
if ( cmp[ z ] == 0 ) {
cmp[ z ] = 1;
ref[ z ] = turn++;
}
p = &get[ ++top ];
p->num = ref[ z ];
p->next = zone[ city ];
zone[ city ] = p;
}
}
top = -1;
for ( i = 0 ; i < m ; ++i ) {
scanf ( "%d", &s );
for ( j = 0 ; j < s ; ++j ) {
scanf ( "%d", &num );
require[ ++top ] = ref[ num ];
}
}
for ( i = 0 ; i ::iterator k = ref.begin() ; k != ref.end() ; ++k ) {
if ( depth[ k->second ] < max ) {
max = depth[ k->second ];
ans = k->first;
}
}
printf ( "%d %d\n", max, ans );
}
return 0;
}
先给你个2913 2914貌似是个数论 一下子想不出来 ==研究研究
要解释的话再回个吧 吃饭去了
#include
#include
#define N 1010
#define eps 1e-5
int data[ N ];
int gcd ( int a, int b ) {
if ( b == 0 ) return a;
else return gcd ( b, a % b );
}
int main () {
int i, n, temp, ans, t;
double money;
scanf ( "%d", &t );
while ( t-- ) {
scanf ( "%lf", &money );
scanf ( "%d", &n );
for ( i = 0 ; i < n ; ++i ) {
scanf ( "%d", &data[ i ] );
}
money *= 4;
if ( fabs ( (int)money - money ) > eps ) {
printf ( "no\n" );
continue;
}
ans = (int)( money + eps );
temp = data[ 0 ];
for ( i = 1 ; i < n ; ++i ) {
temp = gcd ( temp, data[ i ] );
if ( temp == 1 ) break;
}
if ( temp == 1 ) {
printf ( "yes\n" );
}
else {
while ( !( temp % 2 ) ) {
temp /= 2;
}
if ( ans % temp == 0 ) printf ( "yes\n" );
else printf ( "no\n" );
}
}
return 0;
}
终于过了 不过是猜的.不知道对不对的
公约数等于 m的话 能凑的数必定是m的倍数 不过后面的那个不断除以2是我试出来的 不知道对不对 数论不是很懂啊...
#include
#define N 10000
#define M ( N * 10 )
using namespace std;
struct line {
int num;
struct line* next;
};
struct line* zone[ N ];
struct line get[ M ];
struct mark {
int sign, depth;
} visit[ N ];
int require[ N ];
int depth[ N ];
map< int, int > ref;
map< int, int > cmp;
void bfs ( int s ) {
int queue[ N ], head = 0, tail = 1, i, x;
struct line* p;
queue[ 0 ] = s;
visit[ s ].sign = 1;
visit[ s ].depth = 1;
while ( head != tail ) {
x = queue[ head ];
head = ( head + 1 ) % N;
for ( p = zone[ x ] ; p ; p = p->next ) {
if ( visit[ i = p->num ].sign == 0 ) {
visit[ i ].sign = 1;
visit[ i ].depth = visit[ x ].depth + 1;
queue[ tail ] = i;
tail = ( tail + 1 ) % N;
}
}
}
return;
}
int main () {
int t, i, j, s, n, m, z;
int num, turn, city, top, ans, max;
struct line* p;
scanf ( "%d", &t );
while ( t-- ) {
scanf ( "%d%d", &n, &m );
ref.clear();
cmp.clear();
top = -1;
memset ( zone, 0, sizeof ( zone ) );
memset ( require, 0, sizeof ( require ) );
memset ( depth, 0, sizeof ( depth ) );
for ( i = turn = 0 ; i < n ; ++i ) {
scanf ( "%d%d", &num, &s );
if ( cmp[ num ] == 0 ) {
cmp[ num ] = 1;
ref[ num ] = turn++;
}
city = ref[ num ];
for ( j = 0 ; j < s ; ++j ) {
scanf ( "%d", &z );
if ( cmp[ z ] == 0 ) {
cmp[ z ] = 1;
ref[ z ] = turn++;
}
p = &get[ ++top ];
p->num = ref[ z ];
p->next = zone[ city ];
zone[ city ] = p;
}
}
top = -1;
for ( i = 0 ; i < m ; ++i ) {
scanf ( "%d", &s );
for ( j = 0 ; j < s ; ++j ) {
scanf ( "%d", &num );
require[ ++top ] = ref[ num ];
}
}
for ( i = 0 ; i ::iterator k = ref.begin() ; k != ref.end() ; ++k ) {
if ( depth[ k->second ] < max ) {
max = depth[ k->second ];
ans = k->first;
}
}
printf ( "%d %d\n", max, ans );
}
return 0;
}
先给你个2913 2914貌似是个数论 一下子想不出来 ==研究研究
要解释的话再回个吧 吃饭去了
#include
#include
#define N 1010
#define eps 1e-5
int data[ N ];
int gcd ( int a, int b ) {
if ( b == 0 ) return a;
else return gcd ( b, a % b );
}
int main () {
int i, n, temp, ans, t;
double money;
scanf ( "%d", &t );
while ( t-- ) {
scanf ( "%lf", &money );
scanf ( "%d", &n );
for ( i = 0 ; i < n ; ++i ) {
scanf ( "%d", &data[ i ] );
}
money *= 4;
if ( fabs ( (int)money - money ) > eps ) {
printf ( "no\n" );
continue;
}
ans = (int)( money + eps );
temp = data[ 0 ];
for ( i = 1 ; i < n ; ++i ) {
temp = gcd ( temp, data[ i ] );
if ( temp == 1 ) break;
}
if ( temp == 1 ) {
printf ( "yes\n" );
}
else {
while ( !( temp % 2 ) ) {
temp /= 2;
}
if ( ans % temp == 0 ) printf ( "yes\n" );
else printf ( "no\n" );
}
}
return 0;
}
终于过了 不过是猜的.不知道对不对的
公约数等于 m的话 能凑的数必定是m的倍数 不过后面的那个不断除以2是我试出来的 不知道对不对 数论不是很懂啊...