Submission #1077720

# Submission time Handle Problem Language Result Execution time Memory
1077720 2024-08-27T08:42:22 Z hung_nm Pipes (BOI13_pipes) C++17
65 / 100
113 ms 50812 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[u] = 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 5 ms 24152 KB Output is correct
2 Correct 5 ms 24156 KB Output is correct
3 Correct 5 ms 24240 KB Output is correct
4 Correct 40 ms 31212 KB Output is correct
5 Correct 5 ms 24156 KB Output is correct
6 Correct 7 ms 24208 KB Output is correct
7 Correct 6 ms 24156 KB Output is correct
8 Correct 4 ms 24312 KB Output is correct
9 Correct 5 ms 24156 KB Output is correct
10 Correct 6 ms 24156 KB Output is correct
11 Correct 5 ms 24156 KB Output is correct
12 Correct 6 ms 24280 KB Output is correct
13 Correct 30 ms 29780 KB Output is correct
14 Correct 40 ms 30804 KB Output is correct
15 Correct 36 ms 31324 KB Output is correct
16 Correct 34 ms 30296 KB Output is correct
17 Correct 42 ms 31320 KB Output is correct
18 Correct 37 ms 31312 KB Output is correct
19 Correct 40 ms 35156 KB Output is correct
20 Correct 4 ms 24152 KB Output is correct
21 Correct 4 ms 24156 KB Output is correct
22 Correct 38 ms 31380 KB Output is correct
23 Correct 30 ms 29784 KB Output is correct
24 Correct 47 ms 31312 KB Output is correct
25 Correct 31 ms 30040 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 24152 KB Output isn't correct
2 Incorrect 4 ms 24156 KB Output isn't correct
3 Incorrect 29 ms 29784 KB Output isn't correct
4 Correct 30 ms 30208 KB Output is correct
5 Correct 31 ms 30036 KB Output is correct
6 Correct 112 ms 50776 KB Output is correct
7 Incorrect 5 ms 24152 KB Output isn't correct
8 Incorrect 4 ms 24156 KB Output isn't correct
9 Incorrect 4 ms 24156 KB Output isn't correct
10 Correct 4 ms 24156 KB Output is correct
11 Correct 4 ms 24156 KB Output is correct
12 Correct 4 ms 24260 KB Output is correct
13 Correct 4 ms 24156 KB Output is correct
14 Incorrect 5 ms 24156 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 4 ms 24152 KB Output isn't correct
18 Correct 5 ms 24156 KB Output is correct
19 Correct 4 ms 24372 KB Output is correct
20 Correct 5 ms 24156 KB Output is correct
21 Correct 5 ms 24412 KB Output is correct
22 Incorrect 4 ms 24272 KB Output isn't correct
23 Incorrect 25 ms 29012 KB Output isn't correct
24 Incorrect 29 ms 30040 KB Output isn't correct
25 Incorrect 27 ms 29908 KB Output isn't correct
26 Correct 41 ms 30204 KB Output is correct
27 Correct 31 ms 30032 KB Output is correct
28 Correct 31 ms 30552 KB Output is correct
29 Correct 90 ms 45652 KB Output is correct
30 Incorrect 33 ms 30044 KB Output isn't correct
31 Incorrect 37 ms 30400 KB Output isn't correct
32 Incorrect 29 ms 30168 KB Output isn't correct
33 Incorrect 41 ms 30088 KB Output isn't correct
34 Correct 34 ms 30104 KB Output is correct
35 Correct 35 ms 30036 KB Output is correct
36 Correct 31 ms 30296 KB Output is correct
37 Correct 113 ms 50812 KB Output is correct
38 Incorrect 36 ms 30028 KB Output isn't correct
39 Incorrect 43 ms 30040 KB Output isn't correct
40 Incorrect 34 ms 30040 KB Output isn't correct
41 Incorrect 39 ms 30012 KB Output isn't correct
42 Correct 47 ms 30040 KB Output is correct
43 Correct 37 ms 30032 KB Output is correct
44 Correct 35 ms 30032 KB Output is correct
45 Correct 88 ms 48212 KB Output is correct
46 Incorrect 37 ms 30044 KB Output isn't correct
47 Incorrect 37 ms 30148 KB Output isn't correct
48 Incorrect 43 ms 30032 KB Output isn't correct
49 Incorrect 35 ms 29784 KB Output isn't correct
50 Correct 34 ms 30036 KB Output is correct
51 Correct 44 ms 30036 KB Output is correct
52 Correct 40 ms 30032 KB Output is correct
53 Correct 101 ms 47616 KB Output is correct
54 Incorrect 35 ms 30156 KB Output isn't correct