답안 #933270

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
933270 2024-02-25T11:00:10 Z LucaIlie Paths (BOI18_paths) C++17
100 / 100
728 ms 25684 KB
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 3e5;
const int MAX_K = 5;
int color[MAX_N + 1];
int vis[MAX_K + 1];
long long dp[MAX_N + 1];
vector<int> perm;
vector<vector<int>> perms;
vector<int> adj[MAX_N + 1], verticesByColor[MAX_K + 1];

void genAranj( int i, int n, int k ) {
    if ( i == k ) {
        perms.push_back( perm );
        return;
    }

    for ( int x = 1; x <= n; x++ ) {
        if ( vis[x] )
            continue;

        perm.push_back( x );
        vis[x] = true;
        genAranj( i + 1, n, k );
        vis[x] = false;
        perm.pop_back();
    }
}

int main() {
    int n, m, k;

    cin >> n >> m >> k;
    for ( int v = 1; v <= n; v++ ) {
        cin >> color[v];
        verticesByColor[color[v]].push_back( v );
    }
    for ( int i = 0; i < m; i++ ) {
        int u, v;
        cin >> u >> v;
        adj[u].push_back( v );
        adj[v].push_back( u );
    }

    for ( int i = 2; i <= k; i++ )
        genAranj( 0, k, i );

    long long ans = 0;
    for ( vector<int> perm: perms ) {
        for ( int v = 1; v <= n; v++ )
            dp[v] = (color[v] == perm[0] ? 1 : 0);

        for ( int i = 1; i < perm.size(); i++ ) {
            for ( int u: verticesByColor[perm[i]] ) {
                for ( int v: adj[u] ) {
                    if ( color[v] == perm[i - 1])
                        dp[u] += dp[v];
                }
            }
        }

        for ( int v = 1; v <= n; v++ )
            ans = ans + (color[v] == perm[perm.size() - 1] ? dp[v] : 0);
    }

    cout << ans << "\n";

    return 0;
}

Compilation message

paths.cpp: In function 'int main()':
paths.cpp:55:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   55 |         for ( int i = 1; i < perm.size(); i++ ) {
      |                          ~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 10844 KB Output is correct
2 Correct 3 ms 10844 KB Output is correct
3 Correct 2 ms 10844 KB Output is correct
4 Correct 2 ms 10844 KB Output is correct
5 Correct 2 ms 10844 KB Output is correct
6 Correct 2 ms 10844 KB Output is correct
7 Correct 2 ms 10844 KB Output is correct
8 Correct 2 ms 10844 KB Output is correct
9 Correct 2 ms 10844 KB Output is correct
10 Correct 2 ms 10844 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 135 ms 17136 KB Output is correct
2 Correct 121 ms 16616 KB Output is correct
3 Correct 320 ms 25276 KB Output is correct
4 Correct 149 ms 18740 KB Output is correct
5 Correct 148 ms 18648 KB Output is correct
6 Correct 250 ms 22716 KB Output is correct
7 Correct 353 ms 24896 KB Output is correct
8 Correct 315 ms 25648 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 10844 KB Output is correct
2 Correct 3 ms 10844 KB Output is correct
3 Correct 2 ms 10844 KB Output is correct
4 Correct 2 ms 10844 KB Output is correct
5 Correct 2 ms 10844 KB Output is correct
6 Correct 2 ms 10844 KB Output is correct
7 Correct 2 ms 10844 KB Output is correct
8 Correct 2 ms 10844 KB Output is correct
9 Correct 2 ms 10844 KB Output is correct
10 Correct 2 ms 10844 KB Output is correct
11 Correct 135 ms 17136 KB Output is correct
12 Correct 121 ms 16616 KB Output is correct
13 Correct 320 ms 25276 KB Output is correct
14 Correct 149 ms 18740 KB Output is correct
15 Correct 148 ms 18648 KB Output is correct
16 Correct 250 ms 22716 KB Output is correct
17 Correct 353 ms 24896 KB Output is correct
18 Correct 315 ms 25648 KB Output is correct
19 Correct 135 ms 17168 KB Output is correct
20 Correct 112 ms 16640 KB Output is correct
21 Correct 357 ms 25092 KB Output is correct
22 Correct 155 ms 18688 KB Output is correct
23 Correct 158 ms 18580 KB Output is correct
24 Correct 238 ms 22708 KB Output is correct
25 Correct 314 ms 25016 KB Output is correct
26 Correct 339 ms 25684 KB Output is correct
27 Correct 160 ms 16720 KB Output is correct
28 Correct 194 ms 18032 KB Output is correct
29 Correct 728 ms 25208 KB Output is correct
30 Correct 376 ms 21256 KB Output is correct
31 Correct 438 ms 21196 KB Output is correct
32 Correct 697 ms 25048 KB Output is correct
33 Correct 2 ms 10844 KB Output is correct
34 Correct 2 ms 10672 KB Output is correct
35 Correct 2 ms 10844 KB Output is correct
36 Correct 2 ms 10844 KB Output is correct
37 Correct 2 ms 10844 KB Output is correct
38 Correct 2 ms 10880 KB Output is correct
39 Correct 3 ms 10844 KB Output is correct
40 Correct 2 ms 10844 KB Output is correct
41 Correct 2 ms 10840 KB Output is correct
42 Correct 2 ms 10876 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 10876 KB Output is correct
2 Correct 142 ms 12744 KB Output is correct
3 Correct 38 ms 12636 KB Output is correct
4 Correct 87 ms 15404 KB Output is correct
5 Correct 75 ms 15952 KB Output is correct
6 Correct 578 ms 15540 KB Output is correct
7 Correct 56 ms 12716 KB Output is correct
8 Correct 159 ms 15424 KB Output is correct
9 Correct 119 ms 16344 KB Output is correct
10 Correct 120 ms 16332 KB Output is correct
11 Correct 332 ms 14028 KB Output is correct
12 Correct 284 ms 15208 KB Output is correct
13 Correct 336 ms 14228 KB Output is correct
14 Correct 565 ms 15700 KB Output is correct
15 Correct 541 ms 15700 KB Output is correct
16 Correct 2 ms 10844 KB Output is correct
17 Correct 2 ms 10868 KB Output is correct
18 Correct 2 ms 10844 KB Output is correct
19 Correct 2 ms 10844 KB Output is correct
20 Correct 2 ms 10844 KB Output is correct
21 Correct 2 ms 10680 KB Output is correct
22 Correct 2 ms 10840 KB Output is correct
23 Correct 2 ms 10844 KB Output is correct
24 Correct 2 ms 10844 KB Output is correct
25 Correct 2 ms 10844 KB Output is correct