답안 #1119337

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1119337 2024-11-26T20:51:27 Z mariaclara Reconstruction Project (JOI22_reconstruction) C++17
42 / 100
5000 ms 8984 KB
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
typedef tuple<int,int,int> trio;
const int MAXN = 1e5+5;
#define W(x) get<0>(x)
#define U(x) get<1>(x)
#define V(x) get<2>(x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define mk make_pair 
#define pb push_back 
#define fr first 
#define sc second 

int n, m, q, remov[MAXN], add[MAXN], pai[505], tam[505];
vector<trio> edges;
vector<vector<pii>> graph;

int dfs(int no, int fim, int pai, int ind, int flag) {
    if(no == fim) return ind;

    for(auto [viz, i] : graph[no]) {
        if(viz == pai) continue;
        int new_ind = ind;
        if(W(edges[i])*flag < W(edges[ind])*flag) new_ind = i;
        
        int ret = dfs(viz, fim, no, new_ind, flag);
        if(ret != -1) return ret;
    }

    return -1;
}

int find(int x) {
    if(pai[x] == x) return x;
    return pai[x] = find(pai[x]);
}

bool join(int x, int y) {
    x = find(x);
    y = find(y);
    if(x == y) return 0;

    if(tam[x] <= tam[y]) {
        pai[x] = y;
        tam[y] += tam[x];
    }
    else {
        pai[y] = x;
        tam[x] += tam[y];
    }

    return 1;
}
int main() {

    cin >> n >> m;

    for(int i = 1, a, b, w; i <= m; i++) {
        cin >> a >> b >> w;
        edges.pb({w,a,b});
    }

    sort(all(edges));
    graph.resize(n+1);

    for(int i = 0; i < m; i++) {
        auto [w, a, b] = edges[i];

        // se existe um caminho entre a e b
        // removemos a menor aresta nele

        int sub = dfs(a, b, 0, i, 1);
        graph[a].pb({b, i});
        graph[b].pb({a, i});

        remov[i] = sub;
        
        if(sub == -1)  continue;

        auto [w1, a1, b1] = edges[sub];

        for(int j = 0; j < sz(graph[a1]); j++)
            if(graph[a1][j].sc == sub) {
                graph[a1].erase({graph[a1].begin() + j});
                break;
            }

        for(int j = 0; j < sz(graph[b1]); j++)
            if(graph[b1][j].sc == sub) {
                graph[b1].erase({graph[b1].begin() + j});
                break;
            }
    }

    graph.clear();
    graph.resize(n+1);
    set<int> active;

    for(int i = m-1; i >= 0; i--) {
        auto [w, a, b] = edges[i];

        // se existe um caminho entre a e b
        // removemos a menor aresta nele

        int sub = dfs(a, b, 0, i, -1);
        graph[a].pb({b, i});
        graph[b].pb({a, i});
        active.insert(i);
        add[i] = sub;
        
        if(sub == -1)  continue;
        active.erase(sub);

        auto [w1, a1, b1] = edges[sub];

        for(int j = 0; j < sz(graph[a1]); j++)
            if(graph[a1][j].sc == sub) {
                graph[a1].erase({graph[a1].begin() + j});
                break;
            }

        for(int j = 0; j < sz(graph[b1]); j++)
            if(graph[b1][j].sc == sub) {
                graph[b1].erase({graph[b1].begin() + j});
                break;
            }
    }

    cin >> q;

    int ptr = 0, w;
    while(q--) {
        cin >> w;

        while(ptr < m and W(edges[ptr]) <= w) {
            if(add[ptr] != -1) active.insert(add[ptr]);
            if(remov[ptr] != -1) active.erase(remov[ptr]);
            ptr++;
        }

        for(int i = 1; i <= n; i++) pai[i] = i, tam[i] = 1;

        vector<trio> pos1, pos2;
        for(auto it : active) {
            if(W(edges[it]) <= w) pos1.pb(edges[it]);
            else pos2.pb(edges[it]);
        }
        reverse(all(pos1));
      
        ll ans = 0;
        int i = 0, j = 0;
        for(int i = 0, j = 0; i < sz(pos1) and j < sz(pos2); ) {
            auto [w1, a1, b1] = pos1[i];
            auto [w2, a2, b2] = pos2[j];

            if(w - w1 <= w2 - w) {
                if(join(a1, b1)) ans += w - w1;
                i++;
            }
            else {
                if(join(a2, b2)) ans += w2 - w;
                j++;
            }
        }

        while(i < sz(pos1)) ans += (w - W(pos1[i])) * join(U(pos1[i]), V(pos1[i])), i++;
        while(j < sz(pos2)) ans += (W(pos2[j]) - w) * join(U(pos2[j]), V(pos2[j])), j++;
        cout << ans << endl;
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 504 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 504 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 1 ms 336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 504 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 504 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 1 ms 336 KB Output is correct
19 Correct 1 ms 336 KB Output is correct
20 Correct 509 ms 4036 KB Output is correct
21 Correct 379 ms 4132 KB Output is correct
22 Correct 475 ms 4220 KB Output is correct
23 Correct 491 ms 4072 KB Output is correct
24 Correct 474 ms 4048 KB Output is correct
25 Correct 510 ms 4028 KB Output is correct
26 Correct 552 ms 4008 KB Output is correct
27 Correct 513 ms 4028 KB Output is correct
28 Correct 535 ms 4104 KB Output is correct
29 Correct 352 ms 4028 KB Output is correct
30 Correct 527 ms 4168 KB Output is correct
31 Correct 536 ms 4216 KB Output is correct
32 Correct 525 ms 4240 KB Output is correct
33 Correct 514 ms 4184 KB Output is correct
34 Correct 308 ms 4284 KB Output is correct
35 Correct 510 ms 4120 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 456 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Execution timed out 5084 ms 8128 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 504 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 504 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 1 ms 336 KB Output is correct
19 Correct 1 ms 336 KB Output is correct
20 Execution timed out 5083 ms 8984 KB Time limit exceeded
21 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 504 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 504 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 1 ms 336 KB Output is correct
19 Correct 1 ms 336 KB Output is correct
20 Correct 509 ms 4036 KB Output is correct
21 Correct 379 ms 4132 KB Output is correct
22 Correct 475 ms 4220 KB Output is correct
23 Correct 491 ms 4072 KB Output is correct
24 Correct 474 ms 4048 KB Output is correct
25 Correct 510 ms 4028 KB Output is correct
26 Correct 552 ms 4008 KB Output is correct
27 Correct 513 ms 4028 KB Output is correct
28 Correct 535 ms 4104 KB Output is correct
29 Correct 352 ms 4028 KB Output is correct
30 Correct 527 ms 4168 KB Output is correct
31 Correct 536 ms 4216 KB Output is correct
32 Correct 525 ms 4240 KB Output is correct
33 Correct 514 ms 4184 KB Output is correct
34 Correct 308 ms 4284 KB Output is correct
35 Correct 510 ms 4120 KB Output is correct
36 Correct 1197 ms 4448 KB Output is correct
37 Correct 1022 ms 4696 KB Output is correct
38 Correct 1139 ms 4616 KB Output is correct
39 Correct 1259 ms 4540 KB Output is correct
40 Correct 1168 ms 4496 KB Output is correct
41 Correct 1163 ms 4540 KB Output is correct
42 Correct 1267 ms 4540 KB Output is correct
43 Correct 1242 ms 4540 KB Output is correct
44 Correct 968 ms 4544 KB Output is correct
45 Correct 719 ms 4404 KB Output is correct
46 Correct 1199 ms 4420 KB Output is correct
47 Correct 1185 ms 4540 KB Output is correct
48 Correct 1152 ms 4620 KB Output is correct
49 Correct 1184 ms 4564 KB Output is correct
50 Correct 516 ms 4636 KB Output is correct
51 Correct 976 ms 4556 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 504 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 504 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 1 ms 336 KB Output is correct
19 Correct 1 ms 336 KB Output is correct
20 Correct 509 ms 4036 KB Output is correct
21 Correct 379 ms 4132 KB Output is correct
22 Correct 475 ms 4220 KB Output is correct
23 Correct 491 ms 4072 KB Output is correct
24 Correct 474 ms 4048 KB Output is correct
25 Correct 510 ms 4028 KB Output is correct
26 Correct 552 ms 4008 KB Output is correct
27 Correct 513 ms 4028 KB Output is correct
28 Correct 535 ms 4104 KB Output is correct
29 Correct 352 ms 4028 KB Output is correct
30 Correct 527 ms 4168 KB Output is correct
31 Correct 536 ms 4216 KB Output is correct
32 Correct 525 ms 4240 KB Output is correct
33 Correct 514 ms 4184 KB Output is correct
34 Correct 308 ms 4284 KB Output is correct
35 Correct 510 ms 4120 KB Output is correct
36 Correct 1 ms 336 KB Output is correct
37 Correct 1 ms 456 KB Output is correct
38 Correct 1 ms 336 KB Output is correct
39 Execution timed out 5084 ms 8128 KB Time limit exceeded
40 Halted 0 ms 0 KB -