Submission #285937

# Submission time Handle Problem Language Result Execution time Memory
285937 2020-08-29T18:37:37 Z ScarletS Pipes (BOI13_pipes) C++17
100 / 100
260 ms 23208 KB
#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,o;
vector<pii> edges[MAXN];
int scores[MAXN], cur[MAXN], ans[5*MAXN], counter[MAXN];
bool done[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<<0;
        return;
    }
    for (int i=0;i<m;++i)
        cout<<ans[i]*2<<"\n";
} 
 
vector<int> circle;
vector<int> vals;
 
void dfs(int c, int p)
{
    circle.push_back(c);
    done[c]=1;
    for (pii i : edges[c])
        if (i.f!=p)
        {
            if (i.f==o)
            {
                vals.push_back(i.s);
                return;
            }
            if (!done[i.f])
            {
                vals.push_back(i.s);
                dfs(i.f,c);
            }
        }
}
 
int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0);
    int u,v,x=0;
    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<<0;
        return 0;
    }
    stack<int> st;
    for (int i=1;i<=n;++i)
    {
        counter[i]=sz(edges[i]);
        if (counter[i]==1)
            st.push(i);
    }
    while (sz(st))
    {
        ++x;
        v=st.top();st.pop();
        done[v]=1;
        for (pii i : edges[v])
            if (!done[i.f])
            {
                u=i.s;
                ans[u]=scores[v]-cur[v];
                cur[v]+=ans[u];
                cur[i.f]+=ans[u];
                --counter[i.f];
                if (counter[i.f]==1)
                    st.push(i.f);
            }
    }
    if ((n-x+1)&1)
    {
        cout<<0;
        return 0;
    }
    for (int i=1;i<=n;++i)
        if (!done[i])
        {
            o=i;
            dfs(o,0);
            break;
        }
    ll tot=0;
    for (int i=0;i<n-x;++i)
    {
        if (i&1)
            tot-=(scores[circle[i]]-cur[circle[i]]);
        else
            tot+=(scores[circle[i]]-cur[circle[i]]);
    }
    if (tot&1)
    {
        cout<<0;
        return 0;
    }
    ans[vals.back()]=(tot>>1);
    ans[vals[0]]=scores[circle[0]]-cur[circle[0]]-ans[vals.back()];
    for (int i=1;i+1<n-x;++i)
        ans[vals[i]]=scores[circle[i]]-cur[circle[i]]-ans[vals[i-1]];
    for (int i=0;i<m;++i)
        cout<<ans[i]*2<<"\n";
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2688 KB Output is correct
2 Correct 2 ms 2688 KB Output is correct
3 Correct 3 ms 2816 KB Output is correct
4 Correct 66 ms 9592 KB Output is correct
5 Correct 3 ms 2688 KB Output is correct
6 Correct 2 ms 2688 KB Output is correct
7 Correct 2 ms 2688 KB Output is correct
8 Correct 2 ms 2688 KB Output is correct
9 Correct 3 ms 2816 KB Output is correct
10 Correct 3 ms 2816 KB Output is correct
11 Correct 3 ms 2816 KB Output is correct
12 Correct 3 ms 2816 KB Output is correct
13 Correct 57 ms 8160 KB Output is correct
14 Correct 76 ms 9376 KB Output is correct
15 Correct 80 ms 9628 KB Output is correct
16 Correct 62 ms 8568 KB Output is correct
17 Correct 73 ms 9592 KB Output is correct
18 Correct 79 ms 9592 KB Output is correct
19 Correct 89 ms 12920 KB Output is correct
20 Correct 2 ms 2744 KB Output is correct
21 Correct 3 ms 2816 KB Output is correct
22 Correct 83 ms 9652 KB Output is correct
23 Correct 58 ms 8144 KB Output is correct
24 Correct 71 ms 9596 KB Output is correct
25 Correct 81 ms 8440 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2688 KB Output is correct
2 Correct 3 ms 2816 KB Output is correct
3 Correct 71 ms 9184 KB Output is correct
4 Correct 55 ms 7932 KB Output is correct
5 Correct 47 ms 8056 KB Output is correct
6 Correct 255 ms 23124 KB Output is correct
7 Correct 2 ms 2688 KB Output is correct
8 Correct 3 ms 2688 KB Output is correct
9 Correct 2 ms 2816 KB Output is correct
10 Correct 2 ms 2688 KB Output is correct
11 Correct 3 ms 2688 KB Output is correct
12 Correct 2 ms 2688 KB Output is correct
13 Correct 2 ms 2688 KB Output is correct
14 Correct 2 ms 2688 KB Output is correct
15 Correct 3 ms 2816 KB Output is correct
16 Correct 3 ms 2816 KB Output is correct
17 Correct 3 ms 2816 KB Output is correct
18 Correct 3 ms 2688 KB Output is correct
19 Correct 3 ms 2688 KB Output is correct
20 Correct 2 ms 2688 KB Output is correct
21 Correct 3 ms 2816 KB Output is correct
22 Correct 3 ms 2816 KB Output is correct
23 Correct 78 ms 12240 KB Output is correct
24 Correct 85 ms 13648 KB Output is correct
25 Correct 57 ms 9208 KB Output is correct
26 Correct 58 ms 7996 KB Output is correct
27 Correct 66 ms 7800 KB Output is correct
28 Correct 84 ms 8312 KB Output is correct
29 Correct 223 ms 19448 KB Output is correct
30 Correct 100 ms 12744 KB Output is correct
31 Correct 105 ms 15940 KB Output is correct
32 Correct 106 ms 11256 KB Output is correct
33 Correct 64 ms 9464 KB Output is correct
34 Correct 52 ms 7928 KB Output is correct
35 Correct 51 ms 7928 KB Output is correct
36 Correct 55 ms 8184 KB Output is correct
37 Correct 260 ms 23208 KB Output is correct
38 Correct 93 ms 13172 KB Output is correct
39 Correct 78 ms 10872 KB Output is correct
40 Correct 83 ms 13396 KB Output is correct
41 Correct 61 ms 9080 KB Output is correct
42 Correct 54 ms 7800 KB Output is correct
43 Correct 60 ms 7800 KB Output is correct
44 Correct 55 ms 8056 KB Output is correct
45 Correct 193 ms 20376 KB Output is correct
46 Correct 92 ms 12660 KB Output is correct
47 Correct 100 ms 13524 KB Output is correct
48 Correct 105 ms 15700 KB Output is correct
49 Correct 79 ms 9464 KB Output is correct
50 Correct 63 ms 7944 KB Output is correct
51 Correct 58 ms 8136 KB Output is correct
52 Correct 65 ms 7800 KB Output is correct
53 Correct 205 ms 20400 KB Output is correct
54 Correct 96 ms 12368 KB Output is correct