1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
| #include <cstdio> #include <vector> using namespace std; template <class Type> void read(Type &x) { char c; bool flag = false; while ((c = getchar()) < '0' || c > '9') c == '-' && (flag = true); x = c - '0'; while ((c = getchar()) >= '0' && c <= '9') x = (x << 3) + (x << 1) + c - '0'; flag && (x = ~x + 1); } template <class Type> void write(Type x) { x < 0 && (putchar('-'), x = ~x + 1); x > 9 && (write(x / 10), 0); putchar(x % 10 + '0'); } template <class Type> void chkmin (Type &x, Type k) { k < x && (x = k); } const int N = 1e5 + 10; bool vis[N]; int n, m, h, w[N], cnt, id[N], sz[N], du[N]; int top, stk[N]; int stmp, dfn[N], low[N]; vector <int> g[N]; void tarjan (int x) { dfn[x] = low[x] = ++stmp; vis[stk[++top] = x] = true; for (int i : g[x]) if (!dfn[i]) { tarjan(i); chkmin(low[x], low[i]); } else if (vis[i]) chkmin(low[x], dfn[i]); if (low[x] ^ dfn[x]) return; int y; ++cnt; do { vis[y = stk[top--]] = false; sz[id[y] = cnt]++; } while (x ^ y); } int main () { read(n), read(m), read(h); for (int i = 1; i <= n; i++) read(w[i]); for (int a, b; m; m--) { read(a), read(b); (w[a] + 1) % h == w[b] && (g[a].push_back(b), 0); (w[b] + 1) % h == w[a] && (g[b].push_back(a), 0); } for (int i = 1; i <= n; i++) !dfn[i] && (tarjan(i), 0); for (int i = 1; i <= n; i++) for (int j : g[i]) id[i] ^ id[j] && (du[id[i]]++); int res = -1; for (int i = 1; i <= cnt; i++) if (!du[i] && (!~res || sz[i] < sz[res])) res = i; write(sz[res]), puts(""); for (int i = 1; i <= n; i++) id[i] == res && (write(i), putchar(' ')); return 0; }
|