#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <queue>
#include <cstdint>
#include <functional>
#define int long long
using namespace std;
int n, m;
set <pair <int, int>> adj[100005];
int c[100005], viz[100005], ans[100005], suf[100005];
vector <int> cycle_edge, cycle;
queue <int> coada;
int32_t main ()
{
ios :: sync_with_stdio (0);
cin.tie (nullptr);
cin >> n >> m;
for (int i=1;i<=n;i++)
{
cin >> c[i];
}
for (int i=1;i<=m;i++)
{
int x, y;
cin >> x >> y;
adj[x].insert ({y, i});
adj[y].insert ({x, i});
}
if (m>n)
{
cout << "0\n";
return 0;
}
for (int i=1;i<=n;i++)
{
if (adj[i].size()<2)
{
coada.push (i);
viz[i]=1;
}
}
int cnt=0;
while (!coada.empty())
{
cnt++;
auto chestie=coada.front();
coada.pop();
auto neighbour=*adj[chestie].begin();
c[neighbour.first]-=c[chestie];
ans[neighbour.second]+=c[chestie];
adj[neighbour.first].erase ({chestie, neighbour.second});
if (adj[neighbour.first].size()<2 && !viz[neighbour.first]) coada.push (neighbour.first), viz[neighbour.first]=1;
}
if ((n-cnt)%2==0)
{
cout << "0\n";
return 0;
}
function <void (int)> dfs = [&] (int nod)
{
cycle.push_back (nod);
for (auto chestie : adj[nod])
{
if (viz[chestie.first])
continue;
viz[chestie.first]=1;
cycle_edge.push_back (chestie.second);
dfs (chestie.first);
}
};
int start=0;
for (int i=1;i<=n;i++)
{
if (!viz[i])
start=i;
}
viz[start]=1;
if (start) dfs (start);
int sz=cycle.size();
for (int i=sz-1;i>=0;i--)
suf[i]=c[cycle[i]]-suf[i+1];
int pre=0;
for (int i=0;i<sz;i++)
{
pre=c[cycle[i]]-pre;
ans[cycle_edge[i]]=(pre+suf[i+1])/2;
}
for (int i=1;i<=m;i++)
{
cout << ans[i]*2 << "\n";
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |