Submission #1077743

# Submission time Handle Problem Language Result Execution time Memory
1077743 2024-08-27T08:51:46 Z huyngodzz Pipes (BOI13_pipes) C++14
100 / 100
101 ms 17436 KB
    ///huynhocute123///
#include<bits/stdc++.h>
using namespace std;
#define S second
#define F first
#define pii pair<int,int>
#define piii pair<int,pair<int,int>>
#define pb push_back
#define pi M_PI
#define FOR(i, a, b) for(int i = a; i <= b; ++i)
#define REP(i, a, b) for(int i = b; i >= a; --i)
#define ALL(v) v.begin(),v.end()
#define inp(name) if(fopen(name, "r")) freopen(name, "r", stdin);
#define out(name) if(fopen(name, "w")) freopen(name, "w", stdout);
//random_device rd;
//mt19937 rng(rd());
//#pragma GCC optimize ("O3")
//#pragma GCC optimize ("unroll-loops")
//#pragma GCC target("popcnt")
//#define int long long
const int maxN = 1e5 + 9 ;
const int  modd = 1e9 + 7;
const int base = 2309;
const int MAX = 1e9+9;
void minimize(int &u, int v){
    if(v < u) u = v;
}
void maximize(int &u, int v){
    if(v > u) u = v;
}
int n, k, t, m, res, a[maxN], ans[maxN], deg[maxN];
int l, r;
bool vis[maxN], ok[maxN];
vector<pii> e[maxN];
namespace sub1{
    void dfs(int u ,int p){
        for(auto [x, id] : e[u]){
            if(x == p)continue;
            dfs(x , u);
            ans[id]= a[x];
            a[u] -= a[x];


        }
    }
    void solve(){
        dfs(1, 0);
        FOR(i, 1, m)cout << ans[i] *2 <<'\n';
    }
}
namespace sub2{
    queue<int> q;
    long long T =0;
    int root2, fi;
    void dfs(int u ,int p, int delta){
        T+= a[u] * delta;
        ok[u]= 1;
        for(auto [x, id] : e[u]){
            if(vis[x] || x == p)continue;
            if(ok[x] == 0){
                dfs(x, u , delta* (-1));
            }else{
                ans[fi] = T;
            }

        }
    }
    void dfs2(int u ,int p ,int prevEdge){
        ok[u] =1;
        for(auto [x, id] : e[u]){
            if(x == p|| vis[x] || ok[x])continue;
            ans[id] = 2 *a[u] - ans[ prevEdge];
            dfs2(x, u , id);
        }

    }
    void solve(){
        FOR(i, 1, n){
            if(deg[i] == 1){
                q.push(i);
                    vis[i] =1;
//                    cout << i << " ";
            }
        }
//        cout <<endl;
        while(!q.empty()){
            int u = q.front();
//            cout << u << " ";
            q.pop();
            vis[u] =1;
            for(auto [x, id ] : e[u]){
                if(vis[x])continue;
                ans[id] = 2  *a[u];
//                cout << id << " " << a[u] <<'\n';
                a[x]-=a[u];
//                vis[x] = 1;
                deg[x]--;
                if(deg[x] == 1)q.push(x);

            }


        }
        int cnt =0;
        int root = 0;
        FOR(i, 1, n){
            if(!vis[i]){
                root = i;
                cnt++;
            }
    }
        if(cnt %2 ==0){
            cout << 0;
            return;
        }
        for(auto [x, id] : e[root]){
            if(vis[x] )continue;
            fi = id;
            root2  =x;
            break;
        }
        ok[root] =1;
        dfs(root, root2, 1);
        memset(ok, 0 ,sizeof(ok));
        dfs2(root, root2 , fi);

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

    }
}
void solve(){
    cin >> n >> m;
    FOR(i, 1, n)cin >> a[i];
    FOR(i, 1, m){
        cin >> l >> r;
        e[l].pb({r, i});
        e[r].pb({l, i});
        deg[l]++;
        deg[r]++;
    }
    if(m > n ){
        cout << 0;
        return;
    }
    if(m == n)sub2::solve();
    else sub1::solve();

}
signed main(){
//    freopen("name.inp","r",stdin);
//    freopen("name.out","w",stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    inp("task.inp");
    t = 1;
   // cin >> t;
    while( t-- )solve();

}

Compilation message

pipes.cpp: In function 'void sub1::dfs(int, int)':
pipes.cpp:37:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   37 |         for(auto [x, id] : e[u]){
      |                  ^
pipes.cpp: In function 'void sub2::dfs(int, int, int)':
pipes.cpp:58:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   58 |         for(auto [x, id] : e[u]){
      |                  ^
pipes.cpp: In function 'void sub2::dfs2(int, int, int)':
pipes.cpp:70:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   70 |         for(auto [x, id] : e[u]){
      |                  ^
pipes.cpp: In function 'void sub2::solve()':
pipes.cpp:91:22: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   91 |             for(auto [x, id ] : e[u]){
      |                      ^
pipes.cpp:116:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  116 |         for(auto [x, id] : e[root]){
      |                  ^
pipes.cpp: In function 'int main()':
pipes.cpp:13:47: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   13 | #define inp(name) if(fopen(name, "r")) freopen(name, "r", stdin);
      |                                        ~~~~~~~^~~~~~~~~~~~~~~~~~
pipes.cpp:154:5: note: in expansion of macro 'inp'
  154 |     inp("task.inp");
      |     ^~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2648 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 36 ms 7872 KB Output is correct
5 Correct 1 ms 2648 KB Output is correct
6 Correct 2 ms 2652 KB Output is correct
7 Correct 1 ms 2652 KB Output is correct
8 Correct 2 ms 2652 KB Output is correct
9 Correct 1 ms 2652 KB Output is correct
10 Correct 1 ms 2652 KB Output is correct
11 Correct 2 ms 2652 KB Output is correct
12 Correct 1 ms 2908 KB Output is correct
13 Correct 31 ms 6996 KB Output is correct
14 Correct 31 ms 7772 KB Output is correct
15 Correct 33 ms 8020 KB Output is correct
16 Correct 27 ms 7256 KB Output is correct
17 Correct 33 ms 8020 KB Output is correct
18 Correct 42 ms 8016 KB Output is correct
19 Correct 39 ms 11344 KB Output is correct
20 Correct 1 ms 2648 KB Output is correct
21 Correct 2 ms 2652 KB Output is correct
22 Correct 32 ms 7940 KB Output is correct
23 Correct 27 ms 7000 KB Output is correct
24 Correct 31 ms 8024 KB Output is correct
25 Correct 28 ms 7260 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2908 KB Output is correct
2 Correct 2 ms 2908 KB Output is correct
3 Correct 25 ms 7348 KB Output is correct
4 Correct 23 ms 6792 KB Output is correct
5 Correct 25 ms 7056 KB Output is correct
6 Correct 95 ms 17264 KB Output is correct
7 Correct 1 ms 2908 KB Output is correct
8 Correct 2 ms 2904 KB Output is correct
9 Correct 2 ms 2652 KB Output is correct
10 Correct 1 ms 2652 KB Output is correct
11 Correct 1 ms 2652 KB Output is correct
12 Correct 2 ms 2596 KB Output is correct
13 Correct 1 ms 2652 KB Output is correct
14 Correct 1 ms 2908 KB Output is correct
15 Correct 1 ms 2908 KB Output is correct
16 Correct 1 ms 2908 KB Output is correct
17 Correct 1 ms 2652 KB Output is correct
18 Correct 1 ms 2652 KB Output is correct
19 Correct 2 ms 2652 KB Output is correct
20 Correct 1 ms 2652 KB Output is correct
21 Correct 2 ms 2908 KB Output is correct
22 Correct 2 ms 2904 KB Output is correct
23 Correct 31 ms 10900 KB Output is correct
24 Correct 37 ms 12120 KB Output is correct
25 Correct 29 ms 7220 KB Output is correct
26 Correct 22 ms 6744 KB Output is correct
27 Correct 24 ms 6748 KB Output is correct
28 Correct 25 ms 7004 KB Output is correct
29 Correct 82 ms 14696 KB Output is correct
30 Correct 36 ms 11092 KB Output is correct
31 Correct 40 ms 14684 KB Output is correct
32 Correct 33 ms 9560 KB Output is correct
33 Correct 25 ms 7516 KB Output is correct
34 Correct 26 ms 6748 KB Output is correct
35 Correct 23 ms 6740 KB Output is correct
36 Correct 23 ms 7000 KB Output is correct
37 Correct 101 ms 17436 KB Output is correct
38 Correct 37 ms 11352 KB Output is correct
39 Correct 33 ms 9044 KB Output is correct
40 Correct 37 ms 11732 KB Output is correct
41 Correct 25 ms 7260 KB Output is correct
42 Correct 25 ms 6624 KB Output is correct
43 Correct 23 ms 6492 KB Output is correct
44 Correct 43 ms 6932 KB Output is correct
45 Correct 75 ms 15444 KB Output is correct
46 Correct 39 ms 10884 KB Output is correct
47 Correct 41 ms 11860 KB Output is correct
48 Correct 45 ms 14416 KB Output is correct
49 Correct 27 ms 7448 KB Output is correct
50 Correct 23 ms 6740 KB Output is correct
51 Correct 23 ms 7004 KB Output is correct
52 Correct 27 ms 6792 KB Output is correct
53 Correct 80 ms 15188 KB Output is correct
54 Correct 38 ms 10664 KB Output is correct