답안 #448497

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
448497 2021-07-30T09:43:48 Z prvocislo Islands (IOI08_islands) C++17
80 / 100
1598 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, int> > 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:31: 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++)
      |                         ~~~~~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 31812 KB Output is correct
2 Correct 17 ms 31692 KB Output is correct
3 Correct 17 ms 31816 KB Output is correct
4 Correct 17 ms 31692 KB Output is correct
5 Correct 18 ms 31756 KB Output is correct
6 Correct 17 ms 31720 KB Output is correct
7 Correct 17 ms 31692 KB Output is correct
8 Correct 18 ms 31764 KB Output is correct
9 Correct 17 ms 31768 KB Output is correct
10 Correct 18 ms 31692 KB Output is correct
11 Correct 17 ms 31760 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 31772 KB Output is correct
2 Correct 21 ms 31820 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 31820 KB Output is correct
2 Correct 21 ms 32140 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 32752 KB Output is correct
2 Correct 39 ms 35296 KB Output is correct
3 Correct 30 ms 33028 KB Output is correct
4 Correct 23 ms 32332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 58 ms 36424 KB Output is correct
2 Correct 60 ms 37844 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 118 ms 42628 KB Output is correct
2 Correct 128 ms 48788 KB Output is correct
3 Correct 165 ms 59348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 186 ms 48132 KB Output is correct
2 Correct 249 ms 76200 KB Output is correct
3 Correct 326 ms 81764 KB Output is correct
4 Correct 343 ms 103528 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 395 ms 79144 KB Output is correct
2 Correct 1147 ms 131072 KB Output is correct
3 Correct 351 ms 73696 KB Output is correct
4 Correct 492 ms 117100 KB Output is correct
5 Correct 477 ms 120560 KB Output is correct
6 Correct 1598 ms 86080 KB Output is correct
7 Runtime error 491 ms 131076 KB Execution killed with signal 9
# 결과 실행 시간 메모리 Grader output
1 Runtime error 392 ms 131076 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -