Submission #545884

#TimeUsernameProblemLanguageResultExecution timeMemory
545884rainboy007 (CEOI14_007)C11
100 / 100
306 ms23228 KiB
#include <stdio.h> #include <stdlib.h> #define N 200000 int min(int a, int b) { return a < b ? a : b; } int *ej[N], eo[N]; void append(int i, int j) { int o = eo[i]++; if (o >= 2 && (o & o - 1) == 0) ej[i] = (int *) realloc(ej[i], o * 2 * sizeof *ej[i]); ej[i][o] = j; } void bfs(int *dd, int n, int s) { static int qu[N]; int i, head, cnt; for (i = 0; i < n; i++) dd[i] = n; head = cnt = 0; dd[s] = 0, qu[head + cnt++] = s; while (cnt) { int d, o; i = qu[cnt--, head++], d = dd[i] + 1; for (o = eo[i]; o--; ) { int j = ej[i][o]; if (dd[j] > d) dd[j] = d, qu[head + cnt++] = j; } } } int dda[N], ddb[N], dd_[N]; char visited[N]; void dfs(int i) { int o; if (visited[i]) return; dd_[i] = dda[i], visited[i] = 1; for (o = eo[i]; o--; ) { int j = ej[i][o]; if (dda[j] == dda[i] - 1 && ddb[j] == ddb[i] - 1) { dfs(j); dd_[i] = min(dd_[i], dd_[j]); } } } int main() { int n, m, s, t, a, b, i, j, d; scanf("%d%d%d%d%d%d", &n, &m, &s, &t, &a, &b), s--, t--, a--, b--; for (i = 0; i < n; i++) ej[i] = (int *) malloc(2 * sizeof *ej[i]); while (m--) { scanf("%d%d", &i, &j), i--, j--; append(i, j), append(j, i); } bfs(dda, n, a); bfs(ddb, n, b); d = dda[s] > dda[t] || ddb[s] > ddb[t] ? -1 : min(dda[t] - dda[s], ddb[t] - ddb[s]); if (d != -1 && dda[s] == ddb[s] && dda[t] == ddb[t]) { dfs(s), dfs(t); if (dd_[s] > dd_[t]) d--; } printf("%d\n", d); return 0; }

Compilation message (stderr)

007.c: In function 'append':
007.c:13:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   13 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
007.c: In function 'main':
007.c:60:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   60 |  scanf("%d%d%d%d%d%d", &n, &m, &s, &t, &a, &b), s--, t--, a--, b--;
      |  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
007.c:64:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   64 |   scanf("%d%d", &i, &j), i--, j--;
      |   ^~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...