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>
#define ll long long
#define sz(x) (int)(x).size()
#define pii pair<int,int>
#define f first
#define s second
using namespace std;
const int MAXN=1e5+10;
int n,m;
vector<pii> edges[MAXN];
int scores[MAXN], cur[MAXN], ans[5*MAXN];
void dfsTree(int c, int p, int x)
{
for (pii i : edges[c])
if (i.f!=p)
{
dfsTree(i.f,c,i.s);
cur[c]+=ans[i.s];
}
if (c!=1)
{
ans[x]=scores[c]-cur[c];
cur[c]+=ans[x];
}
}
void treeCase()
{
dfsTree(1,0,0);
if (scores[1]!=cur[1])
{
cout<<"-1\n";
return;
}
for (int i=0;i<m;++i)
cout<<ans[i]*2<<"\n";
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
int u,v;
cin>>n>>m;
for (int i=1;i<=n;++i)
cin>>scores[i];
for (int i=0;i<m;++i)
{
cin>>u>>v;
edges[u].push_back({v,i});
edges[v].push_back({u,i});
}
if (m==n-1)
{
treeCase();
return 0;
}
if (m>n)
{
cout<<"-1";
return 0;
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |