답안 #812728

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
812728 2023-08-07T10:29:45 Z thimote75 Reconstruction Project (JOI22_reconstruction) C++14
100 / 100
2090 ms 31212 KB
#include <bits/stdc++.h>
#define int long long

using namespace std;

using idata = vector<int>;
using igrid = vector<idata>;

using di = pair<int, int>;
using vd = vector<di>;

template<typename A, typename B>
string to_string (pair<A, B> p) {
    return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename T>
string to_string(T x) {
    string S = "[";
    bool first = true;
    for (const auto v: x) {
        if (!first) S += ", ";
        S += to_string(v);
        first = false;
    }
    S += "]";
    return S;
}
string to_string( string S ){
    return S;
}

void dbg_out () {
    cout << endl;
}
template<typename Head, typename... Tail>
void dbg_out (Head H, Tail... T) {
    cout << to_string(H) << " ";
    dbg_out(T...);
}

#ifdef DEBUG
#  define dbg(...) { cout << "(" << #__VA_ARGS__ << "): "; dbg_out(__VA_ARGS__); }
#else
#  define dbg(...)
#endif

struct Road {
    int a, b, cost;
    int xr;

    Road (int _a, int _b, int _cost) : a(_a), b(_b), cost(_cost) {
        xr = _a ^ _b;
    }
    inline int next (int node) {
        return xr ^ node;
    }

    bool operator< (const Road &other) {
        return cost < other.cost;
    }
};

using t_Roads = vector<Road>;
using bdata   = vector<bool>;

t_Roads road_array;

igrid road_indices;
bdata isUsing;

idata visited; int stage = 1;
bool dfs (int node, int target, pair<int, int> &ans) {
    if (node == target) return true;
    if (visited[node] == stage) return false;
    visited[node] = stage;

    for (int iR : road_indices[node]) {
        Road &road = road_array[iR];
        int next = road.next(node);

        if (dfs(next, target, ans)) {
            if (road.cost < ans.first)
                ans = { road.cost, iR };
            return true;
        }
    }

    return false;
}

struct UFD {
    idata parents;

    UFD (int size) {
        parents.resize(size);
        iota(parents.begin(), parents.end(), 0);
    }

    int boss (int x) {
        if (parents[x] != x)
            parents[x] = boss(parents[x]);
        return parents[x];
    }
    void merge (int a, int b) {
        a = boss(a); b = boss(b);
        parents[a] = b;
    }
};

signed main () {
    int N, M;
    cin >> N >> M;

    road_indices.resize(N);

    for (int i = 0; i < M; i ++) {
        int a, b, c;
        cin >> a >> b >> c;
        a --; b --;

        road_array.push_back( Road(a, b, c) );
    }

    sort(road_array.begin(), road_array.end());

    UFD ufd(N);
    isUsing.resize(M);
    int cost  = 0;
    int slope = - (N - 1);
    vd slope_modifications;
    for (int i = 0; i < M; i ++) {
        Road &road = road_array[i];

        if (ufd.boss(road.a) == ufd.boss(road.b)) continue ;
        ufd.merge(road.a, road.b);

        road_indices[road.a].push_back(i);
        road_indices[road.b].push_back(i);
        isUsing[i] = true;

        slope_modifications.push_back({ road.cost * 2, 2 });
        cost += 2 * road.cost;
    }

    visited.resize(N);
    for (int i = 0; i < M; i ++) {
        if (isUsing[i]) continue ;

        pair<int, int> answer = { 1e18, -1 };

        Road &new_road = road_array[i];
        stage ++;
        if (!dfs(new_road.a, new_road.b, answer)) continue ;

        Road &old_road = road_array[answer.second];
        slope_modifications.push_back({ new_road.cost + old_road.cost, - 2 });
        slope_modifications.push_back({ new_road.cost * 2, 2 });

        road_indices[old_road.a].erase(
            find(
                road_indices[old_road.a].begin(), 
                road_indices[old_road.a].end(), 
                answer.second
            )
        );
        road_indices[old_road.b].erase(
            find(
                road_indices[old_road.b].begin(), 
                road_indices[old_road.b].end(), 
                answer.second
            )
        );

        road_indices[new_road.a].push_back(i);
        road_indices[new_road.b].push_back(i);
    }

    sort(slope_modifications.begin(), slope_modifications.end());

    int last = 0; int modId = 0;
    int Q; cin >> Q;
    for (int q = 0; q < Q; q ++) {
        int t; cin >> t; t *= 2;
        while (modId != slope_modifications.size() && slope_modifications[modId].first < t) {
            int delta = slope_modifications[modId].first - last;
            cost += slope * delta;

            last  += delta;
            slope += slope_modifications[modId].second;
            modId ++;
        }

        int delta = t - last;
        cost += slope * delta;
        last += delta;

        cout << (cost >> 1) << "\n";
    }
}

Compilation message

reconstruction.cpp: In function 'int main()':
reconstruction.cpp:185:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  185 |         while (modId != slope_modifications.size() && slope_modifications[modId].first < t) {
      |                ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 232 KB Output is correct
2 Correct 1 ms 304 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 296 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 296 KB Output is correct
16 Correct 1 ms 300 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 300 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 232 KB Output is correct
2 Correct 1 ms 304 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 296 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 296 KB Output is correct
16 Correct 1 ms 300 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 300 KB Output is correct
19 Correct 1 ms 300 KB Output is correct
20 Correct 464 ms 11288 KB Output is correct
21 Correct 487 ms 11320 KB Output is correct
22 Correct 479 ms 11304 KB Output is correct
23 Correct 513 ms 11260 KB Output is correct
24 Correct 403 ms 11264 KB Output is correct
25 Correct 448 ms 11284 KB Output is correct
26 Correct 476 ms 11256 KB Output is correct
27 Correct 526 ms 11372 KB Output is correct
28 Correct 471 ms 11324 KB Output is correct
29 Correct 446 ms 11316 KB Output is correct
30 Correct 463 ms 11328 KB Output is correct
31 Correct 465 ms 11276 KB Output is correct
32 Correct 455 ms 11232 KB Output is correct
33 Correct 493 ms 11316 KB Output is correct
34 Correct 442 ms 11432 KB Output is correct
35 Correct 476 ms 11268 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1849 ms 29168 KB Output is correct
5 Correct 1651 ms 29152 KB Output is correct
6 Correct 1726 ms 29136 KB Output is correct
7 Correct 1908 ms 31004 KB Output is correct
8 Correct 1891 ms 31092 KB Output is correct
9 Correct 1770 ms 31152 KB Output is correct
10 Correct 1705 ms 29180 KB Output is correct
11 Correct 1914 ms 31212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 232 KB Output is correct
2 Correct 1 ms 304 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 296 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 296 KB Output is correct
16 Correct 1 ms 300 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 300 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1384 ms 22308 KB Output is correct
21 Correct 1362 ms 22464 KB Output is correct
22 Correct 1358 ms 22452 KB Output is correct
23 Correct 1418 ms 22292 KB Output is correct
24 Correct 1349 ms 22340 KB Output is correct
25 Correct 1358 ms 22348 KB Output is correct
26 Correct 1341 ms 22328 KB Output is correct
27 Correct 1339 ms 22312 KB Output is correct
28 Correct 1365 ms 22268 KB Output is correct
29 Correct 1426 ms 22304 KB Output is correct
30 Correct 1426 ms 22372 KB Output is correct
31 Correct 1379 ms 22128 KB Output is correct
32 Correct 1519 ms 22852 KB Output is correct
33 Correct 1440 ms 22216 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 232 KB Output is correct
2 Correct 1 ms 304 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 296 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 296 KB Output is correct
16 Correct 1 ms 300 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 300 KB Output is correct
19 Correct 1 ms 300 KB Output is correct
20 Correct 464 ms 11288 KB Output is correct
21 Correct 487 ms 11320 KB Output is correct
22 Correct 479 ms 11304 KB Output is correct
23 Correct 513 ms 11260 KB Output is correct
24 Correct 403 ms 11264 KB Output is correct
25 Correct 448 ms 11284 KB Output is correct
26 Correct 476 ms 11256 KB Output is correct
27 Correct 526 ms 11372 KB Output is correct
28 Correct 471 ms 11324 KB Output is correct
29 Correct 446 ms 11316 KB Output is correct
30 Correct 463 ms 11328 KB Output is correct
31 Correct 465 ms 11276 KB Output is correct
32 Correct 455 ms 11232 KB Output is correct
33 Correct 493 ms 11316 KB Output is correct
34 Correct 442 ms 11432 KB Output is correct
35 Correct 476 ms 11268 KB Output is correct
36 Correct 526 ms 11444 KB Output is correct
37 Correct 513 ms 11456 KB Output is correct
38 Correct 493 ms 11412 KB Output is correct
39 Correct 515 ms 11464 KB Output is correct
40 Correct 468 ms 11460 KB Output is correct
41 Correct 592 ms 11424 KB Output is correct
42 Correct 508 ms 11500 KB Output is correct
43 Correct 658 ms 11516 KB Output is correct
44 Correct 569 ms 11412 KB Output is correct
45 Correct 470 ms 11420 KB Output is correct
46 Correct 503 ms 11528 KB Output is correct
47 Correct 691 ms 11460 KB Output is correct
48 Correct 504 ms 11464 KB Output is correct
49 Correct 488 ms 11404 KB Output is correct
50 Correct 448 ms 11608 KB Output is correct
51 Correct 543 ms 11424 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 232 KB Output is correct
2 Correct 1 ms 304 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 296 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 296 KB Output is correct
16 Correct 1 ms 300 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 300 KB Output is correct
19 Correct 1 ms 300 KB Output is correct
20 Correct 464 ms 11288 KB Output is correct
21 Correct 487 ms 11320 KB Output is correct
22 Correct 479 ms 11304 KB Output is correct
23 Correct 513 ms 11260 KB Output is correct
24 Correct 403 ms 11264 KB Output is correct
25 Correct 448 ms 11284 KB Output is correct
26 Correct 476 ms 11256 KB Output is correct
27 Correct 526 ms 11372 KB Output is correct
28 Correct 471 ms 11324 KB Output is correct
29 Correct 446 ms 11316 KB Output is correct
30 Correct 463 ms 11328 KB Output is correct
31 Correct 465 ms 11276 KB Output is correct
32 Correct 455 ms 11232 KB Output is correct
33 Correct 493 ms 11316 KB Output is correct
34 Correct 442 ms 11432 KB Output is correct
35 Correct 476 ms 11268 KB Output is correct
36 Correct 0 ms 212 KB Output is correct
37 Correct 1 ms 212 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Correct 1849 ms 29168 KB Output is correct
40 Correct 1651 ms 29152 KB Output is correct
41 Correct 1726 ms 29136 KB Output is correct
42 Correct 1908 ms 31004 KB Output is correct
43 Correct 1891 ms 31092 KB Output is correct
44 Correct 1770 ms 31152 KB Output is correct
45 Correct 1705 ms 29180 KB Output is correct
46 Correct 1914 ms 31212 KB Output is correct
47 Correct 1 ms 212 KB Output is correct
48 Correct 1384 ms 22308 KB Output is correct
49 Correct 1362 ms 22464 KB Output is correct
50 Correct 1358 ms 22452 KB Output is correct
51 Correct 1418 ms 22292 KB Output is correct
52 Correct 1349 ms 22340 KB Output is correct
53 Correct 1358 ms 22348 KB Output is correct
54 Correct 1341 ms 22328 KB Output is correct
55 Correct 1339 ms 22312 KB Output is correct
56 Correct 1365 ms 22268 KB Output is correct
57 Correct 1426 ms 22304 KB Output is correct
58 Correct 1426 ms 22372 KB Output is correct
59 Correct 1379 ms 22128 KB Output is correct
60 Correct 1519 ms 22852 KB Output is correct
61 Correct 1440 ms 22216 KB Output is correct
62 Correct 526 ms 11444 KB Output is correct
63 Correct 513 ms 11456 KB Output is correct
64 Correct 493 ms 11412 KB Output is correct
65 Correct 515 ms 11464 KB Output is correct
66 Correct 468 ms 11460 KB Output is correct
67 Correct 592 ms 11424 KB Output is correct
68 Correct 508 ms 11500 KB Output is correct
69 Correct 658 ms 11516 KB Output is correct
70 Correct 569 ms 11412 KB Output is correct
71 Correct 470 ms 11420 KB Output is correct
72 Correct 503 ms 11528 KB Output is correct
73 Correct 691 ms 11460 KB Output is correct
74 Correct 504 ms 11464 KB Output is correct
75 Correct 488 ms 11404 KB Output is correct
76 Correct 448 ms 11608 KB Output is correct
77 Correct 543 ms 11424 KB Output is correct
78 Correct 1954 ms 28120 KB Output is correct
79 Correct 1852 ms 30072 KB Output is correct
80 Correct 2035 ms 29132 KB Output is correct
81 Correct 2090 ms 29180 KB Output is correct
82 Correct 1771 ms 28340 KB Output is correct
83 Correct 1832 ms 28196 KB Output is correct
84 Correct 1877 ms 28284 KB Output is correct
85 Correct 1828 ms 28148 KB Output is correct
86 Correct 1978 ms 28116 KB Output is correct
87 Correct 1876 ms 29780 KB Output is correct
88 Correct 1809 ms 28192 KB Output is correct
89 Correct 2043 ms 28236 KB Output is correct
90 Correct 1794 ms 28260 KB Output is correct
91 Correct 1931 ms 28004 KB Output is correct
92 Correct 2001 ms 31048 KB Output is correct
93 Correct 1858 ms 29196 KB Output is correct