답안 #1045089

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1045089 2024-08-05T16:37:18 Z Beerus13 Janjetina (COCI21_janjetina) C++14
110 / 110
248 ms 15816 KB
#include<bits/stdc++.h>
using namespace std;
#define FOR(i, a, b)  for(int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, b, a) for(int i = (b), _a = (a); i >= _a; --i)
#define REP(i, a, b)  for(int i = (a), _b = (b); i < _b;  ++i)
#define REPD(i, b, a) for(int i = (b), _a = (a); i > _a;  --i)
#define MASK(i) (1LL << (i))
#define BIT(mask, i) (((mask) >> (i)) & 1)
#define __builtin_popcount __builtin_popcountll
#define all(val) val.begin(), val.end()
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define sz(v) (int)v.size()
#define fi first
#define se second

mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll Rand(ll l, ll r) {return l + rng() % (r - l + 1);}

template<class X, class Y>
    bool minimize(X &x, const Y &y) {
        if (x > y) {
            x = y;
            return true;
        } else return false;
    }
template<class X, class Y>
    bool maximize(X &x, const Y &y) {
        if (x < y) {
            x = y;
            return true;
        } else return false;
    }
template<class T>
    T Abs(const T &x) {
        return (x < 0 ? -x : x);    
    }

const int mod = 1e9 + 7;
const int N = 1e5 + 5;

int bit[N];

void update(int i, int val) {
    for(; i < N; i += i & -i) bit[i] += val; 
}

int get(int i) {
    int res(0);
    for(; i; i -= i & -i) res += bit[i];
    return res;
}

int n, k;
int sz[N], cnt[N];
bool del[N];
vector<pii> g[N], res;
ll ans;

int dfs_size(int u, int p = 0) {
    sz[u] = 1;
    for(auto [v, w] : g[u]) if(v != p && !del[v]) sz[u] += dfs_size(v, u);
    return sz[u];
}

int find_centroid(int u, int p, int num) {
    for(auto [v, w] : g[u]) if(v != p && !del[v] && sz[v] > num / 2)
        return find_centroid(v, u, num);
    return u;
}

void dfs(int u, int p, int dep, int val) {
    res.emplace_back(val, dep);
    for(auto [v, w] : g[u]) if(v != p && !del[v])
        dfs(v, u, dep + 1, max(val, w));
}

ll compute(vector<pii> &res) {
    sort(all(res));
    ll ans(0);
    for(auto [w, len] : res) {
        if(w >= len) ans += get(w - len);
        update(k + len, 1);
    }
    for(auto [w, len] : res) {
        update(k + len, -1);
    }
    return ans;
}

void decomposition(int u) {
    u = find_centroid(u, 0, dfs_size(u));
    del[u] = 1;
    res.clear();
    dfs(u, 0, 0, 0);
    ans += compute(res);
    for(auto [v, w] : g[u]) if(!del[v]) {
        res.clear();
        dfs(v, u, 1, w);
        ans -= compute(res);
    }
    for(auto [v, w] : g[u]) if(!del[v])
        decomposition(v);
}

void process() {
    cin >> n >> k;
    REP(i, 1, n) {
        int u, v, w; cin >> u >> v >> w;
        g[u].emplace_back(v, w);
        g[v].emplace_back(u, w);
    }
    decomposition(1);
    cout << ans * 2 << '\n';
}

signed main() {
    if(fopen("COCI21_janjetina.inp","r")) {
        freopen("COCI21_janjetina.inp","r",stdin);
        freopen("COCI21_janjetina.out","w",stdout);
    }
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    // clock_t start = clock();

    int ntests = 1;
    // cin >> ntests;
    while(ntests--) process();

    // cerr << "Time elapsed: " << clock() - start << " ms!\n";
    return 0;
}
// https://oj.uz/problem/view/COCI21_janjetina

Compilation message

Main.cpp: In function 'int dfs_size(int, int)':
Main.cpp:64:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   64 |     for(auto [v, w] : g[u]) if(v != p && !del[v]) sz[u] += dfs_size(v, u);
      |              ^
Main.cpp: In function 'int find_centroid(int, int, int)':
Main.cpp:69:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   69 |     for(auto [v, w] : g[u]) if(v != p && !del[v] && sz[v] > num / 2)
      |              ^
Main.cpp: In function 'void dfs(int, int, int, int)':
Main.cpp:76:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   76 |     for(auto [v, w] : g[u]) if(v != p && !del[v])
      |              ^
Main.cpp: In function 'long long int compute(std::vector<std::pair<int, int> >&)':
Main.cpp:83:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   83 |     for(auto [w, len] : res) {
      |              ^
Main.cpp:87:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   87 |     for(auto [w, len] : res) {
      |              ^
Main.cpp: In function 'void decomposition(int)':
Main.cpp:99:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   99 |     for(auto [v, w] : g[u]) if(!del[v]) {
      |              ^
Main.cpp:104:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  104 |     for(auto [v, w] : g[u]) if(!del[v])
      |              ^
Main.cpp: In function 'int main()':
Main.cpp:121:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  121 |         freopen("COCI21_janjetina.inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:122:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  122 |         freopen("COCI21_janjetina.out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2652 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 2 ms 2840 KB Output is correct
5 Correct 2 ms 2908 KB Output is correct
6 Correct 2 ms 2908 KB Output is correct
7 Correct 2 ms 2908 KB Output is correct
8 Correct 2 ms 2908 KB Output is correct
9 Correct 1 ms 2908 KB Output is correct
10 Correct 1 ms 2908 KB Output is correct
11 Correct 1 ms 2908 KB Output is correct
12 Correct 2 ms 2908 KB Output is correct
13 Correct 2 ms 2908 KB Output is correct
14 Correct 1 ms 2908 KB Output is correct
15 Correct 2 ms 2908 KB Output is correct
16 Correct 2 ms 2908 KB Output is correct
17 Correct 2 ms 2908 KB Output is correct
18 Correct 2 ms 2908 KB Output is correct
19 Correct 1 ms 2908 KB Output is correct
20 Correct 1 ms 2908 KB Output is correct
21 Correct 2 ms 2908 KB Output is correct
22 Correct 2 ms 2908 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2652 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 0 ms 2652 KB Output is correct
4 Correct 2 ms 2908 KB Output is correct
5 Correct 14 ms 4188 KB Output is correct
6 Correct 98 ms 9200 KB Output is correct
7 Correct 113 ms 15072 KB Output is correct
8 Correct 231 ms 15816 KB Output is correct
9 Correct 130 ms 15052 KB Output is correct
10 Correct 241 ms 15484 KB Output is correct
11 Correct 156 ms 15060 KB Output is correct
12 Correct 172 ms 15580 KB Output is correct
13 Correct 176 ms 15164 KB Output is correct
14 Correct 205 ms 15504 KB Output is correct
15 Correct 244 ms 15308 KB Output is correct
16 Correct 200 ms 15464 KB Output is correct
17 Correct 185 ms 15308 KB Output is correct
18 Correct 184 ms 15420 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2652 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 2 ms 2840 KB Output is correct
5 Correct 2 ms 2908 KB Output is correct
6 Correct 2 ms 2908 KB Output is correct
7 Correct 2 ms 2908 KB Output is correct
8 Correct 2 ms 2908 KB Output is correct
9 Correct 1 ms 2908 KB Output is correct
10 Correct 1 ms 2908 KB Output is correct
11 Correct 1 ms 2908 KB Output is correct
12 Correct 2 ms 2908 KB Output is correct
13 Correct 2 ms 2908 KB Output is correct
14 Correct 1 ms 2908 KB Output is correct
15 Correct 2 ms 2908 KB Output is correct
16 Correct 2 ms 2908 KB Output is correct
17 Correct 2 ms 2908 KB Output is correct
18 Correct 2 ms 2908 KB Output is correct
19 Correct 1 ms 2908 KB Output is correct
20 Correct 1 ms 2908 KB Output is correct
21 Correct 2 ms 2908 KB Output is correct
22 Correct 2 ms 2908 KB Output is correct
23 Correct 1 ms 2652 KB Output is correct
24 Correct 1 ms 2652 KB Output is correct
25 Correct 0 ms 2652 KB Output is correct
26 Correct 2 ms 2908 KB Output is correct
27 Correct 14 ms 4188 KB Output is correct
28 Correct 98 ms 9200 KB Output is correct
29 Correct 113 ms 15072 KB Output is correct
30 Correct 231 ms 15816 KB Output is correct
31 Correct 130 ms 15052 KB Output is correct
32 Correct 241 ms 15484 KB Output is correct
33 Correct 156 ms 15060 KB Output is correct
34 Correct 172 ms 15580 KB Output is correct
35 Correct 176 ms 15164 KB Output is correct
36 Correct 205 ms 15504 KB Output is correct
37 Correct 244 ms 15308 KB Output is correct
38 Correct 200 ms 15464 KB Output is correct
39 Correct 185 ms 15308 KB Output is correct
40 Correct 184 ms 15420 KB Output is correct
41 Correct 1 ms 2652 KB Output is correct
42 Correct 157 ms 14872 KB Output is correct
43 Correct 225 ms 15536 KB Output is correct
44 Correct 125 ms 15052 KB Output is correct
45 Correct 221 ms 15308 KB Output is correct
46 Correct 149 ms 15060 KB Output is correct
47 Correct 189 ms 15524 KB Output is correct
48 Correct 199 ms 15052 KB Output is correct
49 Correct 248 ms 15312 KB Output is correct
50 Correct 205 ms 15356 KB Output is correct
51 Correct 179 ms 15476 KB Output is correct
52 Correct 80 ms 9676 KB Output is correct
53 Correct 68 ms 10188 KB Output is correct
54 Correct 57 ms 9740 KB Output is correct
55 Correct 71 ms 10196 KB Output is correct
56 Correct 91 ms 10184 KB Output is correct
57 Correct 173 ms 9436 KB Output is correct
58 Correct 166 ms 9692 KB Output is correct
59 Correct 162 ms 9744 KB Output is correct
60 Correct 185 ms 9748 KB Output is correct
61 Correct 170 ms 9692 KB Output is correct
62 Correct 121 ms 9428 KB Output is correct
63 Correct 156 ms 9800 KB Output is correct
64 Correct 168 ms 9924 KB Output is correct
65 Correct 6 ms 3164 KB Output is correct
66 Correct 1 ms 2652 KB Output is correct