Submission #485867

#TimeUsernameProblemLanguageResultExecution timeMemory
485867rainboyKOVANICE (COI15_kovanice)C11
100 / 100
228 ms39008 KiB
#include <stdio.h> #include <stdlib.h> #include <string.h> #define N 300000 #define M 300000 int max(int a, int b) { return a > b ? a : b; } int ds[N]; int find(int i) { return ds[i] < 0 ? i : (ds[i] = find(ds[i])); } void join(int i, int j) { i = find(i); j = find(j); if (i == j) return; if (ds[i] > ds[j]) ds[i] = j; else { if (ds[i] == ds[j]) ds[i]--; ds[j] = i; } } void append(int **ej, int *eo, 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; } int *ej[N], eo[N], *fi[N], fo[N], dd[N], dp[N], dq[N]; void dfs(int **ej, int *eo, int *dp, int i) { if (dd[i] == 0) { int o; dd[i] = -1; for (o = eo[i]; o--; ) { int j = ej[i][o]; dp[j] = max(dp[j], dp[i] + 1), dd[j]--; dfs(ej, eo, dp, j); } } } int main() { static int ii[M], jj[M]; int n, m, k, h, i, j; scanf("%d%d%d", &k, &n, &m); memset(ds, -1, n * sizeof *ds); for (i = 0; i < n; i++) ej[i] = (int *) malloc(2 * sizeof *ej[i]); for (j = 0; j < n; j++) fi[j] = (int *) malloc(2 * sizeof *fi[j]); for (h = 0; h < m; h++) { char c; scanf("%d%c%d", &i, &c, &j), i--, j--; if (c == '=') join(i, j), ii[h] = jj[h] = -1; else if (c == '<') ii[h] = i, jj[h] = j; else ii[h] = j, jj[h] = i; } for (h = 0; h < m; h++) { i = ii[h], j = jj[h]; if (i == -1) continue; i = find(i), j = find(j); append(ej, eo, i, j), append(fi, fo, j, i); } for (i = 0; i < n; i++) if (ds[i] < 0) dd[i] = fo[i]; for (i = 0; i < n; i++) if (ds[i] < 0) dfs(ej, eo, dp, i); for (i = 0; i < n; i++) if (ds[i] < 0) dd[i] = eo[i]; for (i = 0; i < n; i++) if (ds[i] < 0) dfs(fi, fo, dq, i); for (i = 0; i < n; i++) { int p = dp[find(i)], q = dq[find(i)]; if (p + 1 + q == k) printf("K%d\n", p + 1); else printf("?\n"); } return 0; }

Compilation message (stderr)

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