1209F - Koala and Notebook

The game did not play, watching the students fst, so take a look.

This question may seem simple, but the details really thought clearly not OK. As they are now coming around eleven o'clock, not really mind.

First, obviously the median small as possible, because every one to compare, as split points. This time to split into two chains have to (start implementing became undirected chain)

Then this time it can easily run most short-circuited. But more details.

First, go directly greedy minimum edge and then bfs does not work, so consider stratification (here too hung up). For each point projected to the sides of equal length belonging to an equivalence class. At this time, the number of equivalence classes is easy to prove \ (O (m) \) a.

So you can run directly stratification. From small to large by the lexicographic enumeration equivalence class, is easy to know when the update continues to insert a new point is in the end of the list equivalence classes.

This question is on these two difficulties, so are pit two players to a low level.

#include <bits/stdc++.h>

const int mod = 1000000007;
const int MAXN = 1100010;
std::vector<int> G[MAXN][10], qs[MAXN];
void addedge(int b, int e, int v) {
    G[b][v].push_back(e);
}
int n, m, idx, dp[MAXN];
bool vis[MAXN];
int main() {
    std::ios_base::sync_with_stdio(false), std::cin.tie(0);
    std::cin >> n >> m; idx = n;
    for (int i = 1; i <= m; ++i) {
        int t1, t2; std::cin >> t1 >> t2;
        static int arr[10], bak; bak = 0;
        int t = i;
        while (t) arr[bak++] = t % 10, t /= 10;
        int lst = t1;
        for (int j = bak - 1; ~j; --j) {
            int now = j ? ++idx : t2;
            addedge(lst, now, arr[j]);
            lst = now;
        }
        for (int j = bak - 1; ~j; --j) {
            int now = j ? ++idx : t1;
            addedge(lst, now, arr[j]);
            lst = now;
        }
    }
    int T;
    qs[T = 1].push_back(1); vis[1] = true;
    for (int P = 1; P <= T; ++P) {
        for (int j = 0; j != 10; ++j) {
            bool flg = false;
            for (auto t : qs[P]) {
                for (auto v : G[t][j]) {
                    if (vis[v]) continue;
                    vis[v] = true;
                    dp[v] = (dp[t] * 10ll + j) % mod;
                    qs[T + 1].push_back(v);
                    flg = true;
                }
            }
            if (flg) ++T;
        }
    }
    for (int i = 2; i <= n; ++i)
        std::cout << dp[i] << '\n';
    return 0;
}

Guess you like

Origin www.cnblogs.com/daklqw/p/11546379.html