Submission #535984

# Submission time Handle Problem Language Result Execution time Memory
535984 2022-03-12T03:14:24 Z Killer2501 Janjetina (COCI21_janjetina) C++14
50 / 110
393 ms 19236 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;
        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:200:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  200 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
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".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 5076 KB Output is correct
6 Correct 4 ms 5076 KB Output is correct
7 Correct 4 ms 5180 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 4 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 5 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 4 ms 5076 KB Output is correct
22 Correct 5 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 5188 KB Output is correct
5 Correct 22 ms 6520 KB Output is correct
6 Correct 143 ms 12136 KB Output is correct
7 Correct 286 ms 19028 KB Output is correct
8 Correct 316 ms 19068 KB Output is correct
9 Correct 292 ms 19176 KB Output is correct
10 Correct 324 ms 19108 KB Output is correct
11 Correct 277 ms 19088 KB Output is correct
12 Correct 339 ms 19016 KB Output is correct
13 Correct 273 ms 19028 KB Output is correct
14 Correct 345 ms 19116 KB Output is correct
15 Correct 324 ms 19092 KB Output is correct
16 Correct 315 ms 19104 KB Output is correct
17 Correct 321 ms 19180 KB Output is correct
18 Correct 325 ms 19020 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 5076 KB Output is correct
6 Correct 4 ms 5076 KB Output is correct
7 Correct 4 ms 5180 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 4 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 5 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 4 ms 5076 KB Output is correct
22 Correct 5 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 5188 KB Output is correct
27 Correct 22 ms 6520 KB Output is correct
28 Correct 143 ms 12136 KB Output is correct
29 Correct 286 ms 19028 KB Output is correct
30 Correct 316 ms 19068 KB Output is correct
31 Correct 292 ms 19176 KB Output is correct
32 Correct 324 ms 19108 KB Output is correct
33 Correct 277 ms 19088 KB Output is correct
34 Correct 339 ms 19016 KB Output is correct
35 Correct 273 ms 19028 KB Output is correct
36 Correct 345 ms 19116 KB Output is correct
37 Correct 324 ms 19092 KB Output is correct
38 Correct 315 ms 19104 KB Output is correct
39 Correct 321 ms 19180 KB Output is correct
40 Correct 325 ms 19020 KB Output is correct
41 Correct 4 ms 4948 KB Output is correct
42 Correct 265 ms 19116 KB Output is correct
43 Correct 311 ms 19220 KB Output is correct
44 Correct 268 ms 19236 KB Output is correct
45 Correct 314 ms 19188 KB Output is correct
46 Correct 255 ms 19016 KB Output is correct
47 Correct 350 ms 19012 KB Output is correct
48 Correct 269 ms 19060 KB Output is correct
49 Correct 303 ms 19052 KB Output is correct
50 Correct 318 ms 19128 KB Output is correct
51 Correct 331 ms 19068 KB Output is correct
52 Correct 173 ms 12636 KB Output is correct
53 Correct 178 ms 12556 KB Output is correct
54 Correct 146 ms 12456 KB Output is correct
55 Correct 185 ms 12416 KB Output is correct
56 Correct 177 ms 12536 KB Output is correct
57 Correct 347 ms 12272 KB Output is correct
58 Correct 393 ms 12340 KB Output is correct
59 Correct 330 ms 12256 KB Output is correct
60 Correct 333 ms 12264 KB Output is correct
61 Correct 353 ms 12220 KB Output is correct
62 Correct 268 ms 12108 KB Output is correct
63 Correct 310 ms 12160 KB Output is correct
64 Correct 324 ms 12184 KB Output is correct
65 Runtime error 9 ms 10580 KB Execution killed with signal 11
66 Halted 0 ms 0 KB -