ECUSTOJ
Posted shadowland
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ECUSTOJ相关的知识,希望对你有一定的参考价值。
1
#include <bits/stdc++.h> using namespace std ; struct bigInt { int num[ 2000 ] ; int size ; static const int maxN = 2000 ; private : void Init ( ) { size = 0 ; for ( int i=1 ; i<=maxN ; ++i ) num[ i ] = 0 ; } public : void toInt ( char *ch ) { Init ( ) ; size = strlen ( ch ) ; for ( int i=0 ; i<size ; ++i ) { num[ i ] = ch[ size - i - 1 ] - ‘0‘ ; } } public : void Plus ( bigInt o ) { bool up = false ; size = max ( size , o.size ) ; for ( int i=0 ; i<size ; ++i ) { num[ i ] += o.num[ i ] + up ; up = false ; if ( num[ i ] >= 10 ) { num[ i ] -= 10 ; up = true ; } } if ( up ) num[ size++ ] = 1; } public : void print ( ) { for ( int i=size-1 ; i>=0 ; --i ) { printf ( "%d" , num[ i ] ) ; } putchar ( ‘ ‘ ) ; } } A , B; char str1[ 100 ] , str2[ 100 ] ; int main ( ) { while ( scanf ( "%s%s" , str1 , str2 ) != EOF ) { A.toInt (str1) ; B.toInt (str2) ; A.Plus ( B ) ; A.print ( ) ; } return 0 ; }
3
#include <bits/stdc++.h> using namespace std ; const int maxN = 10010 ; const int maxM = 100010 ; struct Edge { int to,from,date; }e[ maxM ] ; int father[ maxM ] ; bool crash[ maxM ] ; bool cmp ( Edge a , Edge b ) { return a.date > b.date ; } int getfa ( int x ) { return father[ x ] == x ? x : father[ x ] = getfa ( father[ x ] ) ; } inline int INPUT ( ) { int x=0,f=1;char ch=getchar(); while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();} while(ch>=‘0‘&&ch<=‘9‘){x=(x<<1)+(x<<3)+ch-‘0‘;ch=getchar();} return x*f; } void Init ( const int N , const int M ) { for ( int i=1 ; i<=N ; ++i ) { father[ i ] = i ; } sort( e + 1 , e + M + 1 , cmp ) ; } void Debug ( int n ) { for ( int i=1 ; i<=n ; ++i ) { cout << e[ i ].date << endl ; } } int main ( ) { int N = INPUT ( ) , M = INPUT ( ) ; memset ( crash , false , sizeof ( crash ) ) ; for ( int i=1 ; i<=M ; ++i ) { e[ i ].to = INPUT ( ) ; e[ i ].from = INPUT ( ) ; e[ i ].date = INPUT ( ) ; } Init ( N , M ) ; //Debug(M); for ( int i=1 ; i<=M ; ++i ) { int px = getfa( e[ i ].to ) ; int py = getfa( e[ i ].from ) ; if ( px != py ) { father[ px ] = py ; crash[ e[ i ].date ] = true ; } } int _cnt = 0 ; for ( int i=1 ; i<=maxM ; i++ ) { if ( crash[ i ] ) _cnt++ ; } printf ( "%d " , _cnt ) ; return 0 ; }
4
#include <bits/stdc++.h> using namespace std ; typedef long long ll ; ll k ; ll QP ( ll a , ll b ) { ll ret = 1 , base = a ; while( b ) { if ( b & 1 ) ret = ret * base % k ; base = base * base % k ; b >>= 1 ; } return ret ; } int main ( ) { ll b , n ; cin >> b >> n >> k ; cout << QP ( b , n ) % k << endl ; return 0 ; }
14
//#include <bits/stdc++.h> #include <cstdio> int INPUT ( ) { int x=0,f=1;char ch=getchar(); while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();} while(ch<=‘9‘&&ch>=‘0‘){x=(x<<1)+(x<<3)+ch-‘0‘;ch=getchar();} return x*f; } int main ( ) { int N = INPUT ( ) ; int cnt = 1 , tmp = -1 ; for ( int i=1 ; i<=N ; ++i ) { int aa = INPUT ( ) ; if ( aa == tmp ) ++cnt ; else --cnt ; if ( !cnt ) { cnt = 1 ; tmp = aa ; } } printf ( "%d " , tmp ) ; return 0 ; }
29
#include <bits/stdc++.h> using namespace std ; const int maxN = 60010; const int inf = 2147483647 ; struct Tree { int l , r , mx , sum ; }; struct Edge { int to , next ; }; Tree tr[ maxN << 2 ] ; Edge e[ maxN << 1 ] ; int head[ maxN ], start[ maxN ] , DFN[ maxN ] , pre[ maxN ] , dep[ maxN ] , size[ maxN ] , val[ maxN ] , Rank[ maxN ]; inline int INPUT ( ) { int x = 0 , f = 1 ; char ch = getchar ( ) ; while ( ch < ‘0‘ || ch > ‘9‘ ) {if ( ch == ‘-‘ ) f = - 1 ; ch = getchar ( ) ; } while ( ch >=‘0‘ && ch <=‘9‘ ) { x = ( x << 1 ) + ( x << 3 ) + ch - ‘0‘ ; ch = getchar ( ) ; } return x * f ; } int _cnt , dfs_num ; inline void addEdge ( const int x , const int y ) { e[ ++_cnt ].to = y ; e[ _cnt ].next = head[ x ] ; head[ x ] = _cnt ; } void initDfs ( const int x ) { size[ x ] = 1 ; for ( int i=head[ x ] ; i ; i = e[ i ].next ) { if ( e[ i ].to != pre[ x ] ) { dep[ e[ i ].to ] = dep[ x ] + 1 ; pre[ e[ i ].to ] = x ; initDfs ( e[ i ].to ) ; size[ x ] += size[ e[ i ].to ] ; } } } void Dfs ( const int x , const int chainStart ) { DFN[ x ] = ++dfs_num ; Rank[ dfs_num ] = x ; start[ x ] = chainStart ; int heavy = 0 ; for ( int i=head[ x ] ; i ; i=e[ i ].next ) { if ( e[ i ].to != pre[ x ] && size[ e[ i ].to ] > size[ heavy ] ) { heavy = e[ i ].to ; } } if ( heavy ) Dfs ( heavy , chainStart ) ; for ( int i=head[ x ] ; i ; i=e[ i ].next ) { if ( e[ i ].to != heavy && e[ i ].to != pre[ x ] ) Dfs ( e[ i ].to , e[ i ].to ) ; } } void buildTree ( int ll ,int rr ,int i ){ tr[ i ].l = ll; tr[ i ].r = rr; if ( ll == rr ) { tr[ i ].sum = val[ Rank[ ll ] ] ; tr[ i ].mx = val[ Rank[ ll ] ] ; } else { int mid = ( ll + rr ) >> 1 ; buildTree ( ll , mid , i << 1 ) ; buildTree ( mid + 1 , rr , i << 1 | 1 ) ; tr[ i ].mx = max ( tr[ i << 1 ].mx , tr[ i << 1 | 1 ].mx ) ; tr[ i ].sum = tr[ i << 1 ].sum + tr[ i << 1 | 1 ].sum ; } } void updateTree ( int target , int val_ , int i ) { if ( tr[ i ].l == tr[ i ].r ) { tr[ i ].mx = val_ ; tr[ i ].sum = val_ ; }else { int mid = ( tr[ i ].l + tr[ i ].r ) >> 1 ; if ( target <= mid ) updateTree ( target , val_ , i << 1 ) ; else updateTree ( target , val_, i << 1 | 1 ) ; tr[ i ].mx = max ( tr[ i << 1 ].mx , tr[ i << 1 | 1 ].mx ) ; tr[ i ].sum = tr[ i << 1 ].sum + tr[ i << 1 | 1 ].sum ; } } int querySum ( const int q , const int w , const int i ) { if ( q <= tr[i].l && w >= tr[i].r ) return tr[i].sum; else { int mid = ( tr[ i ].l + tr[ i ].r ) >> 1 ; if ( q > mid ) return querySum ( q , w , i << 1 | 1) ; else if ( w <= mid ) return querySum ( q , w , i << 1 ) ; else return querySum ( q , w , i << 1 | 1 ) + querySum ( q , w , i << 1 ) ; } } int queryMax ( const int q , const int w , const int i ) { if ( q <= tr[i].l && w >= tr[i].r ) return tr[i].mx; else { int mid = ( tr[ i ].l + tr[ i ].r ) >> 1 ; if ( q > mid ) return queryMax ( q , w , i << 1 | 1) ; else if ( w <= mid ) return queryMax ( q , w , i << 1 ) ; else return max ( queryMax ( q , w , i << 1 | 1 ) , queryMax ( q , w , i << 1 ) ) ; } } int solveSum ( int x , int y ) { int ret = 0 ; while ( start[ x ] != start[ y ] ) { if ( dep[ start[ x ] ] < dep[ start[ y ]] ) swap( x , y ) ; ret += querySum ( DFN[ start[ x ]] , DFN[ x ] , 1 ) ; x = pre[ start[ x ] ] ; } if ( DFN[ x ] > DFN[ y ]) swap( x, y ) ; ret += querySum ( DFN[ x ] , DFN[ y ] , 1 ) ; return ret ; } int solveMax ( int x , int y ) { int ret = -inf ; while ( start[ x ] != start[ y ] ) { if ( dep[ start[ x ] ] < dep[ start[ y ] ] ) swap ( x , y ) ; ret = max ( ret ,queryMax ( DFN[ start[ x ] ] , DFN[ x ] , 1 ) ) ; x = pre[ start[ x ] ] ; } if ( DFN[ x ] > DFN[ y ] ) swap ( x , y ) ; ret = max ( ret , queryMax ( DFN[ x ] , DFN[ y ] , 1 ) ) ; return ret ; } int main ( ) { int N = INPUT ( ) ; for ( int i=1 ; i<N ; ++i ) { int x = INPUT ( ) , y = INPUT ( ) ; addEdge ( x , y ) ; addEdge ( y , x ) ; } for ( int i=1 ; i<=N ; ++i ) val[ i ] = INPUT ( ) ; initDfs ( 1 ) ; Dfs( 1 , 1 ) ; buildTree ( 1 , N , 1 ) ; int Q = INPUT ( ) ; char op[ 10 ] ; for ( int i=1 ; i<=Q ; ++i ) { scanf ( "%s" , op ) ; int x = INPUT ( ) , y = INPUT ( ) ; if ( op[ 1 ] == ‘H‘ ) { updateTree ( DFN[ x ] , y , 1 ) ; }else if ( op[ 1 ] == ‘S‘ ) { printf ( "%d " , solveSum ( x , y ) ) ; }else { printf ( "%d " , solveMax ( x , y ) ) ; } } return 0 ; }
31
#include <bits/stdc++.h> using namespace std ; const int maxN = 100010 ; const int maxM = 1000010 ; const int inf = 2147483647 ; struct Edge { int to , next , val ; }; Edge e[ maxM << 1 ] ; int head[ maxN ] , dis[ maxN ] ; bool inQ[ maxN ] ; int _cnt ; void addEdge ( const int x , const int y , const int val_ ) { e[ ++_cnt ].to = y ; e[ _cnt ].val = val_ ; e[ _cnt ].next = head[ x ] ; head[ x ] = _cnt ; } inline int INPUT ( ) { int x=0,f=1;char ch=getchar(); while(ch<‘0‘||ch>‘9‘){if(x==‘-‘)f=-1;ch=getchar();} while(ch>=‘0‘&&ch<=‘9‘){x=(x<<1)+(x<<3)+ch-‘0‘;ch=getchar();} return x*f; } void Spfa ( const int S ) { memset ( dis , 0x3f3f3f , sizeof ( dis ) ) ; memset ( inQ , false , sizeof ( inQ ) ); queue<int> Q ; Q.push( S ) ; inQ[ S ] = true ; dis[ S ] = 1 ; int p ; while ( !Q.empty ( ) ) { p = Q.front ( ) ; inQ[ p ] = false ; Q.pop ( ) ; for ( int i = head[ p ] ; i ; i = e[ i ].next ) { int to_ = e[ i ].to ; if ( dis[ to_ ] > dis[ p ] + e[ i ].val ) { dis[ to_ ] = dis[ p ] + e[ i ].val ; if ( !inQ[ to_ ] ) { Q.push ( to_ ) ; inQ[ to_ ] = true ; } } } } } void Debug ( int n ) { for ( int i=1 ; i<=n ; ++i ) { cout << dis[ i ] << ‘ ‘ ; } cout << endl ; } int main ( ) { int N = INPUT ( ) , M = INPUT ( ) , S = INPUT ( ) , m = INPUT ( ) ; for ( int i=1 ; i<=M ; ++i ) { int x = INPUT ( ) , y = INPUT ( ) ; addEdge( x , y , 1 ) ; addEdge( y , x , 1 ) ; } Spfa ( S ) ; //Debug ( N ) ; int ans = -inf ; for ( int i=1 ; i<=N ; ++i ) { ans = max ( ans , dis[ i ] ) ; } printf ( "%d " , ans + m ) ; return 0; }
34
#include <bits/stdc++.h> using namespace std ; const int maxN = 1000010 ; struct Edge { int to , next ; }; Edge e[ maxN << 1 ] ; int head[ maxN ] , size[ maxN ] , hv[ maxN ] ; int INPUT ( ) { int x=0,f=1;char ch=getchar(); while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();} while(ch<=‘9‘&&ch>=‘0‘){x=(x<<1)+(x<<3)+ch-‘0‘;ch=getchar();} return x*f; } int _cnt ; void addEdge ( int x , int y ) { e[ ++_cnt ].to = y ; e[ _cnt ].next = head[ x ] ; head[ x ] = _cnt ; } void Dfs ( const int x , const int fa ) { size[ x ] = 1 ; for ( int i=head[ x ] ; i ; i=e[ i ].next ) { if ( e[ i ].to != fa ) { Dfs ( e[ i ].to , x ) ; size[ x ] += size[ e[ i ].to ] ; } } int heavySon = 0 ; for ( int i=head[ x ] ; i ; i=e[ i ].next ) { if ( e[ i ].to != fa && size[ e[ i ].to ] > size[ heavySon ] ) { heavySon = e[ i ].to ; } } if ( heavySon ) hv[ x ] = heavySon ; } bool judge ( const int x , int n ) { if ( size[ hv[ x ] ] > ( int )( n / 2 ) ) return false ; if ( ( n - size[ x ] ) > ( int )( n / 2 ) ) return false ; return true ; } int main ( ) { int N = INPUT ( ) ; for ( int i=1 ; i<N ; ++i ) { int x = INPUT ( ) , y = INPUT ( ) ; addEdge ( x , y ) ; addEdge ( y , x ) ; } Dfs ( 1 , 1 ) ; for ( int i=1 ; i<=N ; ++i ) { if ( judge ( i , N ) ) { printf ( "%d " , i ) ; } } return 0 ; }
以上是关于ECUSTOJ的主要内容,如果未能解决你的问题,请参考以下文章