답안 #58715

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
58715 2018-07-19T01:25:08 Z onjo0127(#1933) Paths (BOI18_paths) C++11
70 / 100
2375 ms 135884 KB
#include <bits/stdc++.h>
using namespace std;

vector<int> adj[300009];
int color[300009];
long long ans, cnt[300009][6], cnt2[300009][6][6];
bool vs[300009], c[6];

void go(int now, int lft) {
    ++ans;
    if(lft == 0) return;
    vs[now] = 1; c[color[now]] = 1;
    for(auto &it : adj[now]) {
        if(!vs[it] && !c[color[it]]) {
            go(it, lft - 1);
        }
    }
    vs[now] = 0; c[color[now]] = 0;
}

int main() {
    int N, M, K;
    scanf("%d%d%d",&N,&M,&K);
    for(int i=1; i<=N; i++) scanf("%d",&color[i]);
    for(int i=0; i<M; i++) {
        int u, v; scanf("%d%d",&u,&v);
        adj[u].push_back(v);
        adj[v].push_back(u);
        ++cnt[u][color[v]];
        ++cnt[v][color[u]];
    }
    if(K == 1) return !printf("0");
    if(N <= 100 && M <= 100 && K <= 4) {
        for(int i=1; i<=N; i++) go(i, K-1);
        printf("%lld", ans - N);
    }
    else if(N <= 300000 && M <= 300000 && K <= 3) {
        int tmp = 1 ^ 2 ^ 3;
        for(int i=1; i<=N; i++) {
            for(auto &it : adj[i]) {
                if(color[i] != color[it]) {
                    ++ans;
                    if(K == 3) ans += cnt[it][tmp ^ color[i] ^ color[it]];
                }
            }
        }
        printf("%lld", ans);
    }
    else if(N <= 300000 && M <= 300000 && K <= 4) {
        for(int i=1; i<=N; i++) {
            for(auto &it : adj[i]) {
                if(color[i] != color[it]) {
                    ++ans;
                    set<int> st = {1, 2, 3, 4};
                    st.erase(color[i]); st.erase(color[it]);
                    int a = *st.begin(), b = *(++st.begin());
                    ans += cnt[it][a] + cnt[it][b];
                    ++cnt2[it][a][b];
                }
            }
        }
        for(int i=1; i<=N; i++) {
            for(auto &it : adj[i]) {
                for(int x=1; x<=4; x++) {
                    for(int y=x+1; y<=4; y++) {
                        set<int> st = {1, 2, 3, 4};
                        st.erase(x); st.erase(y);
                        int a = *st.begin(), b = *(++st.begin());
                        ans += cnt2[it][a][b] * cnt2[i][x][y];
                    }
                }
            }
        }
        printf("%lld", ans);
    }
    return 0;
}

Compilation message

paths.cpp: In function 'int main()':
paths.cpp:23:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d%d",&N,&M,&K);
     ~~~~~^~~~~~~~~~~~~~~~~~~
paths.cpp:24:34: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     for(int i=1; i<=N; i++) scanf("%d",&color[i]);
                             ~~~~~^~~~~~~~~~~~~~~~
paths.cpp:26:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         int u, v; scanf("%d%d",&u,&v);
                   ~~~~~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 7416 KB Output is correct
2 Correct 10 ms 7528 KB Output is correct
3 Correct 7 ms 7528 KB Output is correct
4 Correct 9 ms 7528 KB Output is correct
5 Correct 10 ms 7528 KB Output is correct
6 Correct 7 ms 7528 KB Output is correct
7 Correct 9 ms 7560 KB Output is correct
8 Correct 9 ms 7636 KB Output is correct
9 Correct 7 ms 7636 KB Output is correct
10 Correct 7 ms 7636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 111 ms 11648 KB Output is correct
2 Correct 105 ms 11648 KB Output is correct
3 Correct 385 ms 31316 KB Output is correct
4 Correct 172 ms 31316 KB Output is correct
5 Correct 222 ms 31316 KB Output is correct
6 Correct 302 ms 31316 KB Output is correct
7 Correct 464 ms 31328 KB Output is correct
8 Correct 493 ms 31980 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 7416 KB Output is correct
2 Correct 10 ms 7528 KB Output is correct
3 Correct 7 ms 7528 KB Output is correct
4 Correct 9 ms 7528 KB Output is correct
5 Correct 10 ms 7528 KB Output is correct
6 Correct 7 ms 7528 KB Output is correct
7 Correct 9 ms 7560 KB Output is correct
8 Correct 9 ms 7636 KB Output is correct
9 Correct 7 ms 7636 KB Output is correct
10 Correct 7 ms 7636 KB Output is correct
11 Correct 111 ms 11648 KB Output is correct
12 Correct 105 ms 11648 KB Output is correct
13 Correct 385 ms 31316 KB Output is correct
14 Correct 172 ms 31316 KB Output is correct
15 Correct 222 ms 31316 KB Output is correct
16 Correct 302 ms 31316 KB Output is correct
17 Correct 464 ms 31328 KB Output is correct
18 Correct 493 ms 31980 KB Output is correct
19 Correct 140 ms 31980 KB Output is correct
20 Correct 107 ms 31980 KB Output is correct
21 Correct 577 ms 31980 KB Output is correct
22 Correct 237 ms 31980 KB Output is correct
23 Correct 217 ms 31980 KB Output is correct
24 Correct 347 ms 31980 KB Output is correct
25 Correct 485 ms 31980 KB Output is correct
26 Correct 426 ms 32108 KB Output is correct
27 Correct 1458 ms 32108 KB Output is correct
28 Correct 1682 ms 32108 KB Output is correct
29 Correct 2298 ms 123228 KB Output is correct
30 Correct 2298 ms 123228 KB Output is correct
31 Correct 2193 ms 123228 KB Output is correct
32 Correct 2375 ms 135884 KB Output is correct
33 Correct 9 ms 135884 KB Output is correct
34 Correct 8 ms 135884 KB Output is correct
35 Correct 8 ms 135884 KB Output is correct
36 Correct 8 ms 135884 KB Output is correct
37 Correct 9 ms 135884 KB Output is correct
38 Correct 10 ms 135884 KB Output is correct
39 Correct 10 ms 135884 KB Output is correct
40 Correct 10 ms 135884 KB Output is correct
41 Correct 9 ms 135884 KB Output is correct
42 Correct 10 ms 135884 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 7 ms 135884 KB Output isn't correct
2 Halted 0 ms 0 KB -