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 ll long long
const int nx=1e5+5;
int n, m, h[nx], u, v, sz, res[nx], vs[nx], lvl[nx], f;
vector<pair<int, int>> d[nx];
void dfs(int u, int p, int idx)
{
vs[u]=1; lvl[u]=lvl[p]+1;
for (auto [v, id]:d[u])
{
if (v==p) continue;
if (!vs[v]) dfs(v, u, id);
else if ((lvl[u]-lvl[v]+1)%2==0) f=1;
}
if (idx==-1) return;
res[idx]=2*h[u];
h[p]-=h[u];
}
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>n>>m;
for (int i=1; i<=n; i++) cin>>h[i];
for (int i=1; i<=m; i++) cin>>u>>v, d[u].push_back({v, i}), d[v].push_back({u, i});
dfs(1, 1, -1);
if (m>n||f)
{
cout<<0;
return 0;
}
for (int i=1; i<n; i++) cout<<res[i]<<'\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |