답안 #699695

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
699695 2023-02-17T19:40:50 Z finn__ Pipes (BOI13_pipes) C++17
90.9259 / 100
105 ms 14060 KB
#include <bits/stdc++.h>
using namespace std;

#define N 100000
#define M 500000

vector<pair<unsigned, unsigned>> g[N];
int64_t y[N], ans[M];
bitset<N> visited;

int64_t solve_for_tree(unsigned u, unsigned p1 = -1, unsigned p2 = -1)
{
    int64_t val = 0;

    for (auto const &[v, i] : g[u])
        if (v != p1 && v != p2)
        {
            int64_t z = solve_for_tree(v, u);
            ans[i] = y[v] - z;
            val += ans[i];
        }

    return val;
}

unsigned find_cycle(unsigned u, vector<unsigned> &cycle, unsigned p = -1)
{
    if (visited[u])
        return u;
    visited[u] = 1;
    for (auto const &[v, i] : g[u])
        if (v != p)
        {
            unsigned x = find_cycle(v, cycle, u);
            if (x != UINT_MAX)
            {
                cycle.push_back(u);
                return x == u ? UINT_MAX : x;
            }
        }
    return UINT_MAX;
}

int main()
{
    size_t n, m;
    scanf("%zu %zu", &n, &m);

    if (m > n)
    {
        printf("0\n");
        return 0;
    }

    for (size_t i = 0; i < n; i++)
        scanf("%" PRId64, y + i);
    for (size_t i = 0; i < m; i++)
    {
        unsigned u, v;
        scanf("%u %u", &u, &v);
        g[u - 1].emplace_back(v - 1, i);
        g[v - 1].emplace_back(u - 1, i);
    }

    if (m + 1 == n)
        solve_for_tree(0);
    else
    {
        vector<unsigned> cycle;
        find_cycle(0, cycle);
        vector<int64_t> ny(cycle.size());
        for (size_t i = 0; i < cycle.size(); i++)
            ny[i] = y[cycle[i]] - solve_for_tree(cycle[i], cycle[(i + 1) % cycle.size()], cycle[(i - 1 + cycle.size()) % cycle.size()]);

        unsigned k;
        for (auto const &[v, i] : g[cycle[0]])
            if (v == cycle[1])
            {
                k = i;
                break;
            }

        g[cycle[0]].erase(find(g[cycle[0]].begin(), g[cycle[0]].end(), make_pair(cycle[1], k)));
        g[cycle[1]].erase(find(g[cycle[1]].begin(), g[cycle[1]].end(), make_pair(cycle[0], k)));

        for (size_t z = 0; z < 2; z++)
        {
            ans[k] = 0;
            for (size_t i = 0; i < cycle.size(); i++)
                ans[k] += ny[(i + z) % cycle.size()] * ((i & 1) ? -1 : 1);
            assert(!(ans[k] & 1));
            ans[k] >>= 1;

            y[cycle[0]] -= ans[k];
            y[cycle[1]] -= ans[k];

            if (!solve_for_tree(0))
                break;

            y[cycle[0]] += ans[k];
            y[cycle[1]] += ans[k];
        }
    }

    for (size_t i = 0; i < m; i++)
        printf("%" PRId64 "\n", 2 * ans[i]);
}

Compilation message

pipes.cpp: In function 'int main()':
pipes.cpp:47:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   47 |     scanf("%zu %zu", &n, &m);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~
pipes.cpp:56:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   56 |         scanf("%" PRId64, y + i);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~
pipes.cpp:60:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   60 |         scanf("%u %u", &u, &v);
      |         ~~~~~^~~~~~~~~~~~~~~~~
pipes.cpp:75:18: warning: 'k' may be used uninitialized in this function [-Wmaybe-uninitialized]
   75 |         unsigned k;
      |                  ^
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 81 ms 8268 KB Output is correct
5 Correct 1 ms 2644 KB Output is correct
6 Correct 1 ms 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 1 ms 2644 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 2 ms 2644 KB Output is correct
11 Correct 3 ms 2644 KB Output is correct
12 Correct 2 ms 2716 KB Output is correct
13 Correct 53 ms 7204 KB Output is correct
14 Correct 56 ms 8028 KB Output is correct
15 Correct 62 ms 8296 KB Output is correct
16 Correct 50 ms 7420 KB Output is correct
17 Correct 62 ms 8368 KB Output is correct
18 Correct 57 ms 8352 KB Output is correct
19 Correct 60 ms 10820 KB Output is correct
20 Correct 1 ms 2644 KB Output is correct
21 Correct 3 ms 2596 KB Output is correct
22 Correct 62 ms 8388 KB Output is correct
23 Correct 69 ms 7204 KB Output is correct
24 Correct 62 ms 8328 KB Output is correct
25 Correct 45 ms 7428 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2772 KB Output is correct
3 Incorrect 85 ms 10864 KB Output isn't correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 1 ms 2644 KB Output is correct
6 Correct 1 ms 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 2 ms 2644 KB Output is correct
9 Incorrect 2 ms 2644 KB Output isn't correct
10 Correct 1 ms 2644 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 1 ms 2644 KB Output is correct
13 Correct 1 ms 2644 KB Output is correct
14 Correct 1 ms 2644 KB Output is correct
15 Correct 2 ms 2772 KB Output is correct
16 Correct 2 ms 2644 KB Output is correct
17 Incorrect 2 ms 2644 KB Output isn't correct
18 Correct 1 ms 2644 KB Output is correct
19 Correct 1 ms 2644 KB Output is correct
20 Correct 1 ms 2644 KB Output is correct
21 Correct 1 ms 2644 KB Output is correct
22 Correct 2 ms 2644 KB Output is correct
23 Correct 67 ms 10564 KB Output is correct
24 Correct 94 ms 11688 KB Output is correct
25 Incorrect 86 ms 10740 KB Output isn't correct
26 Correct 1 ms 2644 KB Output is correct
27 Correct 1 ms 2644 KB Output is correct
28 Correct 2 ms 2644 KB Output is correct
29 Correct 1 ms 2644 KB Output is correct
30 Correct 103 ms 13340 KB Output is correct
31 Correct 99 ms 13964 KB Output is correct
32 Correct 68 ms 9492 KB Output is correct
33 Incorrect 75 ms 11820 KB Output isn't correct
34 Correct 1 ms 2644 KB Output is correct
35 Correct 1 ms 2644 KB Output is correct
36 Correct 1 ms 2644 KB Output is correct
37 Correct 1 ms 2644 KB Output is correct
38 Correct 79 ms 12644 KB Output is correct
39 Correct 82 ms 9020 KB Output is correct
40 Correct 86 ms 11388 KB Output is correct
41 Incorrect 100 ms 14060 KB Output isn't correct
42 Correct 1 ms 2644 KB Output is correct
43 Correct 1 ms 2644 KB Output is correct
44 Correct 2 ms 2644 KB Output is correct
45 Correct 1 ms 2644 KB Output is correct
46 Correct 89 ms 13900 KB Output is correct
47 Correct 103 ms 11480 KB Output is correct
48 Correct 105 ms 13792 KB Output is correct
49 Incorrect 65 ms 8928 KB Output isn't correct
50 Correct 1 ms 2644 KB Output is correct
51 Correct 2 ms 2644 KB Output is correct
52 Correct 1 ms 2656 KB Output is correct
53 Correct 1 ms 2644 KB Output is correct
54 Correct 83 ms 12200 KB Output is correct