Submission #365350

# Submission time Handle Problem Language Result Execution time Memory
365350 2021-02-11T13:57:48 Z Atill83 Mag (COCI16_mag) C++14
48 / 120
467 ms 172004 KB
#include <bits/stdc++.h>
#define ff first
#define ss second
#define endl '\n'
using namespace std;
const long long INF = (long long) 1e18;
const int mod = (int) 1e9+7;
const int MAXN = (int) 1e6+5;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll n;
vector<int> adj[MAXN];
ll mg[MAXN];
bool vis[MAXN];
int dep[MAXN];
int dp[MAXN][2];
int vans1 = 1, vans0 = 1;

void dfs(int v){
    if(mg[v] == 2){
        dp[v][0] = -mod;
        dp[v][1] = 1;
    }else{
        dp[v][0] = 1;
    }


    vis[v] = 1;
    for(int i: adj[v]){
        if(!vis[i]){
            dfs(i);
            if(mg[v] == 2){
                dp[v][1] = max(dp[v][1], dp[i][0] + 1);
            }else{
                dp[v][1] = max(dp[v][1], dp[i][1] + 1);
                dp[v][0] = max(dp[v][0], dp[i][0] + 1);
            }
        }
    }
    vans1 = max(vans1, dp[v][1]);
    vans0 = max(vans0, dp[v][0]);
}

pii ed[MAXN];

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);cout.tie(nullptr);

    #ifdef Local
        freopen("C:/Users/Admin/Desktop/Yazilim/C/IO/int.txt","r",stdin);
        freopen("C:/Users/Admin/Desktop/Yazilim/C/IO/out.txt","w",stdout);
    #endif

    cin>>n;

    for(int i = 0; i < n - 1; i++){
        int a, b;
        cin>>a>>b;
        ed[i] = {a, b};
    }
    

    bool var = 0;



    for(int i = 1; i <= n; i++){
        cin>>mg[i];
        var = (var || mg[i] == 1);
    }

    if(!var){
        sort(mg + 1, mg + n + 1);
        cout<<mg[1]<<"/1"<<endl;
        return 0;
    }

    for(int i = 0; i < n - 1; i++){
        if(max(mg[ed[i].ff], mg[ed[i].ss]) > 2)
            continue;
        adj[ed[i].ff].push_back(ed[i].ss);
        adj[ed[i].ss].push_back(ed[i].ff);
    }


    for(int i = 1; i <= n; i++)
        if(!vis[i])
            dfs(i);

    int a, b;

    if(vans1 > 2*vans0){
        a = 2;
        b = vans1;
    }else{
        a = 1;
        b = vans0;
    }


    int d = __gcd(a, b);
    a /= d;
    b /= d;


    cout<<a<<"/"<<b<<endl;


    #ifdef Local
        cout<<endl<<fixed<<setprecision(2)<<1000.0 * clock() / CLOCKS_PER_SEC<< " milliseconds ";
    #endif
}
# Verdict Execution time Memory Grader output
1 Correct 17 ms 23916 KB Output is correct
2 Correct 19 ms 23916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 23916 KB Output is correct
2 Correct 17 ms 24044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 350 ms 113728 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 23788 KB Output is correct
2 Correct 447 ms 172004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 467 ms 171656 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 395 ms 94700 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 376 ms 97628 KB Output is correct
2 Incorrect 76 ms 31084 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 81 ms 30956 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 353 ms 91364 KB Output is correct
2 Incorrect 399 ms 95596 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Correct 411 ms 95504 KB Output is correct
2 Incorrect 345 ms 60012 KB Output isn't correct