Submission #659224

# Submission time Handle Problem Language Result Execution time Memory
659224 2022-11-17T04:50:03 Z Soul234 Pipes (BOI13_pipes) C++14
87.037 / 100
166 ms 30872 KB
#include<bits/stdc++.h>
using namespace std;

void DBG() { cerr << "]\n"; }
template<class H, class... T> void DBG(H h, T... t) {
    cerr << h; if(sizeof...(t)) cerr << ", ";
    DBG(t...);
}
#ifdef LOCAL
#define dbg(...) cerr << "[" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
#else
#define dbg(...) 0
#endif // LOCAL

#define FOR(i,a,b) for(int i = (a) ; i<(b) ; i++)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for(int i = (b)-1 ; i>=(a) ; i--)
#define R0F(i,a) ROF(i,0,a)
#define each(e,a) for(auto &e : (a))
#define sz(v) (int)(v).size()
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define pb push_back
#define tcT template<class T
#define nl "\n"
#define fi first
#define se second

using ll = long long;
using vi = vector<int>;
using pi = pair<int,int>;
using str = string;
using db = long double;
tcT> using V = vector<T>;
tcT> using pqg = priority_queue<T,vector<T>,greater<T>>;

void setIO(string NAME = "") {
    cin.tie(0)->sync_with_stdio(0);
    if(sz(NAME)) {
        freopen((NAME + ".inp").c_str(),"r",stdin);
        freopen((NAME + ".out").c_str(),"w",stdout);
    }
}

tcT> bool ckmin(T&a, const T&b) {
    return b < a ? a=b,1 : 0; }
tcT> bool ckmax(T&a, const T&b) {
    return b > a ? a=b,1 : 0; }

const int MOD = 1e9 + 7;
const ll INF = 1e18;

const int MX = 5e5+5;

int N, M;

vi adj[MX];

int tin[MX], tt;

int deg[MX], val[MX];

pi eds[MX];
bool used[MX], vis[MX];

int ans[MX];

void dfs(int u, int p) {
    tin[u] = ++tt;
    each(id, adj[u]) if(!used[id]) {
        used[id] = true;
        int v = eds[id].fi ^ eds[id].se ^ u;
        if(tin[v]) {
            if((tin[v]-tin[u])&1) {
                cout << "0\n";
                exit(0);
            }
        }
        else {
            dfs(v, u);
        }
    }
}

vi lst, lstid;

ll sum = 0;

int src;

void dfsList(int u, int p) {
    lst.pb(u);
    vis[u] = 1;
    sum += val[u];
    each(id, adj[u]) {
        int v = u ^ eds[id].fi ^ eds[id].se;
        if(deg[v] == 2 && v != p) {
            if(!vis[v] || v == src) lstid.pb(id);
        }
        if(!vis[v] && deg[v] == 2) {
            dfsList(v, u);
            return;
        }
    }
}

void solve() {
    cin >> N >> M;
    FOR(i,1,N+1) cin >> val[i];
    F0R(i, M) {
        int u, v;
        cin >> u >> v;
        adj[u].pb(i);
        adj[v].pb(i);
        eds[i] = {u, v};
        deg[u]++;
        deg[v]++;
    }
    if(M > N) {
        cout << "0\n";
        return;
    }
    dfs(1, -1);
    queue<int> q;
    FOR(i,1,N+1) if(deg[i] == 1) {
        q.push(i);
    }
    memset(used, false, sizeof used);
    while(sz(q)) {
        int u = q.front(); q.pop();
        each(id, adj[u]) if(!used[id]) {
            used[id] = true;
            int v = eds[id].fi ^ eds[id].se ^ u;
            val[v] -= val[u];
            ans[id] = val[u];
            if(--deg[v] == 1) {
                q.push(v);
            }
        }
    }
    FOR(i,1,N+1) if(deg[i] == 2) {
        src = i;
        dfsList(i, -1);
        break;
    }
    sum /= 2;
    if(!lstid.empty()) {
        for(int i = 2; i<sz(lst); i += 2) {
            sum -= val[lst[i]];
        }
//        each(id, lstid) dbg(id);
//        dbg(sum);
        ans[lstid[0]] = sum;
        for(int i = 1; i<sz(lstid); i++) {
            ans[lstid[i]] = val[lst[i]] - ans[lstid[i-1]];
        }
    }
    F0R(i, M) {
        cout << 2*ans[i] << nl;
    }
}

int main() {
    setIO();

    int t=1;
    //cin>>t;
    while(t-->0) {
        solve();
    }

    return 0;
}

Compilation message

pipes.cpp: In function 'void setIO(std::string)':
pipes.cpp:40:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   40 |         freopen((NAME + ".inp").c_str(),"r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
pipes.cpp:41:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   41 |         freopen((NAME + ".out").c_str(),"w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 7 ms 12500 KB Output is correct
2 Correct 8 ms 12712 KB Output is correct
3 Correct 8 ms 12604 KB Output is correct
4 Correct 78 ms 20300 KB Output is correct
5 Correct 7 ms 12500 KB Output is correct
6 Correct 7 ms 12500 KB Output is correct
7 Correct 7 ms 12500 KB Output is correct
8 Correct 7 ms 12500 KB Output is correct
9 Correct 7 ms 12656 KB Output is correct
10 Correct 8 ms 12628 KB Output is correct
11 Correct 7 ms 12628 KB Output is correct
12 Correct 7 ms 12628 KB Output is correct
13 Correct 51 ms 18764 KB Output is correct
14 Correct 66 ms 19968 KB Output is correct
15 Correct 70 ms 20412 KB Output is correct
16 Correct 57 ms 19132 KB Output is correct
17 Correct 67 ms 20320 KB Output is correct
18 Correct 67 ms 20368 KB Output is correct
19 Correct 87 ms 23832 KB Output is correct
20 Correct 7 ms 12500 KB Output is correct
21 Correct 7 ms 12628 KB Output is correct
22 Correct 71 ms 20388 KB Output is correct
23 Correct 54 ms 18760 KB Output is correct
24 Correct 69 ms 20300 KB Output is correct
25 Correct 60 ms 19064 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 7 ms 11988 KB Output isn't correct
2 Incorrect 7 ms 12116 KB Output isn't correct
3 Correct 47 ms 21948 KB Output is correct
4 Correct 45 ms 18292 KB Output is correct
5 Correct 45 ms 18252 KB Output is correct
6 Correct 166 ms 30872 KB Output is correct
7 Incorrect 6 ms 12072 KB Output isn't correct
8 Correct 7 ms 12584 KB Output is correct
9 Correct 8 ms 12116 KB Output is correct
10 Correct 6 ms 11988 KB Output is correct
11 Correct 6 ms 11988 KB Output is correct
12 Correct 6 ms 12096 KB Output is correct
13 Correct 8 ms 11988 KB Output is correct
14 Correct 7 ms 12560 KB Output is correct
15 Incorrect 6 ms 12200 KB Output isn't correct
16 Correct 7 ms 12580 KB Output is correct
17 Correct 8 ms 12204 KB Output is correct
18 Correct 7 ms 12076 KB Output is correct
19 Correct 7 ms 12072 KB Output is correct
20 Correct 6 ms 12116 KB Output is correct
21 Correct 7 ms 12116 KB Output is correct
22 Correct 7 ms 12716 KB Output is correct
23 Correct 68 ms 23496 KB Output is correct
24 Incorrect 56 ms 22912 KB Output isn't correct
25 Correct 52 ms 22092 KB Output is correct
26 Correct 42 ms 18264 KB Output is correct
27 Correct 43 ms 18208 KB Output is correct
28 Correct 44 ms 18424 KB Output is correct
29 Correct 135 ms 27724 KB Output is correct
30 Incorrect 58 ms 25408 KB Output isn't correct
31 Correct 82 ms 28000 KB Output is correct
32 Correct 72 ms 21928 KB Output is correct
33 Incorrect 83 ms 25112 KB Output isn't correct
34 Correct 42 ms 18328 KB Output is correct
35 Correct 43 ms 18436 KB Output is correct
36 Correct 45 ms 18404 KB Output is correct
37 Correct 159 ms 30868 KB Output is correct
38 Correct 80 ms 26480 KB Output is correct
39 Correct 71 ms 21168 KB Output is correct
40 Incorrect 55 ms 22628 KB Output isn't correct
41 Incorrect 88 ms 27892 KB Output isn't correct
42 Correct 43 ms 18296 KB Output is correct
43 Correct 42 ms 18232 KB Output is correct
44 Correct 43 ms 18252 KB Output is correct
45 Correct 114 ms 27888 KB Output is correct
46 Incorrect 55 ms 26260 KB Output isn't correct
47 Correct 77 ms 24720 KB Output is correct
48 Correct 87 ms 27796 KB Output is correct
49 Correct 45 ms 19660 KB Output is correct
50 Correct 43 ms 18292 KB Output is correct
51 Correct 42 ms 18380 KB Output is correct
52 Correct 42 ms 18156 KB Output is correct
53 Correct 137 ms 28364 KB Output is correct
54 Correct 98 ms 25700 KB Output is correct