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>
using namespace std;
const int MAXN = 100001;
stack<pii> edges[MAXN];
int score[MAXN],amount[MAXN],x;
ll cur[MAXN],t;
int ans[5*MAXN];
bool done[MAXN];
vector<int> circle;
void dfs(int c)
{
done[c]=1;
circle.push_back(c);
while (sz(edges[c]))
{
if (done[edges[c].top().first])
edges[c].pop();
else
dfs(edges[c].top().first);
}
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
int n,m,u,v;
cin>>n>>m;
map<pii,int> vals;
for (int i = 1; i <= n; ++i)
cin>>score[i];
for (int i = 0; i < m; ++i)
{
cin>>u>>v;
edges[u].push({v,i});
edges[v].push({u,i});
vals[{u,v}]=i;
vals[{v,u}]=i;
++amount[u];
++amount[v];
}
if (m>n)
{
cout<<"0\n";
return 0;
}
stack<int> s;
for (int i=1;i<=n;++i)
if (amount[i]==1)
s.push(i);
while (sz(s))
{
++x;
u=s.top();
s.pop();
while (done[edges[u].top().first])
edges[u].pop();
v=edges[u].top().first;
ans[edges[u].top().second]=score[u]-cur[u];
cur[u]+=ans[edges[u].top().second];
cur[v]+=ans[edges[u].top().second];
//ans[edges[u].top().second]<<=1;
}
if (m==n-1)
{
for (int i=0;i<m;++i)
cout<<ans[i]*2<<"\n";
return 0;
}
if ((n-x+1)&1)
{
cout<<"0\n";
return 0;
}
for (int i=1;i<=n;++i)
{
while (sz(edges[u])&&done[edges[u].top().first])
edges[u].pop();
if (sz(edges[u]))
x=u;
}
dfs(x);
/**for (int i : circle)
cout<<i<<" ";
cout<<"\n";**/
for (int i=0;i<sz(circle);++i)
{
//cout<<"nigga\n";
if (i&1)
t-=score[circle[i]];
else
t+=score[circle[i]];
}
//cout<<"t="<<t<<"\n";
ans[vals[{circle[0],circle.back()}]]=t/2;
ans[vals[{circle[0],circle[1]}]]=score[circle[0]]-ans[vals[{circle[0],circle.back()}]];
for (int i=1;i+1<sz(circle);++i)
{
//cout<<"ans[vals[{"<<circle[i]<<","<<circle[i+1]<<"}] = "<<score[circle[i]]<<" - "<<ans[vals[{circle[i],circle[i-1]}]]<<"\n";
ans[vals[{circle[i],circle[i+1]}]]=score[circle[i]]-ans[vals[{circle[i],circle[i-1]}]];
}
for (int i=0;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... |