Pagini recente » Cod sursa (job #3219983) | Cod sursa (job #2230976) | Cod sursa (job #2425997) | Cod sursa (job #2481344) | Cod sursa (job #2307218)
#include <fstream>
#include <algorithm>
#include <vector>
#define pb push_back
using namespace std ;
ifstream f ("ninjago.in") ;
ofstream g ("ninjago.out") ;
/*
greseala pentru care mi-au picat 5 teste pe primul subpunct este
ca am identificat cate noduri are subarborele maximal cu aceasta proprietate
fara sa oblig ca nodul 1 sa fie in el
#taragula
*/
const int NR = 31205 ;
int n , m ;
int TT [ NR ] ;
int GR [ NR ] ;
int nrsclavi [ NR ] ;
int best_group = 1 , maxim = 1 ;
bool sw ;
struct muchie
{
int x , y , nre ;
long long cost = 0 ;
}v [ NR ];
struct cmp
{
bool operator ()( const muchie &a , const muchie &b ) const
{
if ( a.nre == b.nre )
return a.cost < b.cost ;
return a.nre < b.nre ;
}
};
int find_root ( int x )
{
int root ;
for ( root = x ; x != TT [ x ] ; x = TT [ x ] , root = x ) ;
for ( ; x != TT [ x ] ; )
{
int y = TT [ x ] ;
TT [ x ] = root ;
x = y ;
}
return root ;
}
void unite ( int y , int x )
{
if ( GR [ x ] > GR [ y ] ) TT [ y ] = x , nrsclavi [ x ] += nrsclavi[ y ];
else TT [ x ] = y , nrsclavi [ y ] += nrsclavi[ x ];
if ( GR [ x ] == GR [ y ] ) GR [ y ] ++ ;
}
int main ()
{
int type ; f >> type ;
f >> n >> m ;
for ( int i = 1 ; i <= m ; ++ i )
{
f >> v [ i ].x >> v [ i ].y ;
char c ;
for ( int j = 1 ; j <= 125 ; j *=5 )
{
f >> c ;
if ( c == 'E' ) v [ i ].nre ++ ;
else v [ i ].cost += j * unsigned ( c - 'A' + 1 ) ;
}
}
for ( int i = 1 ; i <= n ; ++ i ) GR [ i ] = 1 , TT [ i ] = i , nrsclavi [ i ] = 1 ;
sort ( v + 1 , v + m + 1 , cmp() ) ;
long long cnt = 0 , CNT = 0 , s = 0 ;
int nrmuchii = 0 ;
for ( int i = 1 ; nrmuchii < n - 1 ; ++ i )
{
int a = find_root( v [ i ].x ) , b = find_root( v [ i ].y ) ;
if ( a != b )
{
s += v [ i ].cost ;
if ( !sw && v [ i ].nre ) { sw = true ; maxim = nrsclavi[ find_root( 1 ) ] ; }
unite( a , b ) ;
cnt += v [ i ].nre ;
if ( v[ i ].nre ) CNT ++ ;
nrmuchii ++ ;
}
}
if ( type == 1 ) { g << maxim ; return 0 ; }
if ( type == 2 ) { g << CNT << "\n" << cnt << "\n" ; return 0 ; }
if ( type == 3 ) { g << s << "\n" ; return 0 ;}
}