제출 #284710

#제출 시각아이디문제언어결과실행 시간메모리
284710ScarletSPipes (BOI13_pipes)C++17
37.59 / 100
261 ms131072 KiB
#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,d,n;
ll cur[MAXN],t;
int ans[5*MAXN];
bool done[MAXN];
vector<int> circle;
vector<int> vals;

void dfs(int c)
{
    done[c]=1;
    circle.push_back(c);
    if (d+sz(circle)==n)
        return;
    while (sz(edges[c]))
    {
        if (done[edges[c].top().first])
            edges[c].pop();
        else
        {
            vals.push_back(edges[c].top().second);
            dfs(edges[c].top().first);
        }
    }
}

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0);
    int m,u,v;
    cin>>n>>m;
    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});
        ++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;
        ++d;
        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);
    while (edges[circle.back()].top().first!=circle[0])
        edges[circle.back()].pop();
    vals.push_back(edges[circle.back()].top().second);
    /**for (int i : circle)
        cout<<i<<" ";
    cout<<"\n";**/
    for (int i=0;i<sz(circle);++i)
    {
        if (i&1)
            t-=score[circle[i]];
        else
            t+=score[circle[i]];
    }
    ans[vals.back()]=t/2;
    ans[vals[0]]=score[circle[0]]-ans[vals.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[i]]=score[circle[i]]-ans[vals[i-1]];
    }
    for (int i=0;i<m;++i)
        cout<<ans[i]*2<<"\n";
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...