답안 #855007

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
855007 2023-09-29T18:46:36 Z hariaakas646 Pipes (BOI13_pipes) C++17
100 / 100
87 ms 17064 KB
#include <bits/stdc++.h>

using namespace std;

#define scd(t) scanf("%d", &t)
#define sclld(t) scanf("%lld", &t)
#define forr(i, j, k) for (int i = j; i < k; i++)
#define frange(i, j) forr(i, 0, j)
#define all(cont) cont.begin(), cont.end()
#define mp make_pair
#define pb push_back
#define f first
#define s second
typedef long long int lli;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<lli> vll;
typedef vector<string> vs;
typedef vector<pii> vii;
typedef vector<vi> vvi;
typedef map<int, int> mpii;
typedef set<int> seti;
typedef multiset<int> mseti;
typedef long double ld;

void usaco()
{
    freopen("/media/hariaakash646/785EF1075EF0BF46/CompetitiveProgramming/input.in", "r", stdin);
//    freopen("problem.out", "w", stdout);
}


int main() {
    // usaco();
    int n, m;
    scd(n);
    scd(m);
    vll vec(n+1);

    forr(i, 1, n+1) sclld(vec[i]);
    if(m > n) {
        printf("0");
        return 0;
    }
    vector<set<pii>> graph(n+1);
    vi deg(n+1);
    frange(i, m) {
        int a, b;
        scd(a);
        scd(b);
        graph[a].insert(mp(b, i));
        graph[b].insert(mp(a, i));
        deg[a]++;
        deg[b]++;
    }
    vll out(m);
    queue<int> q;
    forr(i, 1, n+1) {
        if(deg[i] == 1) q.push(i);
    }

    vb cyc(n+1, true);
    vb vis(n+1);
    while(q.size()) {
        auto x = q.front();
        q.pop();
        cyc[x] = false;
        vis[x] = true;
        for(auto p : graph[x]) {
            graph[p.f].erase(mp(x, p.s));
            deg[p.f]--;
            out[p.s] = 2*vec[x];
            vec[p.f] -= vec[x];
            if(deg[p.f] == 1) q.push(p.f);
        }
    }
    int c = 0;
    forr(i, 1, n+1) {
        if(cyc[i]) c++;
    }
    if(c > 0 && c % 2==0) {
        printf("0");
        return 0;
    }
    forr(i, 1, n+1) {
        if(cyc[i]) {
            pii p = (*graph[i].begin());
            // printf("sz: %d\n", (int)graph[i].size());
            int x = p.f;
            int ed = p.s;
            lli k = 0;
            int par = i;
            while(x != i) {
                k = vec[x] - k;
                int tmp = x;
                if((*graph[x].begin()).f != par)
                    x = (*graph[x].begin()).f;
                else x = (*next(graph[x].begin())).f;
                par = tmp;
            }
            // printf("%lld\n", k);
            lli v = (vec[i] - k)/2;
            out[ed] = 2 * v;
            x = (*graph[i].begin()).f;
            par = i;
            while(x != i) {
                v = vec[x] - v;
                int tmp = x;
                if((*graph[x].begin()).f != par)
                {pii p = *graph[x].begin();
                                out[p.s] = 2*v;
                                x = p.f;}
                else {
                    pii p = *next(graph[x].begin());
                    out[p.s] = 2*v;
                    x = p.f;
                }
                par = tmp;
            }
            break;
        }
    }
    frange(i, m) printf("%lld\n", out[i]);
}

Compilation message

pipes.cpp: In function 'void usaco()':
pipes.cpp:29:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   29 |     freopen("/media/hariaakash646/785EF1075EF0BF46/CompetitiveProgramming/input.in", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
pipes.cpp: In function 'int main()':
pipes.cpp:5:21: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
    5 | #define scd(t) scanf("%d", &t)
      |                ~~~~~^~~~~~~~~~
pipes.cpp:37:5: note: in expansion of macro 'scd'
   37 |     scd(n);
      |     ^~~
pipes.cpp:5:21: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
    5 | #define scd(t) scanf("%d", &t)
      |                ~~~~~^~~~~~~~~~
pipes.cpp:38:5: note: in expansion of macro 'scd'
   38 |     scd(m);
      |     ^~~
pipes.cpp:6:23: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
    6 | #define sclld(t) scanf("%lld", &t)
      |                  ~~~~~^~~~~~~~~~~~
pipes.cpp:41:21: note: in expansion of macro 'sclld'
   41 |     forr(i, 1, n+1) sclld(vec[i]);
      |                     ^~~~~
pipes.cpp:5:21: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
    5 | #define scd(t) scanf("%d", &t)
      |                ~~~~~^~~~~~~~~~
pipes.cpp:50:9: note: in expansion of macro 'scd'
   50 |         scd(a);
      |         ^~~
pipes.cpp:5:21: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
    5 | #define scd(t) scanf("%d", &t)
      |                ~~~~~^~~~~~~~~~
pipes.cpp:51:9: note: in expansion of macro 'scd'
   51 |         scd(b);
      |         ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 72 ms 17016 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 52 ms 13604 KB Output is correct
14 Correct 81 ms 16108 KB Output is correct
15 Correct 75 ms 16976 KB Output is correct
16 Correct 56 ms 14368 KB Output is correct
17 Correct 69 ms 16980 KB Output is correct
18 Correct 64 ms 17020 KB Output is correct
19 Correct 69 ms 16724 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 68 ms 17024 KB Output is correct
23 Correct 49 ms 13652 KB Output is correct
24 Correct 64 ms 17064 KB Output is correct
25 Correct 51 ms 14164 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 51 ms 15496 KB Output is correct
4 Correct 7 ms 1116 KB Output is correct
5 Correct 7 ms 1116 KB Output is correct
6 Correct 7 ms 1116 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 544 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 600 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 49 ms 13720 KB Output is correct
24 Correct 79 ms 16976 KB Output is correct
25 Correct 46 ms 15444 KB Output is correct
26 Correct 7 ms 1112 KB Output is correct
27 Correct 7 ms 1116 KB Output is correct
28 Correct 7 ms 1116 KB Output is correct
29 Correct 7 ms 1228 KB Output is correct
30 Correct 58 ms 16464 KB Output is correct
31 Correct 66 ms 16720 KB Output is correct
32 Correct 67 ms 16980 KB Output is correct
33 Correct 56 ms 16468 KB Output is correct
34 Correct 11 ms 1112 KB Output is correct
35 Correct 9 ms 1192 KB Output is correct
36 Correct 7 ms 1116 KB Output is correct
37 Correct 8 ms 1116 KB Output is correct
38 Correct 62 ms 16916 KB Output is correct
39 Correct 67 ms 16980 KB Output is correct
40 Correct 87 ms 16964 KB Output is correct
41 Correct 42 ms 16416 KB Output is correct
42 Correct 7 ms 1112 KB Output is correct
43 Correct 7 ms 1116 KB Output is correct
44 Correct 7 ms 1116 KB Output is correct
45 Correct 5 ms 860 KB Output is correct
46 Correct 70 ms 16756 KB Output is correct
47 Correct 65 ms 16976 KB Output is correct
48 Correct 60 ms 16724 KB Output is correct
49 Correct 53 ms 15476 KB Output is correct
50 Correct 9 ms 1112 KB Output is correct
51 Correct 8 ms 1112 KB Output is correct
52 Correct 7 ms 1112 KB Output is correct
53 Correct 5 ms 856 KB Output is correct
54 Correct 69 ms 16608 KB Output is correct