답안 #44730

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
44730 2018-04-05T14:49:22 Z choikiwon 동기화 (JOI13_synchronization) C++17
0 / 100
172 ms 75480 KB
#include<bits/stdc++.h>
using namespace std;

int N, M, Q;
vector<int> adj[200010], U, V;
int D[100010];
vector<int> Time[200010];

map<int, int> dp1[200010], dp2[200010], chk;

void dfs0(int u, int p) {
    for(int i = 0; i < adj[u].size(); i++) {
        int e = adj[u][i];
        int v = U[e] + V[e] - u;
        if(v == p) continue;
        dfs0(v, u);
    }

    chk.clear();
    for(int i = 0; i < Time[u].size(); i++) {
        int t = Time[u][i];
        int e = D[t];
        int v = U[e] + V[e] - u;

        if(i) dp1[u][t] += dp1[u][ Time[u][i - 1] ];
        if(v != p) {
            if(chk.find(v) != chk.end()) {
                dp1[u][t] -= 1 + dp1[v][ chk[v] ];
            }
            chk[v] = t;
            dp1[u][t] += 1 + dp1[v][t];
        }
    }
}

void dfs1(int u, int p) {
    if(p != -1) {
        for(int i = 0; i < Time[u].size(); i++) {
            int t = Time[u][i];
            int e = D[t];
            int v = U[e] + V[e] - u;

            if(v == p) {
                dp2[u][t] += dp2[v][t];
                dp2[u][t] += dp1[v][t] - dp1[u][t];
            }
            else if(i) dp2[u][t] = dp2[u][ Time[u][i - 1] ];
        }
    }

    for(int i = 0; i < adj[u].size(); i++) {
        int e = adj[u][i];
        int v = U[e] + V[e] - u;
        if(v == p) continue;
        dfs1(v, u);
    }
}

int main() {
    scanf("%d %d %d", &N, &M, &Q);

    for(int i = 0; i < N - 1; i++) {
        int u, v; scanf("%d %d", &u, &v);
        u--; v--;

        adj[u].push_back(i);
        adj[v].push_back(i);
        U.push_back(u);
        V.push_back(v);
    }

    for(int i = 0; i < M; i++) {
        int e; scanf("%d", &e);
        e--;
        D[i] = e;
        Time[ U[e] ].push_back(i);
        Time[ V[e] ].push_back(i);
    }

    dfs0(0, -1);
    dfs1(0, -1);

    for(int i = 0; i < Q; i++) {
        int u; scanf("%d", &u);
        u--;

        if(Time[u].size()) printf("%d\n", 1 + dp1[u][ Time[u].back() ] + dp2[u][ Time[u].back() ]);
        else printf("1\n");
    }
}

Compilation message

synchronization.cpp: In function 'void dfs0(int, int)':
synchronization.cpp:12:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0; i < adj[u].size(); i++) {
                    ~~^~~~~~~~~~~~~~~
synchronization.cpp:20:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0; i < Time[u].size(); i++) {
                    ~~^~~~~~~~~~~~~~~~
synchronization.cpp: In function 'void dfs1(int, int)':
synchronization.cpp:38:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int i = 0; i < Time[u].size(); i++) {
                        ~~^~~~~~~~~~~~~~~~
synchronization.cpp:51:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0; i < adj[u].size(); i++) {
                    ~~^~~~~~~~~~~~~~~
synchronization.cpp: In function 'int main()':
synchronization.cpp:60:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d %d", &N, &M, &Q);
     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
synchronization.cpp:63: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);
                   ~~~~~^~~~~~~~~~~~~~~~~
synchronization.cpp:73:21: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         int e; scanf("%d", &e);
                ~~~~~^~~~~~~~~~
synchronization.cpp:84:21: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         int u; scanf("%d", &u);
                ~~~~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 28664 KB Output is correct
2 Correct 24 ms 28780 KB Output is correct
3 Incorrect 24 ms 28780 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 118 ms 73464 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 24 ms 73464 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 172 ms 75480 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 75480 KB Output is correct
2 Correct 24 ms 75480 KB Output is correct
3 Incorrect 24 ms 75480 KB Output isn't correct
4 Halted 0 ms 0 KB -