답안 #1045936

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1045936 2024-08-06T08:37:29 Z CyberCow Petrol stations (CEOI24_stations) C++17
36 / 100
45 ms 15212 KB
#include <random>
#include <algorithm>
#include <bitset>
#include <chrono>
#include <cmath>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <chrono>
#define m_p make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((x).size())
typedef long long ll;
using ull = unsigned long long;
using namespace std;
mt19937 rnd(348502);
ll mod1 = 998244353;
ll mod = 1e9 + 7;
const ll N = 70005;

vector<pair<int, int>> v[N];
ll ans[N], k, sz[N];

void getsz(int g, int p)
{
    sz[g] = 1;
    for (auto to : v[g])
    {
        if (to.first != p)
        {
            getsz(to.first, g);
            sz[g] += sz[to.first];
        }
    }
}

void Dfs(int g, int p, int arj)
{
    for (auto to : v[g])
    {
        if (to.first != p)
        {
            if (to.second > arj)
            {
                ans[g]+=sz[to.first];
                Dfs(to.first, g, k - to.second);
            }
            else
            {
                Dfs(to.first, g, arj - to.second);
            }
        }
    }

}

ll lrjutyun[N];
vector<ll> dasav, arjeqner;

void Dfs1(int g, int p)
{
    sz[g]++;
    dasav.push_back(g);
    for (auto to : v[g])
    {
        if (to.first != p)
        {
            arjeqner.push_back(to.second);
            Dfs1(to.first, g);
            sz[g] += sz[to.first];
        }
    }
}

void solve()
{
    int n, i, j, m, x, y, l;
    cin >> n >> k;
    if (n <= 1000)
    {
        for (i = 0; i < n - 1; i++)
        {
            cin >> x >> y >> l;
            v[x].push_back({ y, l });
            v[y].push_back({ x, l });
        }
        for ( i = 0; i < n; i++)
        {
            for ( j = 0; j < n; j++)
            {
                sz[j] = 0;
            }
            getsz(i, -1);
            Dfs(i, -1, k);
        }
        for ( i = 0; i < n; i++)
        {
            cout << ans[i] << '\n';
        }
        return;
    }
    for (i = 0; i < n - 1; i++)
    {
        cin >> x >> y >> l;
        v[x].push_back({ y, l });
        v[y].push_back({ x, l });
        lrjutyun[i] = 1;
    }
    lrjutyun[n - 1] = 1;
    for ( i = 0; i < n; i++)
    {
        if (v[i].size() == 1)
        {
            Dfs1(i, -1);
            break;
        }
    }
    ll her = 0, r = 0;
    for ( i = 0; i < n - 1; i++)
    {
        while (r < arjeqner.size() && her + arjeqner[r] <= k)
        {
            her += arjeqner[r];
            r++;
        }
        if ((r < arjeqner.size() && her + arjeqner[r] > k) || her > k)
        {
            ans[dasav[r]] += lrjutyun[i] * (n - 1 - r);
            lrjutyun[r] += lrjutyun[i];
        }
        her -= arjeqner[i];
    }
    for ( i = 0; i < n; i++)
    {
        lrjutyun[i] = 1;
    }
    reverse(all(dasav));
    reverse(all(arjeqner));
    her = 0;
    r = 0;
    for (i = 0; i < n - 1; i++)
    {
        while (r < arjeqner.size() && her + arjeqner[r] <= k)
        {
            her += arjeqner[r];
            r++;
        }
        if ((r < arjeqner.size() && her + arjeqner[r] > k) || her > k)
        {
            ans[dasav[r]] += lrjutyun[i] * (n - 1 - r);
            lrjutyun[r] += lrjutyun[i];
        }
        her -= arjeqner[i];
    }
    for ( i = 0; i < n; i++)
    {
        cout << ans[i] << '\n';
    }
}


int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    ll tt = 1;
    //cin >> tt;
    while (tt--) {
        solve();
    }
    return 0;
}

Compilation message

Main.cpp: In function 'void solve()':
Main.cpp:131:18: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  131 |         while (r < arjeqner.size() && her + arjeqner[r] <= k)
      |                ~~^~~~~~~~~~~~~~~~~
Main.cpp:136:16: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  136 |         if ((r < arjeqner.size() && her + arjeqner[r] > k) || her > k)
      |              ~~^~~~~~~~~~~~~~~~~
Main.cpp:153:18: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  153 |         while (r < arjeqner.size() && her + arjeqner[r] <= k)
      |                ~~^~~~~~~~~~~~~~~~~
Main.cpp:158:16: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  158 |         if ((r < arjeqner.size() && her + arjeqner[r] > k) || her > k)
      |              ~~^~~~~~~~~~~~~~~~~
Main.cpp:87:18: warning: unused variable 'm' [-Wunused-variable]
   87 |     int n, i, j, m, x, y, l;
      |                  ^
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 3420 KB Output is correct
2 Correct 1 ms 3420 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 3420 KB Output is correct
2 Correct 1 ms 3420 KB Output is correct
3 Correct 9 ms 3420 KB Output is correct
4 Correct 16 ms 3612 KB Output is correct
5 Correct 12 ms 3616 KB Output is correct
6 Correct 23 ms 3680 KB Output is correct
7 Correct 19 ms 3684 KB Output is correct
8 Correct 1 ms 3516 KB Output is correct
9 Correct 11 ms 3612 KB Output is correct
10 Correct 10 ms 3420 KB Output is correct
11 Correct 11 ms 3612 KB Output is correct
12 Correct 15 ms 3420 KB Output is correct
13 Correct 15 ms 3416 KB Output is correct
14 Correct 13 ms 3616 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 3420 KB Output is correct
2 Correct 25 ms 14360 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 3420 KB Output is correct
2 Correct 1 ms 3420 KB Output is correct
3 Correct 1 ms 3420 KB Output is correct
4 Correct 25 ms 14360 KB Output is correct
5 Correct 45 ms 15212 KB Output is correct
6 Correct 39 ms 14736 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 3420 KB Output is correct
2 Correct 1 ms 3420 KB Output is correct
3 Incorrect 21 ms 7872 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 3420 KB Output is correct
2 Correct 1 ms 3420 KB Output is correct
3 Incorrect 21 ms 7872 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 3420 KB Output is correct
2 Correct 1 ms 3420 KB Output is correct
3 Correct 9 ms 3420 KB Output is correct
4 Correct 16 ms 3612 KB Output is correct
5 Correct 12 ms 3616 KB Output is correct
6 Correct 23 ms 3680 KB Output is correct
7 Correct 19 ms 3684 KB Output is correct
8 Correct 1 ms 3516 KB Output is correct
9 Correct 11 ms 3612 KB Output is correct
10 Correct 10 ms 3420 KB Output is correct
11 Correct 11 ms 3612 KB Output is correct
12 Correct 15 ms 3420 KB Output is correct
13 Correct 15 ms 3416 KB Output is correct
14 Correct 13 ms 3616 KB Output is correct
15 Correct 1 ms 3420 KB Output is correct
16 Correct 25 ms 14360 KB Output is correct
17 Correct 45 ms 15212 KB Output is correct
18 Correct 39 ms 14736 KB Output is correct
19 Incorrect 21 ms 7872 KB Output isn't correct
20 Halted 0 ms 0 KB -