This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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] = 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;
}
for (size_t i = 0; i < cycle.size(); i++)
ans[k] += ny[i] * ((i & 1) ? -1 : 1);
ans[k] >>= 1;
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)));
y[cycle[0]] -= ans[k];
y[cycle[1]] -= ans[k];
solve_for_tree(0);
}
for (size_t i = 0; i < m; i++)
printf("%" PRId64 "\n", 2 * ans[i]);
}
Compilation message (stderr)
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:84:16: warning: 'k' may be used uninitialized in this function [-Wmaybe-uninitialized]
84 | ans[k] >>= 1;
| ~~~~~~~^~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |