Submission #1078242

# Submission time Handle Problem Language Result Execution time Memory
1078242 2024-08-27T14:27:32 Z TVSown Pipes (BOI13_pipes) C++17
74.0741 / 100
152 ms 57636 KB
///*** Sown_Vipro ***///
/// ->TUYEN QUOC GIA<- ///

#include<bits/stdc++.h>
using namespace std;
//#pragma GCC optimize ("O3")
//#pragma GCC optimize ("unroll-loops")
//#pragma GCC target("popcnt")
#define F first
#define S second
#define pb push_back
#define pi pair<int, int>
#define pii pair<int, pair<int, int> >
#define all(a) a.begin(), a.end()
#define FOR(i, a, b) for(int i = a; i <= b; ++i)
#define REP(i, a, b) for(int i = a; i >= b; --i)
#define inp(name) if(fopen(name, "r")) freopen(name, "r", stdin);
#define out(name) if(fopen(name, "w")) freopen(name, "w", stdout);
#define szz(s) (s.size())
#define int long long
const int N = 1e6 + 5, MAX = 1e6, oo = 1e9 + 5, MOD = 1e9 + 7;
int n, m;
int ans[N], c[N], deg[N];
vector<pi> e[N];

namespace sub1{
    void dfs(int u, int p){
        for(auto [v, i] : e[u]){
            if(v == p) continue;
            dfs(v, u);
            ans[i] = 2 * c[v];
            c[u] -= c[v];
        }
    }

    void solve(){
        dfs(1, 0);
        FOR(i, 1, m) cout << ans[i] << "\n";
    }
}

namespace sub2{
    int vst[N], root, A, B;
    vector<int> st;
    queue<int> q;
    void dfs(int u, int p, int type){
        st.pb(u);
//        cout << u << " " << c[u] << "\n";
        vst[u] = 1;
        if(type) A += c[u];
        else B += c[u];
        for(auto [v, i] : e[u]){
            if(vst[v]) continue;
            dfs(v, u, type ^ 1);
            break;
        }
    }

    void solve(){
        FOR(u, 1, n){
            if(deg[u] == 1) q.push(u);
        }
        while(szz(q)){
            int u = q.front(); q.pop();
            vst[u] = 1;
            for(auto [v, i] : e[u]){
                if(vst[v]) continue;
                ans[i] += 2 * c[u];
//                c[u] -= c[u];
                c[v] -= c[u];
                --deg[v];
                if(deg[v] == 1){
                    q.push(v);
                }
            }
        }
        int cnt = 0;
        FOR(u, 1, n){
//            cout << c[u] << "\n";
            if(!vst[u]){
//                cout << u << "\n";
                ++cnt; root = u;
            }
        }
        if(cnt % 2 == 0){
            cout << 0; return;
        }
        dfs(root, 0, 1);
        st.pb(root);
        FOR(j, 0, szz(st) - 2){
            int u = st[j];
            for(auto [v, i] : e[u]){
                if(v == st[j + 1]){
//                    cout << u << " " << st[j + 1] << " " << A - B << " " << B - A << "\n";
                    if(i % 2) ans[i] = B - A;
                    else ans[i] = A - B;
                }
            }
        }

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

void solve(){
    cin >> n >> m;
    FOR(u, 1, n) cin >> c[u];
    FOR(i, 1, m){
        int u, v; cin >> u >> v;
        e[u].pb({v, i});
        e[v].pb({u, i});
        ++deg[u];
        ++deg[v];
    }
    if(m == n - 1) sub1::solve();
    else if(m > n) cout << 0;
    else sub2::solve();
}

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    inp("in.txt");
    int t = 1;
//    cin >> t;
    while(t--){
        solve();
    }
}

Compilation message

pipes.cpp: In function 'void sub2::solve()':
pipes.cpp:15:39: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   15 | #define FOR(i, a, b) for(int i = a; i <= b; ++i)
......
   90 |         FOR(j, 0, szz(st) - 2){
      |             ~~~~~~~~~~~~~~~~~          
pipes.cpp:90:9: note: in expansion of macro 'FOR'
   90 |         FOR(j, 0, szz(st) - 2){
      |         ^~~
pipes.cpp: In function 'int main()':
pipes.cpp:17:47: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   17 | #define inp(name) if(fopen(name, "r")) freopen(name, "r", stdin);
      |                                        ~~~~~~~^~~~~~~~~~~~~~~~~~
pipes.cpp:123:5: note: in expansion of macro 'inp'
  123 |     inp("in.txt");
      |     ^~~
# Verdict Execution time Memory Grader output
1 Correct 11 ms 23956 KB Output is correct
2 Correct 13 ms 23984 KB Output is correct
3 Correct 12 ms 23900 KB Output is correct
4 Correct 67 ms 33252 KB Output is correct
5 Correct 13 ms 23956 KB Output is correct
6 Correct 15 ms 23900 KB Output is correct
7 Correct 17 ms 24156 KB Output is correct
8 Correct 16 ms 23956 KB Output is correct
9 Correct 15 ms 23896 KB Output is correct
10 Correct 13 ms 23900 KB Output is correct
11 Correct 14 ms 23820 KB Output is correct
12 Correct 11 ms 23900 KB Output is correct
13 Correct 51 ms 31444 KB Output is correct
14 Correct 70 ms 32744 KB Output is correct
15 Correct 65 ms 33464 KB Output is correct
16 Correct 55 ms 31780 KB Output is correct
17 Correct 57 ms 33364 KB Output is correct
18 Correct 70 ms 33268 KB Output is correct
19 Correct 66 ms 37308 KB Output is correct
20 Correct 14 ms 24248 KB Output is correct
21 Correct 13 ms 23900 KB Output is correct
22 Correct 51 ms 33364 KB Output is correct
23 Correct 61 ms 31480 KB Output is correct
24 Correct 59 ms 33360 KB Output is correct
25 Correct 46 ms 31848 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 14 ms 23952 KB Output isn't correct
2 Incorrect 11 ms 23952 KB Output isn't correct
3 Correct 47 ms 33396 KB Output is correct
4 Correct 46 ms 32080 KB Output is correct
5 Correct 54 ms 31912 KB Output is correct
6 Correct 152 ms 57636 KB Output is correct
7 Incorrect 17 ms 23900 KB Output isn't correct
8 Incorrect 11 ms 23900 KB Output isn't correct
9 Correct 11 ms 24156 KB Output is correct
10 Correct 11 ms 23900 KB Output is correct
11 Correct 17 ms 23924 KB Output is correct
12 Correct 11 ms 23900 KB Output is correct
13 Correct 12 ms 23900 KB Output is correct
14 Incorrect 11 ms 23984 KB Output isn't correct
15 Incorrect 12 ms 23900 KB Output isn't correct
16 Incorrect 12 ms 23900 KB Output isn't correct
17 Correct 12 ms 23900 KB Output is correct
18 Correct 11 ms 23900 KB Output is correct
19 Correct 15 ms 23964 KB Output is correct
20 Correct 15 ms 23812 KB Output is correct
21 Correct 17 ms 24156 KB Output is correct
22 Incorrect 16 ms 23900 KB Output isn't correct
23 Incorrect 51 ms 34508 KB Output isn't correct
24 Incorrect 59 ms 36680 KB Output isn't correct
25 Correct 57 ms 33384 KB Output is correct
26 Correct 42 ms 32092 KB Output is correct
27 Correct 40 ms 32080 KB Output is correct
28 Correct 40 ms 32600 KB Output is correct
29 Correct 116 ms 51048 KB Output is correct
30 Incorrect 57 ms 35808 KB Output isn't correct
31 Incorrect 64 ms 37788 KB Output isn't correct
32 Incorrect 76 ms 35180 KB Output isn't correct
33 Correct 46 ms 33808 KB Output is correct
34 Correct 45 ms 32080 KB Output is correct
35 Correct 41 ms 32228 KB Output is correct
36 Correct 52 ms 32340 KB Output is correct
37 Correct 142 ms 57616 KB Output is correct
38 Incorrect 57 ms 36296 KB Output isn't correct
39 Incorrect 55 ms 34592 KB Output isn't correct
40 Incorrect 80 ms 36300 KB Output isn't correct
41 Correct 47 ms 33688 KB Output is correct
42 Correct 38 ms 32080 KB Output is correct
43 Correct 56 ms 32084 KB Output is correct
44 Correct 44 ms 32128 KB Output is correct
45 Correct 112 ms 53516 KB Output is correct
46 Incorrect 72 ms 35764 KB Output isn't correct
47 Incorrect 59 ms 36272 KB Output isn't correct
48 Incorrect 68 ms 37644 KB Output isn't correct
49 Correct 47 ms 33356 KB Output is correct
50 Correct 47 ms 32084 KB Output is correct
51 Correct 63 ms 32084 KB Output is correct
52 Correct 39 ms 32080 KB Output is correct
53 Correct 113 ms 53148 KB Output is correct
54 Incorrect 73 ms 35688 KB Output isn't correct