답안 #440701

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
440701 2021-07-02T18:12:13 Z parsabahrami 특수한 그래프 (IZhO13_specialg) C++17
0 / 100
1000 ms 53500 KB
#include <bits/stdc++.h>
 
using namespace std;

typedef long long int ll;
typedef pair<int, int> pii;
 
#define SZ(x)                       (int) x.size()
#define F                           first
#define S                           second

const int N = 1e6 + 10, MOD = 1e9 + 7;
set<int> adj[N]; int dp[N], nxt[N], n, q;

void BFS(int st) {
    queue<int> Q;
    fill(dp, dp + n + 1, MOD);
    Q.push(st), dp[st] = 0;
    while (SZ(Q)) {
        int v = Q.front(); Q.pop();
        for (int u : adj[v])
            if (dp[u] > dp[v] + 1) dp[u] = dp[v] + 1, Q.push(u);
    }
}

int main() {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) 
        scanf("%d", &nxt[i]);
    for (int i = 1; i <= n; i++) {
        if (nxt[i]) adj[i].insert(nxt[i]);
    }
    scanf("%d", &q);
    for (int i = 1; i <= q; i++) {
        int t, v, u; scanf("%d%d", &t, &v);
        if (t < 2) adj[v].erase(nxt[v]);
        else {
            scanf("%d", &u);
            BFS(v);
            printf("%d\n", dp[u] > n ? -1 : dp[u]);
        }
    }
    return 0;
}

Compilation message

specialg.cpp: In function 'int main()':
specialg.cpp:27:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   27 |     scanf("%d", &n);
      |     ~~~~~^~~~~~~~~~
specialg.cpp:29:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   29 |         scanf("%d", &nxt[i]);
      |         ~~~~~^~~~~~~~~~~~~~~
specialg.cpp:33:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   33 |     scanf("%d", &q);
      |     ~~~~~^~~~~~~~~~
specialg.cpp:35:27: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   35 |         int t, v, u; scanf("%d%d", &t, &v);
      |                      ~~~~~^~~~~~~~~~~~~~~~
specialg.cpp:38:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   38 |             scanf("%d", &u);
      |             ~~~~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 30 ms 47328 KB Output is correct
2 Correct 31 ms 47380 KB Output is correct
3 Correct 32 ms 47388 KB Output is correct
4 Correct 37 ms 47428 KB Output is correct
5 Correct 33 ms 47424 KB Output is correct
6 Correct 166 ms 47660 KB Output is correct
7 Correct 174 ms 47512 KB Output is correct
8 Correct 186 ms 47556 KB Output is correct
9 Correct 160 ms 47508 KB Output is correct
10 Correct 193 ms 47604 KB Output is correct
11 Execution timed out 1099 ms 53500 KB Time limit exceeded
12 Halted 0 ms 0 KB -