Submission #1077706

# Submission time Handle Problem Language Result Execution time Memory
1077706 2024-08-27T08:40:04 Z hung_nm Pipes (BOI13_pipes) C++17
65 / 100
115 ms 50860 KB
#include <bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define pii pair<int, int>
#define pipii pair<int, pair<int, int>>
#define vi vector<int>
#define vpi vector<pair<int, int>>
#define inp(name) if(fopen(name, "r")) freopen(name, "r", stdin);
#define out(name) if(fopen(name, "w")) freopen(name, "w", stdout);
#define int long long
const int N = 1e6 + 5;
const int mod = 1e9 + 7;
using namespace std;
long long n, m, c[N], ans[N];
vpi ad[N];
void dfs(int u, int p, int id = 0)
{
    long long res = 0;
    for(auto [v, pos] : ad[u])
    {
        if(v == p) continue ;
        dfs(v, u, pos);
        res += ans[pos];
    }
    ans[id] = c[u] - res;
}

void sub1()
{
    dfs(1, 0);
    for(int i = 1; i <= m; ++i)
    {
        cout << 2*ans[i] << "\n";
    }
}

long long del[N], deg[N], ok[N], tmp, x, root, vis[N];
queue<int> q;

void dfs2(int u, int p, int dau)
{
    tmp += dau * c[u];
    ok[u] = 1;
    for(auto [v, i] : ad[u])
    {
        if(v == p or del[v]) continue ;
        if(ok[v] == 0)
        {
            dfs2(v, u, dau * (-1));
        }
        else
        {
            ans[x] = tmp;
            root = v;
            vis[v] = 1;
            break ;
        }
    }
}

void dfs3(int u = root, int p = root, int pos = x)
{
    vis[u] = 1;
    for(auto [v, i] : ad[u])
    {
        if(v == p or del[v] or vis[v]) continue ;
        ans[i] = 2*c[u] - ans[pos];
        dfs3(v, u, i);
    }
}
void sub2()
{
    for(int i = 1; i <= n; ++i)
    {
        if(ad[i].size() == 1)
        {
            q.push(i);
        }
        deg[i] = ad[i].size();
    }

    if( ( n - q.size() ) % 2 == 0 )
    {
        cout << 0;
        return ;
    }

    while(!q.size())
    {
        int u = q.front(); q.pop();
        for(auto [v, i] : ad[u])
        {
            if(del[v]) continue ;
            ans[i] = 2*c[u];
            c[v] -= c[u];
            deg[v]--;
            if(deg[v] == 1)
            {
                q.push(v);
            }
        }
        del[u] = 1;
    }
    long long des;
    for(int i = 1; i <= n; ++i)
    {
        if(del[i] == 0)
        {
            des = i;
            break;
        }
    }
    dfs2(des, des, 1);
    dfs3();
    for(int i = 1; i <= m; ++i)
    {
        cout << ans[i] << "\n";
    }

}
signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(0);
    cin >> n >> m;
    for(int i = 1; i <= n; ++i)
    {
        cin >> c[i];
    }

    for(int i = 1; i <= m; ++i)
    {
        int u, v;
        cin >> u >> v;
        ad[u].pb({v, i});
        ad[v].pb({u, i});
    }
    if(m > n)
    {
        cout << 0;
        return 0;
    }

    if(m == (n-1))
    {
        sub1();
        return 0;
    }


    return 0;
}

Compilation message

pipes.cpp: In function 'void sub2()':
pipes.cpp:114:9: warning: 'des' may be used uninitialized in this function [-Wmaybe-uninitialized]
  114 |     dfs2(des, des, 1);
      |     ~~~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 24152 KB Output is correct
2 Correct 4 ms 24152 KB Output is correct
3 Correct 5 ms 24156 KB Output is correct
4 Correct 42 ms 31320 KB Output is correct
5 Correct 4 ms 24156 KB Output is correct
6 Correct 5 ms 24276 KB Output is correct
7 Correct 5 ms 24156 KB Output is correct
8 Correct 5 ms 24156 KB Output is correct
9 Correct 5 ms 24376 KB Output is correct
10 Correct 5 ms 24156 KB Output is correct
11 Correct 5 ms 24156 KB Output is correct
12 Correct 7 ms 24412 KB Output is correct
13 Correct 40 ms 29860 KB Output is correct
14 Correct 34 ms 30984 KB Output is correct
15 Correct 43 ms 31252 KB Output is correct
16 Correct 34 ms 30292 KB Output is correct
17 Correct 36 ms 31312 KB Output is correct
18 Correct 41 ms 31380 KB Output is correct
19 Correct 47 ms 35164 KB Output is correct
20 Correct 4 ms 24152 KB Output is correct
21 Correct 5 ms 24156 KB Output is correct
22 Correct 43 ms 31416 KB Output is correct
23 Correct 33 ms 29780 KB Output is correct
24 Correct 49 ms 31532 KB Output is correct
25 Correct 35 ms 30152 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 6 ms 24156 KB Output isn't correct
2 Incorrect 5 ms 24156 KB Output isn't correct
3 Incorrect 30 ms 29780 KB Output isn't correct
4 Correct 37 ms 30044 KB Output is correct
5 Correct 30 ms 29816 KB Output is correct
6 Correct 115 ms 50860 KB Output is correct
7 Incorrect 5 ms 24152 KB Output isn't correct
8 Incorrect 4 ms 24152 KB Output isn't correct
9 Incorrect 4 ms 24248 KB Output isn't correct
10 Correct 5 ms 24156 KB Output is correct
11 Correct 3 ms 24156 KB Output is correct
12 Correct 4 ms 24156 KB Output is correct
13 Correct 4 ms 24156 KB Output is correct
14 Incorrect 4 ms 24248 KB Output isn't correct
15 Incorrect 5 ms 24156 KB Output isn't correct
16 Incorrect 5 ms 24156 KB Output isn't correct
17 Incorrect 5 ms 24156 KB Output isn't correct
18 Correct 5 ms 24156 KB Output is correct
19 Correct 4 ms 24152 KB Output is correct
20 Correct 4 ms 24156 KB Output is correct
21 Correct 4 ms 24244 KB Output is correct
22 Incorrect 5 ms 24156 KB Output isn't correct
23 Incorrect 35 ms 29040 KB Output isn't correct
24 Incorrect 34 ms 30236 KB Output isn't correct
25 Incorrect 32 ms 29912 KB Output isn't correct
26 Correct 32 ms 30036 KB Output is correct
27 Correct 30 ms 30032 KB Output is correct
28 Correct 53 ms 30388 KB Output is correct
29 Correct 111 ms 45652 KB Output is correct
30 Incorrect 39 ms 29980 KB Output isn't correct
31 Incorrect 39 ms 30032 KB Output isn't correct
32 Incorrect 33 ms 30160 KB Output isn't correct
33 Incorrect 37 ms 30080 KB Output isn't correct
34 Correct 36 ms 30220 KB Output is correct
35 Correct 37 ms 30284 KB Output is correct
36 Correct 33 ms 30300 KB Output is correct
37 Correct 115 ms 50776 KB Output is correct
38 Incorrect 35 ms 30300 KB Output isn't correct
39 Incorrect 35 ms 30036 KB Output isn't correct
40 Incorrect 61 ms 30036 KB Output isn't correct
41 Incorrect 40 ms 30036 KB Output isn't correct
42 Correct 43 ms 30032 KB Output is correct
43 Correct 53 ms 30032 KB Output is correct
44 Correct 33 ms 30028 KB Output is correct
45 Correct 109 ms 48188 KB Output is correct
46 Incorrect 36 ms 30052 KB Output isn't correct
47 Incorrect 33 ms 30044 KB Output isn't correct
48 Incorrect 44 ms 30036 KB Output isn't correct
49 Incorrect 31 ms 30536 KB Output isn't correct
50 Correct 36 ms 30244 KB Output is correct
51 Correct 33 ms 30140 KB Output is correct
52 Correct 36 ms 30184 KB Output is correct
53 Correct 98 ms 47544 KB Output is correct
54 Incorrect 54 ms 30036 KB Output isn't correct