Submission #701612

# Submission time Handle Problem Language Result Execution time Memory
701612 2023-02-21T15:19:29 Z Do_you_copy Ceste (COCI17_ceste) C++17
0 / 160
189 ms 131072 KB
#include <bits/stdc++.h>
#define fi first
#define se second
#define faster ios_base::sync_with_stdio(0); cin.tie(0);
#define pb push_back

using namespace std;
using ll = long long;
using pii = pair <int, int>;

const int maxN = 1e5 + 1;
const int inf = 0x3f3f3f3f;
//const int Mod =
int n;
int a[maxN];
vector <int> adj[maxN];
int dp[maxN][2];
struct TFrac{
    int x, y;
    bool operator < (const TFrac other) const{
        //x / y < other.x / other.y;
        //x * other.y < other.x * y;
        return x * other.y < other.x * y;
    }
};
TFrac ans = {1, 1};
void update(pair <pii, pii> &x, pii y){
    if (y > x.fi){
        swap(x.fi, x.se);
        x.fi = y;
    }
    else x.se = max(x.se, y);
}

void dfs(int u, int p){
    for (int i: adj[u]){
        if (i == p) continue;
        dfs(i, u);
    }
    if (a[u] > 2) return;
    if (a[u] == 1){
        pair <pii, pii> max1 = {{0, 0}, {0, 0}};
        pair <pii, pii> max2 = {{0, 0}, {0, 0}};
        for (int i: adj[u]){
            if (i == p) continue;
            update(max1, {dp[i][0], i});
            update(max2, {dp[i][1], i});
        }
        TFrac tem1 = {1, max1.fi.fi + max1.se.fi + 1};
        TFrac tem2;
        if (max1.fi.se == max2.fi.se){
            tem2.x = 2;
            tem2.y = 1 + max(max1.fi.fi + max2.se.fi, max1.se.fi + max2.fi.fi);
        }
        else{
            tem2.x = 2;
            tem2.y = 1 + max1.fi.fi + max2.fi.fi;
        }
        tem1 = min(tem1, tem2);
        ans = min(ans, tem1);
        dp[u][1] = 1 + max2.fi.fi;
        dp[u][0] = 1 + max1.fi.fi;
    }
    else{
        pii max1 = {0, 0};
        for (int i: adj[u]){
            if (i == p) continue;
            if (dp[i][0] > max1.fi){
                swap(max1.fi, max1.se);
                max1.fi = dp[i][0];
            }
            else max1.se = max(max1.se, dp[i][0]);
        }
        TFrac tem = {2, max1.fi + max1.se + 1};
        ans = min(ans, tem);
        dp[u][1] = 1 + max1.fi;
    }
    if (u == 1) cerr << dp[u][0] << ":D";
}
void Init(){
    cin >> n;
    for (int i = 1; i < n; ++i){
        int u, v;
        cin >> u >> v;
        adj[u].pb(v);
        adj[v].pb(u);
    }
    for (int i = 1; i <= n; ++i){
        cin >> a[i];
    }
    if (*min_element(a + 1, a + n + 1) > 1){
        cout << *min_element(a + 1, a + n + 1) << "/" << 1;
        return;
    }
    dfs(1, 0);
    int gcd = __gcd(ans.x, ans.y);
    ans.x /= gcd;
    ans.y /= gcd;
    cout << ans.x << "/" << ans.y;
}

#define taskname "test"
signed main(){
    faster
    if (fopen(taskname ".inp", "r")){
        freopen(taskname ".inp", "r", stdin);
        freopen(taskname ".out", "w", stdout);
    }
    int tt = 1;
    //cin >> tt;
    while (tt--){
        Init();
    }
    if (fopen("timeout.txt", "r")){
        ofstream timeout("timeout.txt");
        cerr << "Time elapsed: " << signed(double(clock()) / CLOCKS_PER_SEC * 1000) << "ms\n";
        timeout << signed(double(clock()) / CLOCKS_PER_SEC * 1000);
        timeout.close();
    }
}

Compilation message

ceste.cpp: In function 'int main()':
ceste.cpp:106:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  106 |         freopen(taskname ".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ceste.cpp:107:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  107 |         freopen(taskname ".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 2644 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 2644 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 2644 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 2644 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 2740 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 189 ms 131072 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 2772 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 2696 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 116 ms 131072 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 80 ms 131072 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -