Submission #731783

# Submission time Handle Problem Language Result Execution time Memory
731783 2023-04-28T01:17:30 Z n0sk1ll Pipes (BOI13_pipes) C++17
100 / 100
166 ms 19872 KB
#include <bits/stdc++.h>

#define FAST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);cerr.tie(0)
#define mp make_pair
#define xx first
#define yy second
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define all(x) x.begin(),x.end()
#define ff(i,a,b) for (int i = a; i < b; i++)
#define fff(i,a,b) for (int i = a; i <= b; i++)
#define bff(i,a,b) for (int i = b-1; i >= a; i--)
#define bfff(i,a,b) for (int i = b; i >= a; i--)

using namespace std;
long double typedef ld;
unsigned int typedef ui;
long long int typedef li;
pair<int,int> typedef pii;
pair<li,li> typedef pli;
pair<ld,ld> typedef pld;
vector<vector<int>> typedef graph;
unsigned long long int typedef ull;
//const int mod = 998244353;
const int mod = 1000000007;







//Note to self: Check for overflow

int c[100005];
graph g(100005);
int deg[100005];
bool vis[100005];

map<pii,int> eds;
int ans[100005];

li tsum=0;
vector<int> kako;
void dfs(int p, int mf)
{
    if (vis[p]) return; vis[p]=1,tsum+=mf*c[p],kako.pb(p);
    for (auto it : g[p]) dfs(it,-mf);
}

int main()
{
    FAST;

    int n,m; cin>>n>>m;
    if (m>n) return cout<<0,0;

    fff(i,1,n) cin>>c[i];
    fff(i,1,m)
    {
        int u,v; cin>>u>>v;
        g[u].pb(v),g[v].pb(u);
        deg[u]++,deg[v]++;
        eds[{min(u,v),max(u,v)}]=i;
    }

    queue<int> bfs;
    fff(i,1,n) if (deg[i]<=1) bfs.push(i);

    int kolko=0;
    while (!bfs.empty())
    {
        int p=bfs.front(); bfs.pop();
        if (vis[p]) continue;
        vis[p]=true;

        for (auto it : g[p]) if (!vis[it])
        {
            ans[eds[{min(p,it),max(p,it)}]]=c[p];
            c[it]-=c[p],c[p]=0;

            deg[it]--,deg[p]--;
            if (deg[it]<=1) bfs.push(it);
            kolko++;
        }
    }

    if (m==kolko)
    {
        fff(i,1,n) if (c[i]) return cout<<0,0;
        fff(i,1,m) cout<<2*ans[i]<<"\n";
        return 0;
    }

    if ((m-kolko)%2==0) return cout<<0,0;

    fff(i,1,n) if (c[i])
    {
        dfs(i,1);
        if (tsum%2==1) return cout<<0,0;

        li x=tsum/2;
        ff(i,0,(int)kako.size())
        {
            int p=kako[i],q=kako[(i+1)%((int)kako.size())];
            x=c[p]-x;
            ans[eds[{min(p,q),max(p,q)}]]=x;
        }

        fff(i,1,m) cout<<2*ans[i]<<"\n";
        return 0;
    }

    fff(i,1,m) cout<<2*ans[i]<<"\n";
}

//Note to self: Check for overflow

Compilation message

pipes.cpp: In function 'void dfs(int, int)':
pipes.cpp:49:5: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
   49 |     if (vis[p]) return; vis[p]=1,tsum+=mf*c[p],kako.pb(p);
      |     ^~
pipes.cpp:49:25: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
   49 |     if (vis[p]) return; vis[p]=1,tsum+=mf*c[p],kako.pb(p);
      |                         ^~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 3 ms 2644 KB Output is correct
3 Correct 3 ms 2772 KB Output is correct
4 Correct 166 ms 15560 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 3 ms 2700 KB Output is correct
8 Correct 2 ms 2644 KB Output is correct
9 Correct 2 ms 2772 KB Output is correct
10 Correct 2 ms 2708 KB Output is correct
11 Correct 2 ms 2712 KB Output is correct
12 Correct 3 ms 2712 KB Output is correct
13 Correct 100 ms 13000 KB Output is correct
14 Correct 127 ms 14884 KB Output is correct
15 Correct 122 ms 15648 KB Output is correct
16 Correct 96 ms 13768 KB Output is correct
17 Correct 126 ms 15600 KB Output is correct
18 Correct 141 ms 15620 KB Output is correct
19 Correct 125 ms 15348 KB Output is correct
20 Correct 2 ms 2644 KB Output is correct
21 Correct 2 ms 2704 KB Output is correct
22 Correct 124 ms 15612 KB Output is correct
23 Correct 109 ms 12852 KB Output is correct
24 Correct 136 ms 15624 KB Output is correct
25 Correct 98 ms 13384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2700 KB Output is correct
2 Correct 3 ms 2772 KB Output is correct
3 Correct 95 ms 14348 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 3 ms 2716 KB Output is correct
7 Correct 2 ms 2700 KB Output is correct
8 Correct 2 ms 2700 KB Output is correct
9 Correct 3 ms 2644 KB Output is correct
10 Correct 2 ms 2644 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 2 ms 2644 KB Output is correct
13 Correct 2 ms 2644 KB Output is correct
14 Correct 2 ms 2696 KB Output is correct
15 Correct 3 ms 2772 KB Output is correct
16 Correct 2 ms 2772 KB Output is correct
17 Correct 2 ms 2772 KB Output is correct
18 Correct 3 ms 2644 KB Output is correct
19 Correct 2 ms 2644 KB Output is correct
20 Correct 2 ms 2644 KB Output is correct
21 Correct 2 ms 2644 KB Output is correct
22 Correct 3 ms 2840 KB Output is correct
23 Correct 92 ms 15764 KB Output is correct
24 Correct 147 ms 18196 KB Output is correct
25 Correct 89 ms 14280 KB Output is correct
26 Correct 2 ms 2644 KB Output is correct
27 Correct 2 ms 2740 KB Output is correct
28 Correct 2 ms 2644 KB Output is correct
29 Correct 2 ms 2644 KB Output is correct
30 Correct 116 ms 17480 KB Output is correct
31 Correct 152 ms 19872 KB Output is correct
32 Correct 133 ms 16432 KB Output is correct
33 Correct 88 ms 15020 KB Output is correct
34 Correct 2 ms 2644 KB Output is correct
35 Correct 2 ms 2708 KB Output is correct
36 Correct 2 ms 2708 KB Output is correct
37 Correct 2 ms 2716 KB Output is correct
38 Correct 117 ms 17828 KB Output is correct
39 Correct 134 ms 16004 KB Output is correct
40 Correct 131 ms 18076 KB Output is correct
41 Correct 83 ms 14816 KB Output is correct
42 Correct 2 ms 2712 KB Output is correct
43 Correct 2 ms 2644 KB Output is correct
44 Correct 2 ms 2644 KB Output is correct
45 Correct 2 ms 2708 KB Output is correct
46 Correct 115 ms 17516 KB Output is correct
47 Correct 121 ms 18040 KB Output is correct
48 Correct 137 ms 19736 KB Output is correct
49 Correct 91 ms 14396 KB Output is correct
50 Correct 2 ms 2712 KB Output is correct
51 Correct 2 ms 2644 KB Output is correct
52 Correct 3 ms 2716 KB Output is correct
53 Correct 2 ms 2712 KB Output is correct
54 Correct 121 ms 17224 KB Output is correct