답안 #877281

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
877281 2023-11-23T05:32:29 Z mgl_diamond Janjetina (COCI21_janjetina) C++17
110 / 110
145 ms 15120 KB
#include <bits/stdc++.h>
using namespace std;

typedef double db;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;

#define lx (id << 1)
#define rx (lx | 1)
#define gcd __gcd
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define bit(i, mask) ((mask) >> (i) & 1)
#define reset(x, val) memset(x, val, sizeof(x))
#define foru(i,a,b) for(int i = (a); i <= (b); ++i)
#define ford(i,a,b) for(int i = (a); i >= (b); --i)
#define FastIO ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);

template<typename T> bool maximize(T &res, const T &val) { if (res < val) { res = val; return true; } return false; }
template<typename T> bool minimize(T &res, const T &val) { if (val < res) { res = val; return true; } return false; }

const ll Linf = 0x3f3f3f3f3f3f3f3f;
const int Inf = 0x3f3f3f3f;
const ll Mod = 1e9 + 7;
const ll Mod2 = ll(1e9) + 9;
const int Lim = 1e6 + 5;
const int inv6 = 166666668;

/// ====*====*====*====*====*====*====*====*====*====*====*====*====*====*====*====*====

const int base = 3;
const int N = 1e5 + 5;
const int K = log2(N) + 1;
const int dx[] = {+1, -1, 0, 0};
const int dy[] = {0, 0, +1, -1};
const int block_size = sqrt(2e9) + 2;

int n, k;
int sz[N], f[N];
vector<pii> adj[N];

struct Fenwick {
    vector<int> f;
    int n = 0;

    Fenwick(int n = 0): n(n) { f.assign(n + 1, 0); }
    void upd(int u, int x) { for (; u <= n; u += u & -u) f[u] += x; }
    int get(int u) { u = min(u, n); int ans = 0; for (; u > 0; u -= u & -u) ans += f[u]; return ans; }
};

int dfs_sz(int u, int p) {
    sz[u] = 1;
    for (auto &[v, w]: adj[u]) if (v != p && !f[v])
        sz[u] += dfs_sz(v, u);
    return sz[u];
}

int find_centroid(int u, int p, int n) {
    for (auto &[v, w]: adj[u]) if (v != p && !f[v])
        if (sz[v] * 2 > n) return find_centroid(v, u, n);
    return u;
}

ll ans;
int nsz;
map<int, Fenwick> dp;

int dist[N], high[N], top[N];
vector<int> prior;

void dfs(int u, int p) {
    if (p) high[u] = high[p] + 1; else high[u] = 0;
    maximize(nsz, high[u]);

    if (p) prior.emplace_back(u);
    for (auto &[v, w]: adj[u]) if (v != p && !f[v]) {
        dist[v] = max(dist[u], w);

        if (p == 0) {
            top[v] = v;
        } else {
            top[v] = top[u];
        }

        dfs(v, u);
    }
}

void solve(int u) {
    dist[u] = 0;
    high[u] = 0;

    nsz = 0;
    prior.clear();
    dfs(u, 0);
    stable_sort(all(prior), [&](int x, int y) {
        return dist[x] < dist[y];
    });

    Fenwick sum(nsz);
    for (auto &u: prior) {
        int w = dist[u];
        if (high[u] == 1) {
            dp[u] = Fenwick(nsz);
        }

        int p = top[u];

        //cout << u << " " << p << " " << w << " " << high[u] << " ";
        if (w - high[u] >= k) {
            ++ans;
            //cout << " + " << 1 << ", ";
        } //cout << " + " << sum.get(w - high[u] - k) - dp[p].get(w - high[u] - k) << "\n";
        
        ans += sum.get(w - high[u] - k) - dp[p].get(w - high[u] - k);

        sum.upd(high[u], 1);
        dp[p].upd(high[u], 1);
    }

    dp.clear();
}

void build(int u) {
    int n = dfs_sz(u, -1);
    u = find_centroid(u, -1, n);

    solve(u);

    f[u] = true;
    for (auto &[v, w]: adj[u]) if (!f[v]) build(v);
    f[u] = false;
}

void solve() {
    cin >> n >> k;
    foru(i, 2, n) {
        int u, v, w;
        cin >> u >> v >> w;

        adj[u].emplace_back(v, w);
        adj[v].emplace_back(u, w);
    }

    build(1);
    cout << ans * 2;
}

signed main() {
    FastIO;

    #define task "HOTPOT"
    if (fopen(task".inp", "r")) {
		freopen(task".inp", "r", stdin);
		freopen(task".out", "w", stdout);
	}

    int ntest = 1;
    // cin >> ntest;
    while (ntest--) {
        //cout << "Case " << q << ": " << "\n"; 
        solve();
        cout << "\n";
    }

    return 0;
}

/**  /\_/\
 *  (= ._.)
 *  / >TL \>AC
**/

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:155:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  155 |   freopen(task".inp", "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:156:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  156 |   freopen(task".out", "w", stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 2 ms 4444 KB Output is correct
5 Correct 2 ms 4444 KB Output is correct
6 Correct 2 ms 4536 KB Output is correct
7 Correct 2 ms 4444 KB Output is correct
8 Correct 2 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4544 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 2 ms 4444 KB Output is correct
13 Correct 2 ms 4444 KB Output is correct
14 Correct 2 ms 4444 KB Output is correct
15 Correct 2 ms 4444 KB Output is correct
16 Correct 2 ms 4444 KB Output is correct
17 Correct 2 ms 4444 KB Output is correct
18 Correct 2 ms 4444 KB Output is correct
19 Correct 2 ms 4444 KB Output is correct
20 Correct 1 ms 4444 KB Output is correct
21 Correct 2 ms 4444 KB Output is correct
22 Correct 2 ms 4444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 2 ms 4444 KB Output is correct
5 Correct 12 ms 5652 KB Output is correct
6 Correct 53 ms 9844 KB Output is correct
7 Correct 96 ms 15116 KB Output is correct
8 Correct 120 ms 14968 KB Output is correct
9 Correct 101 ms 15060 KB Output is correct
10 Correct 126 ms 15112 KB Output is correct
11 Correct 96 ms 14944 KB Output is correct
12 Correct 113 ms 15120 KB Output is correct
13 Correct 102 ms 15120 KB Output is correct
14 Correct 114 ms 15108 KB Output is correct
15 Correct 114 ms 15108 KB Output is correct
16 Correct 115 ms 15116 KB Output is correct
17 Correct 112 ms 15112 KB Output is correct
18 Correct 111 ms 14856 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 2 ms 4444 KB Output is correct
5 Correct 2 ms 4444 KB Output is correct
6 Correct 2 ms 4536 KB Output is correct
7 Correct 2 ms 4444 KB Output is correct
8 Correct 2 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4544 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 2 ms 4444 KB Output is correct
13 Correct 2 ms 4444 KB Output is correct
14 Correct 2 ms 4444 KB Output is correct
15 Correct 2 ms 4444 KB Output is correct
16 Correct 2 ms 4444 KB Output is correct
17 Correct 2 ms 4444 KB Output is correct
18 Correct 2 ms 4444 KB Output is correct
19 Correct 2 ms 4444 KB Output is correct
20 Correct 1 ms 4444 KB Output is correct
21 Correct 2 ms 4444 KB Output is correct
22 Correct 2 ms 4444 KB Output is correct
23 Correct 1 ms 4444 KB Output is correct
24 Correct 1 ms 4444 KB Output is correct
25 Correct 1 ms 4444 KB Output is correct
26 Correct 2 ms 4444 KB Output is correct
27 Correct 12 ms 5652 KB Output is correct
28 Correct 53 ms 9844 KB Output is correct
29 Correct 96 ms 15116 KB Output is correct
30 Correct 120 ms 14968 KB Output is correct
31 Correct 101 ms 15060 KB Output is correct
32 Correct 126 ms 15112 KB Output is correct
33 Correct 96 ms 14944 KB Output is correct
34 Correct 113 ms 15120 KB Output is correct
35 Correct 102 ms 15120 KB Output is correct
36 Correct 114 ms 15108 KB Output is correct
37 Correct 114 ms 15108 KB Output is correct
38 Correct 115 ms 15116 KB Output is correct
39 Correct 112 ms 15112 KB Output is correct
40 Correct 111 ms 14856 KB Output is correct
41 Correct 1 ms 4444 KB Output is correct
42 Correct 97 ms 14936 KB Output is correct
43 Correct 112 ms 15044 KB Output is correct
44 Correct 102 ms 15108 KB Output is correct
45 Correct 109 ms 15116 KB Output is correct
46 Correct 97 ms 15116 KB Output is correct
47 Correct 114 ms 15108 KB Output is correct
48 Correct 103 ms 15108 KB Output is correct
49 Correct 122 ms 14976 KB Output is correct
50 Correct 112 ms 14912 KB Output is correct
51 Correct 111 ms 15112 KB Output is correct
52 Correct 73 ms 9424 KB Output is correct
53 Correct 71 ms 9432 KB Output is correct
54 Correct 53 ms 9572 KB Output is correct
55 Correct 77 ms 9420 KB Output is correct
56 Correct 77 ms 9424 KB Output is correct
57 Correct 143 ms 9016 KB Output is correct
58 Correct 118 ms 9180 KB Output is correct
59 Correct 109 ms 9172 KB Output is correct
60 Correct 135 ms 9180 KB Output is correct
61 Correct 145 ms 9068 KB Output is correct
62 Correct 105 ms 9160 KB Output is correct
63 Correct 121 ms 9164 KB Output is correct
64 Correct 133 ms 9224 KB Output is correct
65 Correct 5 ms 4696 KB Output is correct
66 Correct 1 ms 4444 KB Output is correct