Submission #621359

# Submission time Handle Problem Language Result Execution time Memory
621359 2022-08-03T18:02:59 Z 353cerega Pipes (BOI13_pipes) C++17
74.0741 / 100
122 ms 16284 KB
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>


using namespace std;


using ll = long long;
using ld = long double;

#define X first
#define Y second

//const ll mod = 1000000007;
const ll mod = 998244353;





void solve()
{
    ll n, m;
    cin >> n >> m;
    if (m>n)
    {
        cout << 0 << "\n";
        return;
    }
    vector<ll> a(n);
    ll W = 0;
    for (ll i=0;i<n;i++)
    {
        cin >> a[i];
        W += a[i];
    }
    if (W%2!=0)
    {
        cout << 0 << "\n";
        return;
    }
    vector<ll> b(m), d(n);
    vector<pair<ll,ll>> e(m);
    vector<vector<ll>> g(n);
    for (ll i=0;i<m;i++)
    {
        ll u, v;
        cin >> u >> v;
        u--, v--;
        g[u].push_back(i);
        g[v].push_back(i);
        e[i] = {u,v};
        d[u]++, d[v]++;
    }
    vector<ll> was(m), ans(m);
    set<ll> kek;
    for (ll i=0;i<n;i++) if (d[i]==1) kek.insert(i);
    while (kek.size()>0)
    {
        ll u = *kek.begin();
        ll w = a[u];
        for (ll p: g[u])
        {
            if (was[p]==1) continue;
            was[p] = 1;
            d[e[p].X]--;
            if (d[e[p].X]==1) kek.insert(e[p].X);
            if (d[e[p].X]==0) kek.erase(e[p].X);
            d[e[p].Y]--;
            if (d[e[p].Y]==1) kek.insert(e[p].Y);
            if (d[e[p].Y]==0) kek.erase(e[p].Y);
            a[e[p].X] -= w;
            a[e[p].Y] -= w;
            ans[p] = w;
        }
    }
    ll D = 0;
    for (ll i=0;i<n;i++) if (d[i]==2) D++;
    if (D==0)
    {
        for (ll i=0;i<n;i++)
        {
            if (a[i]!=0)
            {
                cout << 0 << "\n";
                return;
            }
        }
        for (ll i=0;i<m;i++) cout << ans[i]*2 << "\n";
        return;
    }
    if (D%2==0)
    {
        cout << 0 << "\n";
        return;
    }
    for (ll st=0;st<n;st++)
    {
        if (d[st]!=2) continue;
        vector<ll> used(n);
        ll u = st;
        vector<ll> A, B;
        ll S = 0;
        for (ll k=0;k<D;k++)
        {
            A.push_back(a[u]);
            S += a[u];
            for (ll p: g[u])
            {
                if (was[p]==1) continue;
                was[p] = 1;
                B.push_back(p);
                u = u^e[p].X^e[p].Y;
                break;
            }
        }
        S /= 2;
        for (ll i=2;i<D;i+=2) S -= A[i];
        ll L = 0, R = D-1;
        ll Q = D;
        for (ll i=0;Q>0;i=(i+2)%D,Q--)
        {
            ans[B[i]] = S;
            L = (L+2)%D;
            S += A[L];
            R = (R+2)%D;
            S -= A[R];
        }
        for (ll i=0;i<m;i++) cout << ans[i] << "\n";
        return;
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    ll T;
    T = 1;
    //cin >> T;
    while (T--) solve();
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 456 KB Output is correct
4 Correct 89 ms 16076 KB Output is correct
5 Correct 0 ms 320 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 2 ms 472 KB Output is correct
12 Correct 3 ms 340 KB Output is correct
13 Correct 109 ms 12856 KB Output is correct
14 Correct 113 ms 15292 KB Output is correct
15 Correct 96 ms 16208 KB Output is correct
16 Correct 84 ms 13724 KB Output is correct
17 Correct 106 ms 16232 KB Output is correct
18 Correct 100 ms 16284 KB Output is correct
19 Correct 88 ms 13528 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 95 ms 16200 KB Output is correct
23 Correct 72 ms 12852 KB Output is correct
24 Correct 97 ms 16216 KB Output is correct
25 Correct 80 ms 13448 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 340 KB Output isn't correct
2 Incorrect 1 ms 468 KB Output isn't correct
3 Correct 66 ms 13892 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Incorrect 1 ms 212 KB Output isn't correct
8 Incorrect 1 ms 324 KB Output isn't correct
9 Correct 1 ms 320 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 320 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Incorrect 1 ms 212 KB Output isn't correct
15 Incorrect 2 ms 460 KB Output isn't correct
16 Incorrect 2 ms 468 KB Output isn't correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Incorrect 1 ms 468 KB Output isn't correct
23 Incorrect 79 ms 13148 KB Output isn't correct
24 Incorrect 85 ms 15420 KB Output isn't correct
25 Correct 75 ms 13892 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 0 ms 340 KB Output is correct
30 Incorrect 73 ms 14760 KB Output isn't correct
31 Incorrect 78 ms 15752 KB Output isn't correct
32 Incorrect 122 ms 15696 KB Output isn't correct
33 Correct 63 ms 14204 KB Output is correct
34 Correct 0 ms 340 KB Output is correct
35 Correct 1 ms 340 KB Output is correct
36 Correct 0 ms 340 KB Output is correct
37 Correct 1 ms 340 KB Output is correct
38 Incorrect 79 ms 15404 KB Output isn't correct
39 Incorrect 114 ms 15920 KB Output isn't correct
40 Incorrect 98 ms 15540 KB Output isn't correct
41 Correct 52 ms 13164 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 1 ms 340 KB Output is correct
45 Correct 1 ms 340 KB Output is correct
46 Incorrect 73 ms 14776 KB Output isn't correct
47 Incorrect 89 ms 15632 KB Output isn't correct
48 Incorrect 80 ms 15840 KB Output isn't correct
49 Correct 85 ms 14776 KB Output is correct
50 Correct 1 ms 336 KB Output is correct
51 Correct 1 ms 328 KB Output is correct
52 Correct 1 ms 340 KB Output is correct
53 Correct 2 ms 340 KB Output is correct
54 Incorrect 92 ms 15316 KB Output isn't correct