提出 #26687334


ソースコード 拡げる

#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 N, X;
vector<pair<int, int>> E[3010];
//---------------------------------------------------------------------------------------------------
int tot[3010];
void dfs(int cu, int pa = -1) {
    fore(p, E[cu]) if (p.first != pa) {
        tot[p.first] = tot[cu] + p.second;
        dfs(p.first, cu);
    }
}
//---------------------------------------------------------------------------------------------------
void _main() {
    cin >> N >> X;
    HLDecomposition hld;
    hld.init(N);
    rep(i, 0, N - 1) {
        int a, b, c; cin >> a >> b >> c;
        a--; b--;
        hld.add(a, b);
        E[a].push_back({ b, c });
        E[b].push_back({ a, c });
    }
    
    hld.build(0);
    dfs(0);

    rep(a, 0, N) rep(b, a + 1, N) {
        int lc = hld.lca(a, b);
        int len = tot[a] + tot[b] - 2 * tot[lc];
        if (len == X) {
            cout << "Yes" << endl;
            return;
        }
    }

    cout << "No" << endl;
}





提出情報

提出日時
問題 H - 最短経路
ユーザ hamayanhamayan
言語 C++ (GCC 9.2.1)
得点 6
コード長 4911 Byte
結果 AC
実行時間 92 ms
メモリ 4404 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 × 27
セット名 テストケース
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_random_00.txt, 01_random_01.txt, 01_random_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 02_path_00.txt, 02_path_01.txt, 02_path_02.txt, 02_path_03.txt, 02_path_04.txt, 03_star_00.txt, 03_star_01.txt, 03_star_02.txt, 03_star_03.txt, 03_star_04.txt, 04_path_c_max_00.txt, 04_path_c_max_01.txt, 04_path_c_max_02.txt, 04_path_c_max_03.txt
ケース名 結果 実行時間 メモリ
00_sample_00.txt AC 7 ms 3676 KiB
00_sample_01.txt AC 2 ms 3628 KiB
00_sample_02.txt AC 2 ms 3636 KiB
01_random_00.txt AC 92 ms 4176 KiB
01_random_01.txt AC 34 ms 4116 KiB
01_random_02.txt AC 92 ms 4076 KiB
01_random_03.txt AC 14 ms 4112 KiB
01_random_04.txt AC 88 ms 4140 KiB
01_random_05.txt AC 59 ms 4172 KiB
01_random_06.txt AC 89 ms 4112 KiB
01_random_07.txt AC 19 ms 4060 KiB
01_random_08.txt AC 90 ms 4128 KiB
01_random_09.txt AC 75 ms 4184 KiB
02_path_00.txt AC 40 ms 4312 KiB
02_path_01.txt AC 10 ms 4160 KiB
02_path_02.txt AC 39 ms 4184 KiB
02_path_03.txt AC 21 ms 4404 KiB
02_path_04.txt AC 29 ms 4396 KiB
03_star_00.txt AC 29 ms 4064 KiB
03_star_01.txt AC 7 ms 4196 KiB
03_star_02.txt AC 32 ms 4080 KiB
03_star_03.txt AC 8 ms 4204 KiB
03_star_04.txt AC 28 ms 4056 KiB
04_path_c_max_00.txt AC 37 ms 4268 KiB
04_path_c_max_01.txt AC 23 ms 4228 KiB
04_path_c_max_02.txt AC 35 ms 4288 KiB
04_path_c_max_03.txt AC 10 ms 4240 KiB