Submission #448464

# Submission time Handle Problem Language Result Execution time Memory
448464 2021-07-30T08:48:12 Z prvocislo Islands (IOI08_islands) C++17
80 / 100
888 ms 131076 KB
#include <iostream>
#include <vector>
#include <algorithm>
typedef long long ll;
using namespace std;

const int maxn = 1e6 + 5;
vector<bool> vis(maxn, false);
vector<pair<int, ll> > p(maxn, { -1, 0 });
vector<vector<pair<int, int> > > g(maxn);
void dfs1(int u, int& a, int& b, ll &l, vector<int>& v)
{
    v.push_back(u); vis[u] = true;
    for (const pair<int, int>& i : g[u])
    {
        if (!vis[i.first]) p[i.first] = { u, i.second }, dfs1(i.first, a, b, l, v);
        else if (i.first != p[u].first) a = i.first, b = u, l = i.second;
    }
}
pair<ll, int> dfs2(int u, int p, ll d)
{
    pair<ll, int> ans(d, u);
    for (const pair<int, int>& i : g[u]) if (!vis[i.first] && i.first != p) 
        ans = max(ans, dfs2(i.first, u, d + i.second));
    return ans;
}
void del(int u, int v, int l)
{
    for (int i = 0; i < g[u].size(); i++) if (g[u][i] == make_pair(v, l))
    {
        g[u].erase(g[u].begin() + i);
        break;
    }
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n; cin >> n;
    for (int i = 0, u, l; i < n; i++) cin >> u >> l, g[--u].push_back({ i, l }), g[i].push_back({ u, l });
    ll ans = 0;
    for (int i = 0; i < n; i++) if (!vis[i])
    {
        int u, v; ll l; vector<int> c;
        dfs1(i, u, v, l, c); // u je ten nizsi, v je ten vyssi
        del(u, v, l), del(v, u, l);
        for (int j : c) vis[j] = false;
        int v1 = dfs2(i, -1, 0).second;
        ll maxi = dfs2(v1, -1, 0).first;
        vector<int> path;
        for (int j = u; j != p[v].first; j = p[j].first)
            path.push_back(j), vis[j] = true;
        int k = path.size();
        vector<ll> best, pf(k, 0), sf(k, 0);
        for (int j : path)best.push_back(dfs2(j, -1, 0).first);
        ll len = 0;
        for (int j = 0; j < path.size(); j++)
        {
            if (j) len += p[path[j-1]].second;
            pf[j] = len + best[j];
            if (j) pf[j] = max(pf[j], pf[j - 1]);
        } len = 0;
        for (int j = path.size() - 1; j >= 0; j--)
        {
            if (j != path.size()-1) len += p[path[j]].second;
            sf[j] = len + best[j];
            if (j != path.size() - 1) sf[j] = max(sf[j], sf[j + 1]);
        }
        for (int j = 0; j+1 < path.size(); j++)
            maxi = max(maxi, l + pf[j] + sf[j + 1]);
        ans += maxi;
        for (int j : c) vis[j] = true;
    }
    cout << ans << "\n";
    return 0;
}

Compilation message

islands.cpp: In function 'void del(int, int, int)':
islands.cpp:29:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   29 |     for (int i = 0; i < g[u].size(); i++) if (g[u][i] == make_pair(v, l))
      |                     ~~^~~~~~~~~~~~~
islands.cpp: In function 'int main()':
islands.cpp:57:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   57 |         for (int j = 0; j < path.size(); j++)
      |                         ~~^~~~~~~~~~~~~
islands.cpp:65:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   65 |             if (j != path.size()-1) len += p[path[j]].second;
      |                 ~~^~~~~~~~~~~~~~~~
islands.cpp:67:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   67 |             if (j != path.size() - 1) sf[j] = max(sf[j], sf[j + 1]);
      |                 ~~^~~~~~~~~~~~~~~~~~
islands.cpp:69:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   69 |         for (int j = 0; j+1 < path.size(); j++)
      |                         ~~~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 21 ms 39500 KB Output is correct
2 Correct 21 ms 39496 KB Output is correct
3 Correct 22 ms 39628 KB Output is correct
4 Correct 20 ms 39592 KB Output is correct
5 Correct 21 ms 39500 KB Output is correct
6 Correct 21 ms 39496 KB Output is correct
7 Correct 21 ms 39556 KB Output is correct
8 Correct 20 ms 39500 KB Output is correct
9 Correct 21 ms 39548 KB Output is correct
10 Correct 21 ms 39528 KB Output is correct
11 Correct 21 ms 39552 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 39628 KB Output is correct
2 Correct 24 ms 39628 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 39628 KB Output is correct
2 Correct 25 ms 40084 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 29 ms 40636 KB Output is correct
2 Correct 42 ms 43436 KB Output is correct
3 Correct 34 ms 40828 KB Output is correct
4 Correct 27 ms 40180 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 61 ms 44920 KB Output is correct
2 Correct 64 ms 46876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 130 ms 52912 KB Output is correct
2 Correct 139 ms 59788 KB Output is correct
3 Correct 162 ms 72836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 187 ms 61132 KB Output is correct
2 Correct 265 ms 92348 KB Output is correct
3 Correct 304 ms 98708 KB Output is correct
4 Correct 348 ms 126644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 403 ms 102212 KB Output is correct
2 Runtime error 888 ms 131076 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 444 ms 131076 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -