AtCoder Beginner Contest 225 D - Play Train
阿新 • • 發佈:2021-12-17
題意
題解
手動做連結串列模擬
#include<cstdio> #include<iostream> #include<vector> using namespace std; const int MAXN = 2e5; struct Car { int front, back; }tr[MAXN]; int vec[MAXN]; int main() { int n, q; scanf("%d%d", &n, &q); for (int i = 1; i <= q; i++) { int opt; scanf("%d", &opt); if (opt == 1) { int x, y; scanf("%d%d", &x, &y); tr[y].front = x; tr[x].back = y; } if (opt == 2) { int x, y; scanf("%d%d", &x, &y); tr[y].front = 0; tr[x].back = 0; } if (opt == 3) { int x; scanf("%d", &x); int front = x; while (tr[front].front != 0)front = tr[front].front; int num = 0; vec[++num] = front; while (tr[front].back != 0) { vec[++num] = tr[front].back; front = tr[front].back; } printf("%d ", num); for (int i = 1; i <= num; i++) { printf("%d ", vec[i]); } printf("\n"); } } return 0; }
TRANSLATE with x English TRANSLATE with EMBED THE SNIPPET BELOW IN YOUR SITE Enable collaborative features and customize widget: