답안 #25839

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
25839 2017-06-24T09:41:57 Z gs14004 Pipes (BOI13_pipes) C++11
100 / 100
143 ms 10288 KB
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
typedef pair<int,int> pi;
typedef long long lint;
 
int n, m;
int deg[100005];
bool vis[100005];
 
int a[100005];
int ret[100005];
 
vector<pi> graph[100005];
queue<int> q;
 
int curval[100005], nxt[100005], cycsz;
 
int main(){
    scanf("%d %d",&n,&m);
    if(n < m){
        puts("0");
        return 0;
    }
    for(int i=1; i<=n; i++){
        scanf("%d",&a[i]);
    }
    for(int i=0; i<m; i++){
        int s, e;
        scanf("%d %d",&s,&e);
        graph[s].push_back(pi(i, e));
        graph[e].push_back(pi(i, s));
        deg[s]++, deg[e]++;
    }
    for(int i=1; i<=n; i++){
        if(deg[i] == 1){
            q.push(i);
        }
    }
    while(!q.empty()){
        int x = q.front();
        q.pop();
        deg[x] = 0;
        vis[x] = 1;
        for(int i=0; i<graph[x].size(); i++){
            int pos = graph[x][i].second;
            if(vis[pos]) continue;
            deg[pos]--;
            ret[graph[x][i].first] = 2 * a[x];
            a[pos]-=a[x];
            if(deg[pos] == 1){
                q.push(pos);
            }
        }
    }
    int pos;
    for(int i=1; i<=n; i++){
        if(deg[i]) cycsz++, pos = i;
    }
    if(n == m && cycsz % 2 == 0){
        puts("0");
        return 0;
    }
    lint sum = 0;
    for(int i=0; i<cycsz; i++){
        curval[i] = pos;
        sum += a[pos];
        for(int j=0; j<graph[pos].size(); j++){
            pi edg = graph[pos][j];
            if(deg[edg.second]){
                deg[edg.second] = 0;
                nxt[i] = edg.first;
                pos = edg.second;
                break;
            }
        }
    }
    if(cycsz){
        sum /= 2;
        lint tsum = 0;
        for(int i=0; i<cycsz/2; i++){
            tsum += a[curval[(2+2*i) % cycsz]];
        }
        ret[nxt[0]] = 2 * (sum - tsum);
        int pt = 0;
        for(int i=2; i!=0; i = (i + 2) % cycsz){
            tsum -= a[curval[i]];
            tsum += a[curval[(i + cycsz / 2 * 2) % cycsz]];
            ret[nxt[i]] = 2 * (sum - tsum);
        }
    }
    for(int i=0; i<m; i++){
        printf("%d\n",ret[i]);
    }
}

Compilation message

pipes.cpp: In function 'int main()':
pipes.cpp:47:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int i=0; i<graph[x].size(); i++){
                       ^
pipes.cpp:70:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int j=0; j<graph[pos].size(); j++){
                       ^
pipes.cpp:87:13: warning: unused variable 'pt' [-Wunused-variable]
         int pt = 0;
             ^
pipes.cpp:22:25: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d",&n,&m);
                         ^
pipes.cpp:28:26: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d",&a[i]);
                          ^
pipes.cpp:32:29: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d",&s,&e);
                             ^
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 6328 KB Output is correct
2 Correct 0 ms 6328 KB Output is correct
3 Correct 0 ms 6328 KB Output is correct
4 Correct 113 ms 10288 KB Output is correct
5 Correct 0 ms 6328 KB Output is correct
6 Correct 0 ms 6328 KB Output is correct
7 Correct 0 ms 6328 KB Output is correct
8 Correct 0 ms 6328 KB Output is correct
9 Correct 3 ms 6328 KB Output is correct
10 Correct 0 ms 6328 KB Output is correct
11 Correct 3 ms 6328 KB Output is correct
12 Correct 0 ms 6328 KB Output is correct
13 Correct 83 ms 9496 KB Output is correct
14 Correct 113 ms 10024 KB Output is correct
15 Correct 109 ms 10288 KB Output is correct
16 Correct 96 ms 9628 KB Output is correct
17 Correct 103 ms 10288 KB Output is correct
18 Correct 109 ms 10288 KB Output is correct
19 Correct 123 ms 9496 KB Output is correct
20 Correct 0 ms 6328 KB Output is correct
21 Correct 0 ms 6328 KB Output is correct
22 Correct 113 ms 10288 KB Output is correct
23 Correct 66 ms 9496 KB Output is correct
24 Correct 86 ms 10288 KB Output is correct
25 Correct 89 ms 9632 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 6328 KB Output is correct
2 Correct 0 ms 6328 KB Output is correct
3 Correct 113 ms 9760 KB Output is correct
4 Correct 0 ms 6328 KB Output is correct
5 Correct 0 ms 6328 KB Output is correct
6 Correct 0 ms 6328 KB Output is correct
7 Correct 0 ms 6328 KB Output is correct
8 Correct 0 ms 6328 KB Output is correct
9 Correct 3 ms 6328 KB Output is correct
10 Correct 0 ms 6328 KB Output is correct
11 Correct 0 ms 6328 KB Output is correct
12 Correct 0 ms 6328 KB Output is correct
13 Correct 0 ms 6328 KB Output is correct
14 Correct 0 ms 6328 KB Output is correct
15 Correct 3 ms 6328 KB Output is correct
16 Correct 3 ms 6328 KB Output is correct
17 Correct 0 ms 6328 KB Output is correct
18 Correct 0 ms 6328 KB Output is correct
19 Correct 0 ms 6328 KB Output is correct
20 Correct 0 ms 6328 KB Output is correct
21 Correct 0 ms 6328 KB Output is correct
22 Correct 0 ms 6328 KB Output is correct
23 Correct 73 ms 9100 KB Output is correct
24 Correct 109 ms 9892 KB Output is correct
25 Correct 73 ms 9760 KB Output is correct
26 Correct 0 ms 6328 KB Output is correct
27 Correct 0 ms 6328 KB Output is correct
28 Correct 0 ms 6328 KB Output is correct
29 Correct 0 ms 6328 KB Output is correct
30 Correct 116 ms 9496 KB Output is correct
31 Correct 143 ms 9628 KB Output is correct
32 Correct 109 ms 10156 KB Output is correct
33 Correct 99 ms 9892 KB Output is correct
34 Correct 0 ms 6328 KB Output is correct
35 Correct 0 ms 6328 KB Output is correct
36 Correct 0 ms 6328 KB Output is correct
37 Correct 0 ms 6328 KB Output is correct
38 Correct 136 ms 9628 KB Output is correct
39 Correct 113 ms 10156 KB Output is correct
40 Correct 106 ms 9892 KB Output is correct
41 Correct 83 ms 9628 KB Output is correct
42 Correct 0 ms 6328 KB Output is correct
43 Correct 0 ms 6328 KB Output is correct
44 Correct 0 ms 6328 KB Output is correct
45 Correct 0 ms 6328 KB Output is correct
46 Correct 109 ms 9496 KB Output is correct
47 Correct 109 ms 9892 KB Output is correct
48 Correct 133 ms 9628 KB Output is correct
49 Correct 86 ms 10024 KB Output is correct
50 Correct 0 ms 6328 KB Output is correct
51 Correct 0 ms 6328 KB Output is correct
52 Correct 0 ms 6328 KB Output is correct
53 Correct 0 ms 6328 KB Output is correct
54 Correct 116 ms 9628 KB Output is correct