Submission #535986

# Submission time Handle Problem Language Result Execution time Memory
535986 2022-03-12T03:18:18 Z Killer2501 Janjetina (COCI21_janjetina) C++14
110 / 110
366 ms 19308 KB
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define ull unsigned long long
#define pb push_back
#define pll pair<ll, ll>
#define pii pair<int, int>
#define fi first
#define se second
using namespace std;
const int N = 2e5+2;
const int M = 52;
const int mod = 1e9+7;
const ll base = 75;
const ll inf = 1e16;
int n, lab[N], t, c[N], m, k, a[N], b[N], d[N], tong;
ll ans;
vector<pii> adj[N];
vector<int> vi, cur;
bool vis[N];
int r, sz;
ll pw(ll k, ll n)
{
    ll total = 1;
    for(; n; n >>= 1)
    {
        if(n&1)total = total * k % mod;
        k = k * k % mod;
    }
    return total;
}
void add(int& x, int y)
{
    x += y;
    if(x >= mod)x -= mod;
}
int findp(int u)
{
    return lab[u] < 0 ? u : lab[u] = findp(lab[u]);
}
bool hop(int u, int v)
{
    u = findp(u);
    v = findp(v);
    if(u == v)return false;
    if(lab[u] > lab[v])swap(u, v);
    lab[u] += lab[v];
    lab[v] = u;
    return true;
}
struct BIT
{
    int n;
    vector<ll> fe;
    BIT(){}
    void init(int _n)
    {
        n = _n;
        fe.resize(n+1, 0);
    }
    void reset()
    {
        fill(fe.begin(), fe.end(), 0);
    }
    void add(int id, int x)
    {
        if(id <= 0)exit(0);
        for(; id <= n; id += id & -id)fe[id] += x;
    }
    ll get(int id)
    {
        ll total = 0;
        id = min(id, n);
        if(id <= 0)return total;
        for(; id; id -= id & -id)total += fe[id];
        return total;
    }
}bit;
void dfs(int u, int p = 0)
{
    d[u] = 1;
    int mx = 0;
    for(pii x: adj[u])
    {
        int v = x.se;
        if(v == p || b[v])continue;
        dfs(v, u);
        d[u] += d[v];
        mx = max(mx, d[v]);
    }
    mx = max(mx, sz-d[u]);
    if(mx*2 <= sz)r = u;
}
bool cmp(const int& x, const int& y)
{
    return (a[x] < a[y]) || (a[x] == a[y] && c[x] < c[y]);
}
void cal(int u, int p = 0)
{
    for(pii v: adj[u])
    {
        if(v.se == p || b[v.se])continue;
        a[v.se] = max(a[u], v.fi);
        c[v.se] = c[u]+1;
        cal(v.se, u);
    }
    cur.pb(u);
}
void fix()
{
    vector<int> res;
    int i = 0, j = 0;
    while(i < (int) cur.size() || j < (int) vi.size())
    {
        if(i == (int) cur.size())
        {
            res.pb(vi[j]);
            ++j;
        }
        else if(j == (int) vi.size() || cmp(cur[i], vi[j]))
        {
            res.pb(cur[i]);
            ++i;
        }
        else
        {
            res.pb(vi[j]);
            ++j;
        }
    }
    swap(res, vi);
    cur.clear();
}
void decom(int u)
{
    int all = sz;
    b[u] = 1;
    //cout << u <<'\n';
    for(pii x: adj[u])
    {
        if(b[x.se])continue;
        a[x.se] = x.fi;
        c[x.se] = 1;
        cal(x.se);
        sort(cur.begin(), cur.end(), cmp);
        for(int v: cur)
        {
            ans -= bit.get(a[v]-c[v]);
            bit.add(c[v], 1);
        }
        for(int v: cur)bit.add(c[v], -1);
        fix();
    }
    for(int v: vi)
    {
        //if(u == 1)cout << a[v] <<" "<<c[v]<<" "<<v<<'\n';
        ans += bit.get(a[v]-c[v]);
        bit.add(c[v], 1);
    }
    for(int v: vi)
    {
        bit.add(c[v], -1);
        if(a[v] >= c[v])++ans;
    }
    //cout << u <<" "<<ans<<'\n';
    vi.clear();
    for(pii x: adj[u])
    {
        int v = x.se;
        if(b[v])continue;
        sz = (d[v] > d[u] ? all - d[u] : d[v]);
        dfs(v);
        decom(r);
    }
}
void sol()
{
    cin >> n >> k;
    for(int i = 1; i < n; i ++)
    {
        int x, y, w;
        cin >> x >> y >> w;
        w -= k;
        adj[x].pb({w, y});
        adj[y].pb({w, x});
    }
    bit.init(n);
    sz = n;
    dfs(1);
    decom(r);
    cout << ans*2;
}
int main()
{
    cin.tie(0);
    cout.tie(0);
    ios_base::sync_with_stdio(0);
    #define task "test"
    if(fopen(task".inp", "r"))
    {
        freopen(task".inp", "r", stdin);
        freopen(task".out", "w", stdout);
    }
    int test = 1;
    //cin >> test;
    while(test -- > 0)sol();
    return 0;
}
/*
1234
21
*/

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:201:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  201 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:202:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  202 |         freopen(task".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 5 ms 5076 KB Output is correct
5 Correct 4 ms 5096 KB Output is correct
6 Correct 4 ms 5076 KB Output is correct
7 Correct 5 ms 5204 KB Output is correct
8 Correct 4 ms 5076 KB Output is correct
9 Correct 4 ms 5076 KB Output is correct
10 Correct 4 ms 5076 KB Output is correct
11 Correct 3 ms 5076 KB Output is correct
12 Correct 4 ms 5076 KB Output is correct
13 Correct 4 ms 5076 KB Output is correct
14 Correct 4 ms 5076 KB Output is correct
15 Correct 4 ms 5076 KB Output is correct
16 Correct 4 ms 5076 KB Output is correct
17 Correct 4 ms 5076 KB Output is correct
18 Correct 4 ms 5076 KB Output is correct
19 Correct 4 ms 5076 KB Output is correct
20 Correct 4 ms 5076 KB Output is correct
21 Correct 5 ms 5100 KB Output is correct
22 Correct 4 ms 5076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 4 ms 5076 KB Output is correct
5 Correct 23 ms 6492 KB Output is correct
6 Correct 137 ms 12112 KB Output is correct
7 Correct 286 ms 19016 KB Output is correct
8 Correct 309 ms 19244 KB Output is correct
9 Correct 266 ms 19020 KB Output is correct
10 Correct 306 ms 19308 KB Output is correct
11 Correct 257 ms 19076 KB Output is correct
12 Correct 302 ms 19040 KB Output is correct
13 Correct 267 ms 19288 KB Output is correct
14 Correct 302 ms 19020 KB Output is correct
15 Correct 320 ms 19136 KB Output is correct
16 Correct 310 ms 19144 KB Output is correct
17 Correct 319 ms 19112 KB Output is correct
18 Correct 312 ms 19160 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 5 ms 5076 KB Output is correct
5 Correct 4 ms 5096 KB Output is correct
6 Correct 4 ms 5076 KB Output is correct
7 Correct 5 ms 5204 KB Output is correct
8 Correct 4 ms 5076 KB Output is correct
9 Correct 4 ms 5076 KB Output is correct
10 Correct 4 ms 5076 KB Output is correct
11 Correct 3 ms 5076 KB Output is correct
12 Correct 4 ms 5076 KB Output is correct
13 Correct 4 ms 5076 KB Output is correct
14 Correct 4 ms 5076 KB Output is correct
15 Correct 4 ms 5076 KB Output is correct
16 Correct 4 ms 5076 KB Output is correct
17 Correct 4 ms 5076 KB Output is correct
18 Correct 4 ms 5076 KB Output is correct
19 Correct 4 ms 5076 KB Output is correct
20 Correct 4 ms 5076 KB Output is correct
21 Correct 5 ms 5100 KB Output is correct
22 Correct 4 ms 5076 KB Output is correct
23 Correct 3 ms 4948 KB Output is correct
24 Correct 3 ms 4948 KB Output is correct
25 Correct 3 ms 4948 KB Output is correct
26 Correct 4 ms 5076 KB Output is correct
27 Correct 23 ms 6492 KB Output is correct
28 Correct 137 ms 12112 KB Output is correct
29 Correct 286 ms 19016 KB Output is correct
30 Correct 309 ms 19244 KB Output is correct
31 Correct 266 ms 19020 KB Output is correct
32 Correct 306 ms 19308 KB Output is correct
33 Correct 257 ms 19076 KB Output is correct
34 Correct 302 ms 19040 KB Output is correct
35 Correct 267 ms 19288 KB Output is correct
36 Correct 302 ms 19020 KB Output is correct
37 Correct 320 ms 19136 KB Output is correct
38 Correct 310 ms 19144 KB Output is correct
39 Correct 319 ms 19112 KB Output is correct
40 Correct 312 ms 19160 KB Output is correct
41 Correct 3 ms 4948 KB Output is correct
42 Correct 263 ms 19020 KB Output is correct
43 Correct 310 ms 19304 KB Output is correct
44 Correct 266 ms 19076 KB Output is correct
45 Correct 336 ms 19148 KB Output is correct
46 Correct 257 ms 19196 KB Output is correct
47 Correct 339 ms 19048 KB Output is correct
48 Correct 264 ms 19080 KB Output is correct
49 Correct 307 ms 19028 KB Output is correct
50 Correct 323 ms 19172 KB Output is correct
51 Correct 313 ms 19292 KB Output is correct
52 Correct 164 ms 12440 KB Output is correct
53 Correct 180 ms 12576 KB Output is correct
54 Correct 161 ms 12480 KB Output is correct
55 Correct 182 ms 12484 KB Output is correct
56 Correct 175 ms 12484 KB Output is correct
57 Correct 311 ms 12236 KB Output is correct
58 Correct 339 ms 12172 KB Output is correct
59 Correct 329 ms 12256 KB Output is correct
60 Correct 352 ms 12236 KB Output is correct
61 Correct 342 ms 12292 KB Output is correct
62 Correct 284 ms 12284 KB Output is correct
63 Correct 293 ms 12124 KB Output is correct
64 Correct 366 ms 12232 KB Output is correct
65 Correct 11 ms 5468 KB Output is correct
66 Correct 3 ms 5044 KB Output is correct