Submission #167648

# Submission time Handle Problem Language Result Execution time Memory
167648 2019-12-09T08:08:29 Z Flying_dragon_02 Mag (COCI16_mag) C++14
120 / 120
543 ms 130720 KB
#include <bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define pb push_back
#define mp make_pair

typedef pair< int, int > ii;

const int N = 1e6 + 5;

const long long infll = (long long)(1e18);

int n, f[N][2], x[N], pref[2];

vector< int > graph[N];

bool vis[N];

double mn;

long long X, Y;

double F(long long a, long long b) {
    if( b == 0 ) return infll;
    else return (double)( (double)( a ) / (double)( b ) );
}

void dfs(int u, int p) {
    f[u][ x[u] - 1 ] = 1;
    vis[u] = 1;
    for(int i = 0; i < (int)( graph[u].size() ); i++ ) {
        int v = graph[u][i];
        if( v == p || x[v] > 2 ) continue;
        dfs( v, u );
        if( x[u] == 1 ) {
            f[u][0] = max( f[u][0], f[v][0] + 1 );
            f[u][1] = max( f[u][1], f[v][1] + 1 );
        }
        else
            f[u][1] = max( f[u][1], f[v][0] + 1 );
    }
    pref[0] = pref[1] = 0;
    for(int i = 0; i < (int)( graph[u].size() ); i++ ) {
        int v = graph[u][i];
        if( v == p || x[v] > 2 ) continue;
        if( x[u] == 1 ) {
            if( f[v][0] && F( 1, f[v][0] + 1 + pref[0] ) < mn ) {
                X = 1;
                mn = F( 1, f[v][0] + 1 + pref[0] );
                Y = f[v][0] + 1 + pref[0];
            }
            if( f[v][1] && F( 2, f[v][1] + 1 + pref[0] ) < mn ) {
                X = 2;
                mn = F( 2, f[v][1] + 1 + pref[0] );
                Y = f[v][1] + 1 + pref[0];
            }
            if( f[v][0] && F( 2, f[v][0] + 1 + pref[1] ) < mn ) {
                X = 2;
                mn = F( 2, f[v][0] + 1 + pref[1] );
                Y = f[v][0] + 1 + pref[1];
            }
        }
        else {
            if( f[v][0] && F( 2, f[v][0] + 1 + pref[0] ) < mn ) {
                X = 2;
                mn = F( 2, f[v][0] + 1 + pref[0] );
                Y = f[v][0] + 1 + pref[0];
            }
        }
        for(int j = 0; j < 2; j++)
            pref[j] = max( pref[j], f[v][j] );
    }
}

int main() {
    cin.tie(0), ios_base::sync_with_stdio(0);
    cin >> n;
    for(int i = 1; i <= n - 1; i++) {
        int u, v;
        cin >> u >> v;
        graph[u].pb(v);
        graph[v].pb(u);
    }
    mn = infll;
    for(int i = 1; i <= n; i++) {
        cin >> x[i];
        if(x[i] < mn) {
            mn = x[i];
            X = x[i];
            Y = 1;
        }
    }
    for(int i = 1; i <= n; i++) {
        if(!vis[i] && x[i] < 3) {
            dfs( i, i );
        }
    }
    for(int i = 1; i <= n; i++) {
        if(!vis[i]) continue;
        if( F( 1, f[i][0] ) < mn ) {
            X = 1;
            mn = F( 1, f[i][0] );
            Y = f[i][0];
        }
        if( F( 2, f[i][1] ) < mn ) {
            X = 2;
            mn = F( 2, f[i][1] );
            Y = f[i][1];
        }
    }
    cout << X << "/" << Y;
}
# Verdict Execution time Memory Grader output
1 Correct 24 ms 23928 KB Output is correct
2 Correct 23 ms 23928 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 23928 KB Output is correct
2 Correct 24 ms 23928 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 420 ms 84088 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 23800 KB Output is correct
2 Correct 533 ms 130140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 527 ms 127992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 473 ms 67704 KB Output is correct
2 Correct 351 ms 56600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 454 ms 68708 KB Output is correct
2 Correct 89 ms 30072 KB Output is correct
3 Correct 543 ms 130720 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 116 ms 28280 KB Output is correct
2 Correct 476 ms 68072 KB Output is correct
3 Correct 379 ms 46644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 428 ms 64912 KB Output is correct
2 Correct 468 ms 67028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 483 ms 68152 KB Output is correct
2 Correct 385 ms 46456 KB Output is correct