답안 #114666

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
114666 2019-06-02T08:42:34 Z zubec Islands (IOI08_islands) C++14
30 / 100
707 ms 131072 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;

const int N = 1000100;

int dsu[N], sz[N];

int n;

int dsu_get(int v){
    if (dsu[v] == v)
        return v;
    return dsu[v] = dsu_get(dsu[v]);
}

void dsu_unite(int a, int b){
    a = dsu_get(a);
    b = dsu_get(b);
    if (a == b)
        return;
    if (sz[a] < sz[b])
        swap(a, b);
    sz[a] += sz[b];
    dsu[b] = a;
}

ll ans = 0;

vector <int> g[N];

int len[N], nxt[N];

vector <pair<int, int> > gg[N];

bool used[N];

ll mx = -1;
int mxver = 0;

void dfs(int v, int p, ll curlen){
    if (curlen > mx){
        mx = curlen;
        mxver = v;
    }
    for (int i = 0; i < gg[v].size(); i++){
        int to = gg[v][i].first, len = gg[v][i].second;
        if (to != p)
            dfs(to, v, curlen+len);
    }
}

int main(){
    ios_base::sync_with_stdio(0);cin.tie(0);

    cin >> n;
    for (int i = 1; i <= n; i++){
        dsu[i] = i;
        sz[i] = 1;
    }
    for (int i = 1; i <= n; i++){
        int to;
        cin >> to >> len[i];
        nxt[i] = to;
        g[i].push_back(to);
        g[to].push_back(i);
    }
    for (int i = 1; i <= n; i++){
        if (used[i])
            continue;
        queue <int> q;
        vector <pair<int, pair<int, int> > > vec;
        q.push(i);
        while(!q.empty()){
            int v = q.front();
            q.pop();
            vec.push_back({len[v], {v, nxt[v]}});
            for (int j = 0; j < g[v].size(); j++){
                int to = g[v][j];
                if (!used[to]){
                    used[to] = 1;
                    q.push(to);
                }
            }
        }
        sort(vec.rbegin(), vec.rend());
        for (int i = 0; i < vec.size(); i++){
            int a = vec[i].second.first, b = vec[i].second.second;
            if (dsu_get(a) != dsu_get(b)){
                gg[a].push_back({b, vec[i].first});
                gg[b].push_back({a, vec[i].first});
                dsu_unite(a, b);
            }
        }
        mx = -1, mxver = 0;
        dfs(i, 0, 0);
        mx = -1;
        dfs(mxver, 0, 0);
        ans += mx;
    }
    cout << ans;


}

Compilation message

islands.cpp: In function 'void dfs(int, int, ll)':
islands.cpp:47:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < gg[v].size(); i++){
                     ~~^~~~~~~~~~~~~~
islands.cpp: In function 'int main()':
islands.cpp:79:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             for (int j = 0; j < g[v].size(); j++){
                             ~~^~~~~~~~~~~~~
islands.cpp:88:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (int i = 0; i < vec.size(); i++){
                         ~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 43 ms 47352 KB Output is correct
2 Correct 41 ms 47356 KB Output is correct
3 Correct 40 ms 47352 KB Output is correct
4 Correct 42 ms 47352 KB Output is correct
5 Correct 42 ms 47352 KB Output is correct
6 Correct 51 ms 47288 KB Output is correct
7 Incorrect 44 ms 47304 KB Output isn't correct
8 Incorrect 45 ms 47352 KB Output isn't correct
9 Correct 42 ms 47324 KB Output is correct
10 Correct 43 ms 47352 KB Output is correct
11 Correct 44 ms 47480 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 43 ms 47480 KB Output is correct
2 Correct 45 ms 47472 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 46 ms 47600 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 57 ms 49016 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 89 ms 53972 KB Output is correct
2 Incorrect 112 ms 57268 KB Output isn't correct
# 결과 실행 시간 메모리 Grader output
1 Correct 233 ms 68276 KB Output is correct
2 Correct 227 ms 71084 KB Output is correct
3 Incorrect 363 ms 79232 KB Output isn't correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 329 ms 82140 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 564 ms 131072 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 707 ms 131072 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -