Submission #625953

# Submission time Handle Problem Language Result Execution time Memory
625953 2022-08-11T04:38:29 Z messiuuuuu Paths (RMI21_paths) C++17
100 / 100
285 ms 16868 KB
///
#include<bits/stdc++.h>
#define task "C"
#define ll long long
#define ld long double
#define fi first
#define se second
#define pb push_back
using namespace std;
const int MAXN = 1e5 + 5;
const ll INF = 1e18 + 5;

int n, k;
vector<pair<int, int>> Adj[MAXN];

void Input()
{
    cin >> n >> k;
    for (int i = 1; i < n; i++)
    {
        int u, v, w;
        cin >> u >> v >> w;
        Adj[u].pb({v, w});
        Adj[v].pb({u, w});
    }
}

ll up[MAXN], down[MAXN];

void DFS(int u, int par)
{
    for (auto v : Adj[u])
    {
        if (v.fi == par)
            continue;
        DFS(v.fi, u);
        down[u] = max(down[u], down[v.fi] + v.se);
    }
}

multiset<ll> s1, s2;
ll ans = 0;

void add(ll val)
{
    s1.insert(val);
    ans += val;
    if (s1.size() > k)
    {
        ans -= *s1.begin();
        s2.insert(*s1.begin());
        s1.erase(s1.begin());
    }
}

void del(ll val)
{
    if (s1.find(val) != s1.end())
    {
        ans -= val;
        s1.erase(s1.find(val));
        if (!s2.empty())
        {
            ans += *s2.rbegin();
            s1.insert(*s2.rbegin());
            s2.erase(s2.find(*s2.rbegin()));
        }
    } else
    {
        if (s2.find(val) != s2.end())
            s2.erase(s2.find(val));
    }
}

void DFS1(int u, int par)
{
    ll max1 = -INF, max2 = -INF;
    int v1, v2;
    for (auto v : Adj[u])
    {
        if (v.fi == par)
            continue;
        if (max1 < down[v.fi] + v.se)
        {
            v2 = v1;
            max2 = max1;
            v1 = v.fi;
            max1 = down[v.fi] + v.se;
        } else
            if (max2 < down[v.fi] + v.se)
            {
                v2 = v.fi;
                max2 = down[v.fi] + v.se;
            }
    }
    for (auto v : Adj[u])
    {
        if (v.fi == par)
            continue;
        if (v.fi == v1)
            up[v.fi] = max(up[u], max2);
        else
            up[v.fi] = max(up[u], max1);
        up[v.fi] += v.se;
        DFS1(v.fi, u);
        if (v.fi != v1)
            add(down[v.fi] + v.se);
    }
}

ll res[MAXN];

void DFS_cal(int u, int par)
{
    res[u] = ans;
    for (auto v : Adj[u])
    {
        if (v.fi == par)
            continue;
        del(up[v.fi] - v.se);
        add(up[v.fi]);
        del(down[v.fi] + v.se);
        add(down[v.fi]);
        DFS_cal(v.fi, u);
        add(up[v.fi] - v.se);
        del(up[v.fi]);
        add(down[v.fi] + v.se);
        del(down[v.fi]);
    }
}

void Solve()
{
    DFS(1, 0);
    DFS1(1, 0);
    add(down[1]);
    DFS_cal(1, 0);
    for (int i = 1; i <= n; i++)
        cout << res[i] << '\n';
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    if (fopen(task".INP","r"))
    {
        freopen(task".INP","r",stdin);
        //freopen(task".OUT","w",stdout);
    }
    Input();
    Solve();
}

Compilation message

Main.cpp: In function 'void add(long long int)':
Main.cpp:48:19: warning: comparison of integer expressions of different signedness: 'std::multiset<long long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   48 |     if (s1.size() > k)
      |         ~~~~~~~~~~^~~
Main.cpp: In function 'void DFS1(int, int)':
Main.cpp:78:13: warning: variable 'v2' set but not used [-Wunused-but-set-variable]
   78 |     int v1, v2;
      |             ^~
Main.cpp: In function 'int main()':
Main.cpp:148:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  148 |         freopen(task".INP","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
Main.cpp: In function 'void DFS1(int, int)':
Main.cpp:100:9: warning: 'v1' may be used uninitialized in this function [-Wmaybe-uninitialized]
  100 |         if (v.fi == v1)
      |         ^~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2760 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2760 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2780 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2684 KB Output is correct
7 Correct 3 ms 2644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2760 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2780 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2684 KB Output is correct
7 Correct 3 ms 2644 KB Output is correct
8 Correct 4 ms 3032 KB Output is correct
9 Correct 4 ms 2824 KB Output is correct
10 Correct 3 ms 2776 KB Output is correct
11 Correct 3 ms 2804 KB Output is correct
12 Correct 3 ms 2772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2760 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2780 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2684 KB Output is correct
7 Correct 3 ms 2644 KB Output is correct
8 Correct 4 ms 3032 KB Output is correct
9 Correct 4 ms 2824 KB Output is correct
10 Correct 3 ms 2776 KB Output is correct
11 Correct 3 ms 2804 KB Output is correct
12 Correct 3 ms 2772 KB Output is correct
13 Correct 5 ms 3092 KB Output is correct
14 Correct 4 ms 2900 KB Output is correct
15 Correct 4 ms 2828 KB Output is correct
16 Correct 4 ms 2900 KB Output is correct
17 Correct 4 ms 2856 KB Output is correct
18 Correct 4 ms 2776 KB Output is correct
19 Correct 5 ms 2824 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 217 ms 14588 KB Output is correct
2 Correct 279 ms 16444 KB Output is correct
3 Correct 143 ms 12052 KB Output is correct
4 Correct 205 ms 14288 KB Output is correct
5 Correct 238 ms 15296 KB Output is correct
6 Correct 224 ms 14528 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2760 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2780 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2684 KB Output is correct
7 Correct 3 ms 2644 KB Output is correct
8 Correct 4 ms 3032 KB Output is correct
9 Correct 4 ms 2824 KB Output is correct
10 Correct 3 ms 2776 KB Output is correct
11 Correct 3 ms 2804 KB Output is correct
12 Correct 3 ms 2772 KB Output is correct
13 Correct 5 ms 3092 KB Output is correct
14 Correct 4 ms 2900 KB Output is correct
15 Correct 4 ms 2828 KB Output is correct
16 Correct 4 ms 2900 KB Output is correct
17 Correct 4 ms 2856 KB Output is correct
18 Correct 4 ms 2776 KB Output is correct
19 Correct 5 ms 2824 KB Output is correct
20 Correct 217 ms 14588 KB Output is correct
21 Correct 279 ms 16444 KB Output is correct
22 Correct 143 ms 12052 KB Output is correct
23 Correct 205 ms 14288 KB Output is correct
24 Correct 238 ms 15296 KB Output is correct
25 Correct 224 ms 14528 KB Output is correct
26 Correct 243 ms 14788 KB Output is correct
27 Correct 255 ms 16484 KB Output is correct
28 Correct 249 ms 16772 KB Output is correct
29 Correct 153 ms 12256 KB Output is correct
30 Correct 285 ms 14752 KB Output is correct
31 Correct 250 ms 13120 KB Output is correct
32 Correct 268 ms 15580 KB Output is correct
33 Correct 268 ms 14676 KB Output is correct
34 Correct 142 ms 11912 KB Output is correct
35 Correct 274 ms 14736 KB Output is correct
36 Correct 224 ms 16868 KB Output is correct