結果
問題 | No.1775 Love Triangle 2 |
ユーザー | 👑 ygussany |
提出日時 | 2021-11-19 18:47:31 |
言語 | C (gcc 12.3.0) |
結果 |
TLE
(最新)
AC
(最初)
|
実行時間 | - |
コード長 | 2,797 bytes |
コンパイル時間 | 274 ms |
コンパイル使用メモリ | 32,640 KB |
実行使用メモリ | 8,736 KB |
最終ジャッジ日時 | 2024-07-03 21:08:03 |
合計ジャッジ時間 | 9,775 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,812 KB |
testcase_01 | AC | 1 ms
6,940 KB |
testcase_02 | AC | 1 ms
6,940 KB |
testcase_03 | AC | 1 ms
6,940 KB |
testcase_04 | TLE | - |
testcase_05 | -- | - |
testcase_06 | -- | - |
testcase_07 | -- | - |
testcase_08 | -- | - |
testcase_09 | -- | - |
testcase_10 | -- | - |
testcase_11 | -- | - |
testcase_12 | -- | - |
testcase_13 | -- | - |
testcase_14 | -- | - |
testcase_15 | -- | - |
testcase_16 | -- | - |
testcase_17 | -- | - |
testcase_18 | -- | - |
testcase_19 | -- | - |
testcase_20 | -- | - |
testcase_21 | -- | - |
testcase_22 | -- | - |
testcase_23 | -- | - |
testcase_24 | -- | - |
testcase_25 | -- | - |
testcase_26 | -- | - |
testcase_27 | -- | - |
testcase_28 | -- | - |
testcase_29 | -- | - |
testcase_30 | -- | - |
testcase_31 | -- | - |
testcase_32 | -- | - |
testcase_33 | -- | - |
testcase_34 | -- | - |
testcase_35 | -- | - |
testcase_36 | -- | - |
testcase_37 | -- | - |
testcase_38 | -- | - |
testcase_39 | -- | - |
testcase_40 | -- | - |
testcase_41 | -- | - |
testcase_42 | -- | - |
testcase_43 | -- | - |
testcase_44 | -- | - |
testcase_45 | -- | - |
testcase_46 | -- | - |
testcase_47 | -- | - |
testcase_48 | -- | - |
testcase_49 | -- | - |
testcase_50 | -- | - |
testcase_51 | -- | - |
testcase_52 | -- | - |
testcase_53 | -- | - |
testcase_54 | -- | - |
testcase_55 | -- | - |
testcase_56 | -- | - |
testcase_57 | -- | - |
testcase_58 | -- | - |
testcase_59 | -- | - |
testcase_60 | -- | - |
testcase_61 | -- | - |
testcase_62 | -- | - |
testcase_63 | -- | - |
testcase_64 | -- | - |
testcase_65 | -- | - |
testcase_66 | -- | - |
testcase_67 | -- | - |
testcase_68 | -- | - |
testcase_69 | -- | - |
testcase_70 | -- | - |
testcase_71 | -- | - |
testcase_72 | -- | - |
testcase_73 | -- | - |
testcase_74 | -- | - |
testcase_75 | -- | - |
testcase_76 | -- | - |
testcase_77 | -- | - |
testcase_78 | -- | - |
testcase_79 | -- | - |
testcase_80 | -- | - |
testcase_81 | -- | - |
testcase_82 | -- | - |
testcase_83 | -- | - |
testcase_84 | -- | - |
testcase_85 | -- | - |
testcase_86 | -- | - |
testcase_87 | -- | - |
testcase_88 | -- | - |
testcase_89 | -- | - |
ソースコード
#include <stdio.h> #define N_MAX 100 #define M_MAX 5000 typedef struct Edge { struct Edge *next; int v; unsigned int label; } edge; void DFS_naive(edge* adj[], int X, int Y, int Z, int u, int flag[], int* tmp, int* ans) { if (flag[u] != 0) { if (u == X && flag[Y] != 0 && flag[Z] != 0) *ans = *tmp; return; } else flag[u] = 1; edge *p; (*tmp)++; if (*tmp < *ans) for (p = adj[u]; p != NULL; p = p->next) DFS_naive(adj, X, Y, Z, p->v, flag, tmp, ans); (*tmp)--; flag[u] = 0; } // Naive solution int naive(int N, int M, int X, int Y, int Z, int A[], int B[]) { static int i, u, w, adj_mat[N_MAX + 1][N_MAX + 1]; static edge *adj[N_MAX + 1], e[M_MAX * 2], *p; for (u = 1; u <= N; u++) for (w = u + 1; w <= N; w++) adj_mat[u][w] = 0; for (i = 1; i <= M; i++) { u = A[i]; w = B[i]; adj_mat[u][w] = 1; } for (u = 1; u <= N; u++) adj[u] = NULL; for (u = 1, i = 0; u <= N; u++) { for (w = u + 1; w <= N; w++) { if (adj_mat[u][w] != 0) continue; e[i].v = w; e[i].next = adj[u]; adj[u] = &(e[i++]); e[i].v = u; e[i].next = adj[w]; adj[w] = &(e[i++]); } } static int flag[N_MAX + 1] = {}, q[N_MAX + 1], head, tail; // Y--Z path? flag[X] = 1; flag[Y] = 1; q[0] = X; q[1] = Y; for (head = 1, tail = 2; head < tail; head++) { u = q[head]; for (p = adj[u]; p != NULL; p = p->next) { w = p->v; if (flag[w] == 0) { flag[w] = 1; q[tail++] = w; } } } if (flag[Z] == 0) { for (head = 0; head < tail; head++) flag[q[head]] = 0; return -1; } else for (head = 0; head < tail; head++) flag[q[head]] = 0; // Z--X path? flag[Y] = 1; flag[Z] = 1; q[0] = Y; q[1] = Z; for (head = 1, tail = 2; head < tail; head++) { u = q[head]; for (p = adj[u]; p != NULL; p = p->next) { w = p->v; if (flag[w] == 0) { flag[w] = 1; q[tail++] = w; } } } if (flag[X] == 0) { for (head = 0; head < tail; head++) flag[q[head]] = 0; return -1; } else for (head = 0; head < tail; head++) flag[q[head]] = 0; // X--Y path? flag[Z] = 1; flag[X] = 1; q[0] = Z; q[1] = X; for (head = 1, tail = 2; head < tail; head++) { u = q[head]; for (p = adj[u]; p != NULL; p = p->next) { w = p->v; if (flag[w] == 0) { flag[w] = 1; q[tail++] = w; } } } if (flag[Y] == 0) { for (head = 0; head < tail; head++) flag[q[head]] = 0; return -1; } else for (head = 0; head < tail; head++) flag[q[head]] = 0; // DFS int ans = N + 1, tmp = 0; DFS_naive(adj, X, Y, Z, X, flag, &tmp, &ans); if (ans > N) return -1; else return ans; } int main() { int i, N, M, X, Y, Z, A[M_MAX + 1], B[M_MAX + 1]; scanf("%d %d", &N, &M); scanf("%d %d %d", &X, &Y, &Z); for (i = 1; i <= M; i++) scanf("%d %d", &(A[i]), &(B[i])); printf("%d\n", naive(N, M, X, Y, Z, A, B)); fflush(stdout); return 0; }