Submission #535967

# Submission time Handle Problem Language Result Execution time Memory
535967 2022-03-12T02:15:54 Z Killer2501 Janjetina (COCI21_janjetina) C++14
50 / 110
385 ms 20952 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)
    {
        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:199:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  199 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
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".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 4 ms 5168 KB Output is correct
5 Correct 4 ms 5204 KB Output is correct
6 Correct 4 ms 5204 KB Output is correct
7 Correct 4 ms 5204 KB Output is correct
8 Correct 5 ms 5204 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 5088 KB Output is correct
14 Correct 4 ms 5076 KB Output is correct
15 Correct 4 ms 5044 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 5048 KB Output is correct
20 Correct 4 ms 5076 KB Output is correct
21 Correct 5 ms 5048 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 4924 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 4 ms 5176 KB Output is correct
5 Correct 24 ms 6576 KB Output is correct
6 Correct 146 ms 13048 KB Output is correct
7 Correct 288 ms 20432 KB Output is correct
8 Correct 337 ms 20952 KB Output is correct
9 Correct 297 ms 20648 KB Output is correct
10 Correct 344 ms 20776 KB Output is correct
11 Correct 276 ms 20572 KB Output is correct
12 Correct 348 ms 20944 KB Output is correct
13 Correct 291 ms 20496 KB Output is correct
14 Correct 324 ms 20872 KB Output is correct
15 Correct 340 ms 20704 KB Output is correct
16 Correct 331 ms 20952 KB Output is correct
17 Correct 349 ms 20916 KB Output is correct
18 Correct 351 ms 20724 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 5168 KB Output is correct
5 Correct 4 ms 5204 KB Output is correct
6 Correct 4 ms 5204 KB Output is correct
7 Correct 4 ms 5204 KB Output is correct
8 Correct 5 ms 5204 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 5088 KB Output is correct
14 Correct 4 ms 5076 KB Output is correct
15 Correct 4 ms 5044 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 5048 KB Output is correct
20 Correct 4 ms 5076 KB Output is correct
21 Correct 5 ms 5048 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 4924 KB Output is correct
25 Correct 3 ms 4948 KB Output is correct
26 Correct 4 ms 5176 KB Output is correct
27 Correct 24 ms 6576 KB Output is correct
28 Correct 146 ms 13048 KB Output is correct
29 Correct 288 ms 20432 KB Output is correct
30 Correct 337 ms 20952 KB Output is correct
31 Correct 297 ms 20648 KB Output is correct
32 Correct 344 ms 20776 KB Output is correct
33 Correct 276 ms 20572 KB Output is correct
34 Correct 348 ms 20944 KB Output is correct
35 Correct 291 ms 20496 KB Output is correct
36 Correct 324 ms 20872 KB Output is correct
37 Correct 340 ms 20704 KB Output is correct
38 Correct 331 ms 20952 KB Output is correct
39 Correct 349 ms 20916 KB Output is correct
40 Correct 351 ms 20724 KB Output is correct
41 Correct 3 ms 4948 KB Output is correct
42 Correct 277 ms 20488 KB Output is correct
43 Correct 329 ms 20928 KB Output is correct
44 Correct 270 ms 20480 KB Output is correct
45 Correct 325 ms 20844 KB Output is correct
46 Correct 266 ms 20476 KB Output is correct
47 Correct 330 ms 20860 KB Output is correct
48 Correct 282 ms 20524 KB Output is correct
49 Correct 314 ms 20856 KB Output is correct
50 Correct 328 ms 20600 KB Output is correct
51 Correct 326 ms 20784 KB Output is correct
52 Correct 163 ms 13824 KB Output is correct
53 Correct 184 ms 14256 KB Output is correct
54 Correct 195 ms 13684 KB Output is correct
55 Correct 180 ms 14204 KB Output is correct
56 Correct 189 ms 14216 KB Output is correct
57 Correct 346 ms 13704 KB Output is correct
58 Correct 330 ms 13880 KB Output is correct
59 Correct 331 ms 14072 KB Output is correct
60 Correct 344 ms 13880 KB Output is correct
61 Correct 385 ms 14028 KB Output is correct
62 Correct 274 ms 13492 KB Output is correct
63 Correct 298 ms 13960 KB Output is correct
64 Correct 336 ms 13936 KB Output is correct
65 Runtime error 9 ms 10736 KB Execution killed with signal 11
66 Halted 0 ms 0 KB -