提出 #22667026


ソースコード 拡げる

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
#define fore(i,a) for(auto &i:a)
#define all(x) (x).begin(),(x).end()
//#pragma GCC optimize ("-O3")
using namespace std;
void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60;
template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }
//---------------------------------------------------------------------------------------------------
struct HLDecomposition {
    vector<set<int>> g; vector<int> vid, head, heavy, parent, depth, inv, in, out;
    HLDecomposition() {} HLDecomposition(int n) { init(n); }
    void init(int n) {
        g.resize(n); vid.resize(n, -1); head.resize(n); heavy.resize(n, -1);
        parent.resize(n); depth.resize(n); inv.resize(n); in.resize(n); out.resize(n);
    }
    void add(int u, int v) { g[u].insert(v); g[v].insert(u); }
	void build(int root) { dfs(root, -1); t = 0; dfs_hld(root); }

    int dfs(int curr, int prev) {
        parent[curr] = prev; int sub = 1, max_sub = 0;
        heavy[curr] = -1;
        for (int next : g[curr]) if (next != prev) {
            depth[next] = depth[curr] + 1;
            int sub_next = dfs(next, curr); sub += sub_next;
            if (max_sub < sub_next) max_sub = sub_next, heavy[curr] = next;
        }return sub;
    }

    int t = 0;
    void dfs_hld(int v = 0)
    {
        vid[v] = in[v] = t;
        t++;
        inv[in[v]] = v;
        if (0 <= heavy[v]) {
            head[heavy[v]] = head[v];
            dfs_hld(heavy[v]);
        }
        for (auto u : g[v]) if(depth[v] < depth[u])  if (u != heavy[v]) {
            head[u] = u;
            dfs_hld(u);
        }
        out[v] = t;
    }


    void foreach(int u, int v, function<void(int, int)> f) { // [x,y]
        if (vid[u] > vid[v]) swap(u, v); f(max(vid[head[v]], vid[u]), vid[v]);
        if (head[u] != head[v]) foreach(u, parent[head[v]], f);
    }
    int ancestor(int from, int times) {
        while (true) {
            if (depth[head[from]] > depth[from] - times) {
                times -= depth[from] - depth[head[from]] + 1; if (head[from] == 0)return -1; from = parent[head[from]];
            }
            else return inv[vid[from] - times];
        }
    }
    int lca(int u, int v) {
        if (vid[u] > vid[v]) swap(u, v); if (head[u] == head[v]) return u;
        return lca(u, parent[head[v]]);
    }
    int distance(int u, int v) { return depth[u] + depth[v] - 2 * depth[lca(u, v)]; }
    int child(int parent, int child, int times) {
        assert(depth[parent]<depth[child]);
        int d = distance(parent, child); assert(times - 1 <= d); return ancestor(child, d - times);
    }
    int go(int from, int to, int times) {
        int d = distance(from, to); assert(0 <= times and times <= d);
        int lc = lca(from, to); if (lc == to)return ancestor(from, times); if (lc == from)return child(from, to, times);
        int dd = distance(from, lc); if (dd <= times)return go(lc, to, times - dd); return ancestor(from, times);
    }
};
/*---------------------------------------------------------------------------------------------------
            ∧_∧
      ∧_∧  (´<_` )  Welcome to My Coding Space!
     ( ´_ゝ`) /  ⌒i     @hamayanhamayan0
    /   \     | |
    /   / ̄ ̄ ̄ ̄/  |
  __(__ニつ/     _/ .| .|____
     \/____/ (u ⊃
---------------------------------------------------------------------------------------------------*/










/*


int d[V][V];
REP(i, V) REP(j, V)
{
    if (i == j)
        d[i][j] = 0;
    else
        d[i][j] = INF;
}
// ここで辺のコストを指定
rep(k, 0, N) rep(i, 0, N) rep(j, 0, N) chmin(D[i][j], D[i][k] + D[k][j]);



*/



int N, M;
vector<int> E[101010];
int Q;
HLDecomposition hld;
map<int, map<int, int>> D;
//---------------------------------------------------------------------------------------------------
vector<pair<int, int>> unused;
bool vis[101010];
void dfs(int cu, int pa = -1) {
    vis[cu] = true;
    fore(to, E[cu]) if (to != pa) {
        if (vis[to]) {
            unused.push_back({ cu, to });
            continue;
        }

        hld.add(cu, to);
        dfs(to, cu);
    }
}
//---------------------------------------------------------------------------------------------------
void _main() {
    cin >> N >> M;
    rep(i, 0, M) {
        int a, b; cin >> a >> b;
        a--; b--;
        E[a].push_back(b);
        E[b].push_back(a);
    }

    hld.init(N);
    dfs(0);
    hld.build(0);

    vector<int> targets;
    fore(p, unused) {
        targets.push_back(p.first);
        targets.push_back(p.second);
    }
    sort(all(targets));
    targets.erase(unique(all(targets)), targets.end());

    fore(x, targets) fore(y, targets) D[x][y] = hld.distance(x, y);
    fore(p, unused) D[p.first][p.second] = D[p.second][p.first] = 1;
    fore(k, targets) fore(i, targets) fore(j, targets) chmin(D[i][j], D[i][k] + D[k][j]);

    cin >> Q;
    rep(i, 0, Q) {
        int u, v; cin >> u >> v;
        u--; v--;

        int ans = hld.distance(u, v);
        fore(x, targets) fore(y, targets) chmin(ans, hld.distance(u, x) + D[x][y] + hld.distance(y, v));
        printf("%d\n", ans);
    }
}





提出情報

提出日時
問題 O - 最短距離クエリ
ユーザ hamayanhamayan
言語 C++ (GCC 9.2.1)
得点 6
コード長 5725 Byte
結果 AC
実行時間 391 ms
メモリ 36904 KiB

コンパイルエラー

./Main.cpp: In member function ‘void HLDecomposition::foreach(int, int, std::function<void(int, int)>)’:
./Main.cpp:52:9: warning: this ‘if’ clause does not guard... [-Wmisleading-indentation]
   52 |         if (vid[u] > vid[v]) swap(u, v); f(max(vid[head[v]], vid[u]), vid[v]);
      |         ^~
./Main.cpp:52:42: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the ‘if’
   52 |         if (vid[u] > vid[v]) swap(u, v); f(max(vid[head[v]], vid[u]), vid[v]);
      |                                          ^
./Main.cpp: In member function ‘int HLDecomposition::lca(int, int)’:
./Main.cpp:64:9: warning: this ‘if’ clause does not guard... [-Wmisleading-indentation]
   64 |         if (vid[u] > vid[v]) swap(u, v); if (head[u] == head[v]) return u;
      |         ^~
./Main.cpp:64:42: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the ‘if’
   64 |         if (vid[u] > vid[v]) swap(u, v); if (head[u] == head[v]) return u;
      |                                          ^~

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 6 / 6
結果
AC × 3
AC × 50
セット名 テストケース
Sample 01_sample.txt, 02_sample.txt, 03_sample.txt
All 01_sample.txt, 02_sample.txt, 03_sample.txt, 04_hand.txt, 05_hand.txt, 06_hand.txt, 07_small.txt, 08_small.txt, 09_small.txt, 10_small.txt, 11_small.txt, 12_small.txt, 13_small.txt, 14_small.txt, 15_small.txt, 16_small.txt, 17_small.txt, 18_small.txt, 19_small.txt, 20_small.txt, 21_small.txt, 22_small.txt, 23_small.txt, 24_small.txt, 25_small.txt, 26_small.txt, 27_large.txt, 28_large.txt, 29_large.txt, 30_large.txt, 31_large.txt, 32_large.txt, 33_pathlike.txt, 34_pathlike.txt, 35_pathlike.txt, 36_pathlike.txt, 37_pathlike.txt, 38_cycle.txt, 39_cycle.txt, 40_cycle.txt, 41_cycle.txt, 42_cycle.txt, 43_starlike.txt, 44_starlike.txt, 45_starlike.txt, 46_starlike.txt, 47_starlike.txt, 48_max.txt, 49_max.txt, 50_max.txt
ケース名 結果 実行時間 メモリ
01_sample.txt AC 8 ms 6052 KiB
02_sample.txt AC 5 ms 5976 KiB
03_sample.txt AC 8 ms 6004 KiB
04_hand.txt AC 5 ms 6052 KiB
05_hand.txt AC 4 ms 6124 KiB
06_hand.txt AC 3 ms 5948 KiB
07_small.txt AC 3 ms 6004 KiB
08_small.txt AC 8 ms 6084 KiB
09_small.txt AC 5 ms 5980 KiB
10_small.txt AC 6 ms 5912 KiB
11_small.txt AC 6 ms 5952 KiB
12_small.txt AC 5 ms 6004 KiB
13_small.txt AC 5 ms 5916 KiB
14_small.txt AC 9 ms 6012 KiB
15_small.txt AC 5 ms 5920 KiB
16_small.txt AC 5 ms 5920 KiB
17_small.txt AC 6 ms 5960 KiB
18_small.txt AC 6 ms 6064 KiB
19_small.txt AC 6 ms 6060 KiB
20_small.txt AC 5 ms 5928 KiB
21_small.txt AC 7 ms 5964 KiB
22_small.txt AC 5 ms 5984 KiB
23_small.txt AC 5 ms 6016 KiB
24_small.txt AC 8 ms 5984 KiB
25_small.txt AC 7 ms 5956 KiB
26_small.txt AC 5 ms 6012 KiB
27_large.txt AC 58 ms 17428 KiB
28_large.txt AC 45 ms 13368 KiB
29_large.txt AC 12 ms 6340 KiB
30_large.txt AC 85 ms 14096 KiB
31_large.txt AC 177 ms 21824 KiB
32_large.txt AC 292 ms 15636 KiB
33_pathlike.txt AC 117 ms 35972 KiB
34_pathlike.txt AC 113 ms 32784 KiB
35_pathlike.txt AC 112 ms 31728 KiB
36_pathlike.txt AC 175 ms 33104 KiB
37_pathlike.txt AC 365 ms 32788 KiB
38_cycle.txt AC 108 ms 36816 KiB
39_cycle.txt AC 106 ms 36904 KiB
40_cycle.txt AC 116 ms 34440 KiB
41_cycle.txt AC 181 ms 34692 KiB
42_cycle.txt AC 353 ms 34172 KiB
43_starlike.txt AC 101 ms 26400 KiB
44_starlike.txt AC 110 ms 26264 KiB
45_starlike.txt AC 104 ms 26376 KiB
46_starlike.txt AC 111 ms 26376 KiB
47_starlike.txt AC 136 ms 26444 KiB
48_max.txt AC 391 ms 26248 KiB
49_max.txt AC 268 ms 26308 KiB
50_max.txt AC 344 ms 26344 KiB