提出 #53146457


ソースコード 拡げる

#include <bits/stdc++.h>
using namespace std;
using lli = int64_t;

#define INFILE "F.inp"
#define OUTFILE "F.out"

int main() {
    ios::sync_with_stdio(0); cin.tie(0);
    if (fopen(INFILE, "r")) {
        freopen(INFILE, "r", stdin);
        freopen(OUTFILE, "w", stdout);
    }

    int N, M; cin >> N >> M;

    vector <vector <int>> graph(N, vector <int> (N, -100));

    for (int i = 0; i < M; ++i) {
        int A, B, C; cin >> A >> B >> C;
        A -= 1; B -= 1;
        graph[A][B] = C;
        graph[B][A] = -C;
    }

    for (int k = 0; k < N; ++k) {
        for (int i = 0; i < N; ++i) {
            for (int j = 0; j < N; ++j) {
                if (graph[i][k] == -100 or graph[k][j] == -100) continue;
                graph[i][j] = graph[i][k] + graph[k][j];
            }
        }
    }

    vector <int> visited(N);

    vector <vector <int>> components, nxt;
    components.emplace_back();
    nxt.emplace_back();
    vector <int> degree(N);

    auto BFS = [&] (int source) -> int {
        vector <int> current;
        queue <int> q; q.push(source);

        visited[source] = true;
        while (not q.empty()) {
            int u = q.front(); q.pop();

            current.push_back(u);
            for (int v = 0; v < N; ++v) {
                if (not visited[v] and graph[u][v] != -100) {
                    visited[v] = true;
                    q.push(v);
                }
            }
        }

        components.emplace_back(current);

        for (int u : components.back()) {
            for (int v : components.back()) {
                if (graph[v][u] > 0) {
                    degree[v] += 1;
                }
            }
        }

        int root = -1;

        vector <int> temp_value(N, -1);
        for (int x : components.back()) {
            visited[x] = false;
            if (degree[x] == 0) {
                root = x; visited[x] = true;
                q.push(x);
                temp_value[x] = 0;
            }
        }

        while (not q.empty()) {
            int u = q.front(); q.pop();
            for (int v : components.back()) {
                if (graph[v][u] > 0 and visited[v] == false) {
                    visited[v] = true;

                    temp_value[v] = temp_value[u] + graph[v][u];
                    q.push(v);
                }
            }
        }

        sort(current.begin(), current.end(), [&] (int u, int v) {
            return temp_value[u] < temp_value[v];
        });

        int mask = 0;
        components.back() = move(current);

        for (int x : components.back()) {
            mask |= 1 << temp_value[x];
        }

        return mask;
    };
    for (int i = 0; i < N; ++i) {
        if (visited[i]) continue;

        int mask = BFS(i);
        nxt.emplace_back();

        while (mask < (1 << N)) {
            nxt.back().push_back(mask);
            mask <<= 1;
        }
    }

    int len = (int) components.size();

    vector <vector <int>> dp_left(len + 1, vector <int> (1 << N)), dp_right(len + 1, vector <int> (1 << N));

    dp_left[0][0] = 1;
    for (int i = 1; i < len; ++i) {
        for (int mask = 0; mask < (1 << N); ++mask) {
            if (dp_left[i - 1][mask] == 0) continue;

            for (int x : nxt[i]) {
                if ((mask & x) == 0) {
                    dp_left[i][mask | x] = true;
                }
            }
        }
    }

    dp_right[len][0] = 1;
    for (int i = len - 1; i; --i) {
        for (int mask = 0; mask < (1 << N); ++mask) {
            if (dp_right[i + 1][mask] == 0) continue;

            for (int x : nxt[i]) {
                if ((mask & x) == 0) {
                    dp_right[i][mask | x] = true;
                }
            }
        }
    }

    vector <int> F(N, -1);

    
    for (int i = 1; i < len; ++i) {
        vector <int>& v = components[i];

        for (int chosen : nxt[i]) {
            for (int left_mask = 0; left_mask < (1 << N); ++left_mask) {
                if ((left_mask & chosen)) continue;

                if (dp_left[i - 1][left_mask] and dp_right[i + 1][((1 << N) - 1) ^ left_mask ^ chosen]) {
                    int p = 0;

                    for (int x = chosen; x; x -= x & -x) {
                        int d = __builtin_ctz(x);

                        if (F[v[p]] == -100) {
                            p += 1; continue;
                        }

                        if (F[v[p]] >= 0) {
                            F[v[p]] = -100;
                        } else {
                            F[v[p]] = d;
                        }

                        p += 1;
                    }

                    break;
                }
            }
        }
    }

    for (int i = 0; i < N; ++i) {
        if (F[i] < 0) cout << "-1 ";
        else cout << F[i] + 1 << ' ';
    }
    return 0;
}

提出情報

提出日時
問題 F - Estimate Order
ユーザ TrendBattles
言語 C++ 17 (gcc 12.2)
得点 525
コード長 5011 Byte
結果 AC
実行時間 14 ms
メモリ 12640 KiB

コンパイルエラー

Main.cpp: In lambda function:
Main.cpp:69:13: warning: variable ‘root’ set but not used [-Wunused-but-set-variable]
   69 |         int root = -1;
      |             ^~~~
Main.cpp: In function ‘int main()’:
Main.cpp:11:16: warning: ignoring return value of ‘FILE* freopen(const char*, const char*, FILE*)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   11 |         freopen(INFILE, "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~
Main.cpp:12:16: warning: ignoring return value of ‘FILE* freopen(const char*, const char*, FILE*)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   12 |         freopen(OUTFILE, "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 525 / 525
結果
AC × 3
AC × 62
セット名 テストケース
Sample sample00.txt, sample01.txt, sample02.txt
All sample00.txt, sample01.txt, sample02.txt, testcase00.txt, testcase01.txt, testcase02.txt, testcase03.txt, testcase04.txt, testcase05.txt, testcase06.txt, testcase07.txt, testcase08.txt, testcase09.txt, testcase10.txt, testcase11.txt, testcase12.txt, testcase13.txt, testcase14.txt, testcase15.txt, testcase16.txt, testcase17.txt, testcase18.txt, testcase19.txt, testcase20.txt, testcase21.txt, testcase22.txt, testcase23.txt, testcase24.txt, testcase25.txt, testcase26.txt, testcase27.txt, testcase28.txt, testcase29.txt, testcase30.txt, testcase31.txt, testcase32.txt, testcase33.txt, testcase34.txt, testcase35.txt, testcase36.txt, testcase37.txt, testcase38.txt, testcase39.txt, testcase40.txt, testcase41.txt, testcase42.txt, testcase43.txt, testcase44.txt, testcase45.txt, testcase46.txt, testcase47.txt, testcase48.txt, testcase49.txt, testcase50.txt, testcase51.txt, testcase52.txt, testcase53.txt, testcase54.txt, testcase55.txt, testcase56.txt, testcase57.txt, testcase58.txt
ケース名 結果 実行時間 メモリ
sample00.txt AC 1 ms 3484 KiB
sample01.txt AC 1 ms 3328 KiB
sample02.txt AC 1 ms 3440 KiB
testcase00.txt AC 2 ms 4716 KiB
testcase01.txt AC 2 ms 4744 KiB
testcase02.txt AC 2 ms 4712 KiB
testcase03.txt AC 2 ms 5292 KiB
testcase04.txt AC 2 ms 5260 KiB
testcase05.txt AC 2 ms 5292 KiB
testcase06.txt AC 3 ms 5864 KiB
testcase07.txt AC 3 ms 5812 KiB
testcase08.txt AC 3 ms 5720 KiB
testcase09.txt AC 4 ms 6328 KiB
testcase10.txt AC 3 ms 6372 KiB
testcase11.txt AC 4 ms 6360 KiB
testcase12.txt AC 5 ms 6964 KiB
testcase13.txt AC 5 ms 6804 KiB
testcase14.txt AC 5 ms 6840 KiB
testcase15.txt AC 6 ms 7332 KiB
testcase16.txt AC 6 ms 7264 KiB
testcase17.txt AC 5 ms 7428 KiB
testcase18.txt AC 6 ms 7880 KiB
testcase19.txt AC 6 ms 7908 KiB
testcase20.txt AC 6 ms 7924 KiB
testcase21.txt AC 6 ms 8380 KiB
testcase22.txt AC 5 ms 8428 KiB
testcase23.txt AC 7 ms 8432 KiB
testcase24.txt AC 6 ms 8940 KiB
testcase25.txt AC 7 ms 8920 KiB
testcase26.txt AC 8 ms 8952 KiB
testcase27.txt AC 7 ms 9480 KiB
testcase28.txt AC 7 ms 9428 KiB
testcase29.txt AC 7 ms 9520 KiB
testcase30.txt AC 8 ms 9980 KiB
testcase31.txt AC 8 ms 10008 KiB
testcase32.txt AC 8 ms 10112 KiB
testcase33.txt AC 9 ms 10408 KiB
testcase34.txt AC 10 ms 10552 KiB
testcase35.txt AC 10 ms 10500 KiB
testcase36.txt AC 11 ms 11084 KiB
testcase37.txt AC 11 ms 11036 KiB
testcase38.txt AC 11 ms 10928 KiB
testcase39.txt AC 12 ms 11592 KiB
testcase40.txt AC 12 ms 11572 KiB
testcase41.txt AC 12 ms 11448 KiB
testcase42.txt AC 12 ms 12212 KiB
testcase43.txt AC 14 ms 12088 KiB
testcase44.txt AC 13 ms 12056 KiB
testcase45.txt AC 14 ms 12640 KiB
testcase46.txt AC 14 ms 12588 KiB
testcase47.txt AC 14 ms 12544 KiB
testcase48.txt AC 4 ms 5164 KiB
testcase49.txt AC 2 ms 4120 KiB
testcase50.txt AC 7 ms 7532 KiB
testcase51.txt AC 1 ms 4260 KiB
testcase52.txt AC 2 ms 4104 KiB
testcase53.txt AC 3 ms 5252 KiB
testcase54.txt AC 1 ms 3836 KiB
testcase55.txt AC 1 ms 3812 KiB
testcase56.txt AC 2 ms 4748 KiB
testcase57.txt AC 3 ms 5396 KiB
testcase58.txt AC 2 ms 5248 KiB