Title Link: Hdu 5385 the path
Maintain a l,r,l increment starting from 2, r decrements from N, each distance value L,r at most one step, and each time a point mark is reached, note that the last maximum value can only be one.
#include <cstdio> #include <cstring> #include <vector> #include <queue> #include <algorithm >using namespace std;typedef pair<int,int> pii;const int maxn = 1e5 + 5;int N, M, D[MAXN], R[maxn];int X[MAXN], Y [MAXN], w[maxn];vector<pii> g[maxn];void init () {scanf ("%d%d", &n, &m); memset (r, 0, sizeof (r)); for (int i = 1; I <= N; i++) {D[i] =-1; G[i].clear ();} for (int i = 1; I <= M; i++) {W[i] = -1;scanf ("%d%d", &x[i], &y[i]); G[x[i]].push_back (Make_pair (y[i], i));//g[y[i]].push_back (Make_pair (x[i], i));}} void Solve () {d[1] = 0;queue<int> Q; Q.push (1); int L = 2, r = n;for (int d = 1; d <= N; d++) {if (R < L) Break;while (! Q.empty ()) {int u = q.front (); Q.pop (); for (int i = 0; i < g[u].size (); i++) {int v = g[u][i].first;if (r[v]) continue; R[V] = G[u][i].second;}} if (R[l]) {D[l] = d;int u = x[r[l]] + y[r[l] "-l; W[r[l]] = d[l]-d[u]; Q.push (l++);} if (R > L && r[r]) {D[r] = d;int u = x[r[r]] + y[r[r]]-r; W[r[r]] = D[R]-d[u]; Q.push (r--);}} /*for (int i = 1; I <= N; i++) printf ("%d", D[i]);p rintf ("\ n"), */}int Main () {int cas;scanf ("%d", &cas); while (CAS --) {init (); solve (); for (int i = 1; I <= M; i++) printf ("%d\n", w[i] = = 1? N:w[i]);} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 5385 The path (shortest circuit + construction)