# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
78929 | Charis02 | Pipes (BOI13_pipes) | C++14 | 1088 ms | 62372 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*
solution is the same as described here: https://boi2013.informatik-olympiade.de/wp-content/uploads/2013/05/pipes-spoiler.pdf
*/
#include<iostream>
#include<stdio.h>
#include<vector>
#include<cmath>
#include<queue>
#include<string.h>
#include<map>
#include<set>
#include<algorithm>
#include<stack>
#define ll long long
#define pi pair < ll,ll >
#define mp(a,b) make_pair(a,b)
#define rep(i,a,b) for(int i = a;i < b;i++)
#define N 100004
#define INF 1e9+7
using namespace std;
ll n,m,ar[N],val[N],deg[N];
bool vis[N],cycle;
vector < vector < pi > > graph(N);
vector < pi > neo;
vector < pi > edges;
void solvetree(ll cur,ll par,ll edge)
{
rep(i,0,graph[cur].size())
{
ll v = graph[cur][i].first;
if(v == par)
continue;
solvetree(v,cur,graph[cur][i].second);
ar[cur] -= val[graph[cur][i].second];
}
// cout << cur << " " << par << " " << edge << " " << ar[cur] <<endl;
val[edge] = ar[cur];
return;
}
int main()
{
ios_base::sync_with_stdio(false);
cin >> n >> m;
rep(i,1,n+1)
{
cin >> ar[i];
}
rep(i,0,m)
{
ll a,b;
cin >> a >> b;
graph[a].push_back(mp(b,i));
graph[b].push_back(mp(a,i));
edges.push_back(mp(a,b));
}
if(m > n)
{
cout << 0 << endl;
return 0;
}
else if(m == n-1)
{
solvetree(1,1,m);
rep(i,0,m)
{
cout << 2*val[i] << endl;
}
}
else
{
stack < pi > s;
rep(i,1,n+1)
{
deg[i] = graph[i].size();
if(deg[i] == 1)
{
vis[i] = true;
s.push(mp(i,graph[i][0].second));
}
}
while(!s.empty())
{
ll cur = s.top().first;
ll e = s.top().second;
s.pop();
val[e] = ar[cur];
rep(i,0,graph[cur].size())
{
ll v = graph[cur][i].first;
if(vis[v])
continue;
ar[v] -= val[e];
deg[v]--;
graph[v].erase(find(graph[v].begin(),graph[v].end(),mp(v,graph[v][i].second)));
if(deg[v] == 1)
{
s.push(mp(v,graph[v][0].second));
}
}
}
ll st = 1;
while(vis[st])
{
st++;
}
ll start = st;
while(!vis[start])
{
rep(i,0,graph[st].size())
{
ll v= graph[st][i].first;
if(!vis[v])
{
neo.push_back(mp(st,graph[st][i].second));
vis[v] = true;
st = v;
break;
}
}
}
/* cout << neo.size() << endl;
rep(i,0,neo.size())
{
cout << neo[i].first << " " << ar[neo[i].first] << endl;
}
*/
if((neo.size())%2==0)
{
cout << 0 << endl;
}
else
{
ll s = 0;
rep(i,0,neo.size())
{
s += ar[neo[i].first]*pow(-1,i%2);
}
val[neo[neo.size()-1].second] = s/2;
val[neo[0].second] = ar[neo[0].first] - s/2;
rep(i,1,neo.size()-1)
{
val[neo[i].second] =ar[neo[i].first] - val[neo[i-1].second];
}
rep(i,0,m)
{
cout << 2*val[i] << endl;
}
}
}
return 0;
}
/*
3 3
1 3 2
1 2
1 3
2 3
4 4
1 3 2 -4
1 2
4 3
2 3
2 4
*/
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |