답안 #253385

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
253385 2020-07-27T21:00:13 Z m3r8 Pipes (BOI13_pipes) C++14
35 / 100
231 ms 17376 KB
#include <stdio.h>
#include <queue>
#include <vector>
#include <utility>
#include <functional>

#define N 100100
#define M 500500
#define ii std::pair<int,int>
#define ll long long

std::vector<ii> adj[N];

ll val[M];
int iq[N];
int used[M];
ll cn[N];
int dg[N];
std::queue<int> que;
std::vector<int> rm;

ll dfs(int v, int p, int cc, int en){
  if(v == en)return 0ll;
  ll erg = 0;  
  if(cc)erg += cn[v]*2ll;
  for(auto i: adj[v]){
    if(i.second != p && !iq[i.second]){
      erg += dfs(i.second,v,cc^1,en);
    };  
  };
  return erg;
};

int main(void){
  int n,m;  
  int a,b;
  scanf("%d %d",&n,&m);
  for(int i = 1;i<=n;i++){
    scanf("%lld",&cn[i]);  
  };
  for(int i = 0;i<m;i++){
    scanf("%d %d",&a,&b);  
    adj[a].push_back({i,b});
    adj[b].push_back({i,a});
  };
  int pos = 1;
  if(m > n){
    pos = 0;
  }else{
    for(int i = 1;i<=n;i++){
      dg[i] = adj[i].size();
      if(adj[i].size() == 1){
        iq[i] = 1;
        que.push(i);  
      };
    };  
    while(que.size()){
      int akt = que.front();que.pop();  
      for(auto i : adj[akt]){
        if(!used[i.first]){
          val[i.first] = cn[akt];
          used[i.first] = 1;
          dg[i.second]--;
          cn[i.second] -= val[i.first];
          if(dg[i.second] == 1){
            que.push(i.second);  
            iq[i.second] = 1;
          };
        };  
      };
    };
    ll sm = 0;
    for(int i = 1;i<=n;i++){
      if(!iq[i]){
        sm += cn[i];
        rm.push_back(i); 
      };  
    };
    printf("%lld\n",sm);
    if(rm.size()%2 == 0 && rm.size()){
      pos = 0;
    }else if(rm.size()){
      int st = rm[0];
      int en = 0;
      int idd;
      for(auto i: adj[st]){
        if(!iq[i.second]){
          en = i.second;
          idd = i.first;
          break;
        };  
      };
      ll tmp = dfs(st,en,0,en);
      sm -= tmp;
      val[idd] = sm/2ll;
      used[idd] = 1;
      dg[st]--;
      dg[en]--;
      cn[st] -= val[idd];
      cn[en] -= val[idd];
      que.push(st);
      que.push(en);
      iq[st] = 1;
      iq[en] = 1;
      while(que.size()){
        int akt = que.front();que.pop();  
        for(auto i: adj[akt]){
          if(!used[i.first]){
            val[i.first] = cn[akt];
            used[i.first] = 1;
            dg[i.second]--;
            cn[i.second] -= val[i.first];
            if(dg[i.second] == 1){
              que.push(i.second);  
              iq[i.second] = 1;
            };
          }; 
        };
      };
    };
  };
  if(pos){
    for(int i = 0;i<m;i++){
      printf("%lld\n",val[i] * 2ll);  
    };
  }else{
    printf("0\n");  
  };
  return 0;
};




Compilation message

pipes.cpp: In function 'int main()':
pipes.cpp:37:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d",&n,&m);
   ~~~~~^~~~~~~~~~~~~~~
pipes.cpp:39:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%lld",&cn[i]);  
     ~~~~~^~~~~~~~~~~~~~~
pipes.cpp:42:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d",&a,&b);  
     ~~~~~^~~~~~~~~~~~~~~
pipes.cpp:96:17: warning: 'idd' may be used uninitialized in this function [-Wmaybe-uninitialized]
       used[idd] = 1;
       ~~~~~~~~~~^~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 2688 KB Output isn't correct
2 Incorrect 1 ms 2688 KB Output isn't correct
3 Incorrect 2 ms 2688 KB Output isn't correct
4 Incorrect 82 ms 9816 KB Output isn't correct
5 Incorrect 2 ms 2688 KB Output isn't correct
6 Incorrect 1 ms 2688 KB Output isn't correct
7 Incorrect 1 ms 2688 KB Output isn't correct
8 Incorrect 2 ms 2688 KB Output isn't correct
9 Incorrect 2 ms 2688 KB Output isn't correct
10 Incorrect 2 ms 2688 KB Output isn't correct
11 Incorrect 2 ms 2816 KB Output isn't correct
12 Incorrect 2 ms 2688 KB Output isn't correct
13 Incorrect 63 ms 8316 KB Output isn't correct
14 Incorrect 75 ms 9336 KB Output isn't correct
15 Incorrect 82 ms 9816 KB Output isn't correct
16 Incorrect 69 ms 8696 KB Output isn't correct
17 Incorrect 78 ms 9688 KB Output isn't correct
18 Incorrect 81 ms 9816 KB Output isn't correct
19 Incorrect 82 ms 9048 KB Output isn't correct
20 Incorrect 1 ms 2688 KB Output isn't correct
21 Incorrect 2 ms 2688 KB Output isn't correct
22 Incorrect 84 ms 9712 KB Output isn't correct
23 Incorrect 66 ms 8312 KB Output isn't correct
24 Incorrect 86 ms 9744 KB Output isn't correct
25 Incorrect 65 ms 8700 KB Output isn't correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 2688 KB Output isn't correct
2 Incorrect 2 ms 2816 KB Output isn't correct
3 Incorrect 68 ms 9056 KB Output isn't correct
4 Correct 56 ms 6776 KB Output is correct
5 Correct 55 ms 6904 KB Output is correct
6 Correct 231 ms 17376 KB Output is correct
7 Incorrect 2 ms 2688 KB Output isn't correct
8 Incorrect 2 ms 2688 KB Output isn't correct
9 Incorrect 1 ms 2688 KB Output isn't correct
10 Correct 1 ms 2688 KB Output is correct
11 Correct 2 ms 2688 KB Output is correct
12 Correct 2 ms 2688 KB Output is correct
13 Correct 2 ms 2688 KB Output is correct
14 Incorrect 2 ms 2688 KB Output isn't correct
15 Incorrect 2 ms 2816 KB Output isn't correct
16 Incorrect 2 ms 2816 KB Output isn't correct
17 Incorrect 2 ms 2688 KB Output isn't correct
18 Correct 2 ms 2688 KB Output is correct
19 Correct 2 ms 2688 KB Output is correct
20 Correct 2 ms 2688 KB Output is correct
21 Correct 3 ms 2816 KB Output is correct
22 Incorrect 2 ms 2816 KB Output isn't correct
23 Incorrect 81 ms 12400 KB Output isn't correct
24 Incorrect 114 ms 13684 KB Output isn't correct
25 Incorrect 70 ms 9016 KB Output isn't correct
26 Correct 62 ms 6776 KB Output is correct
27 Correct 59 ms 6648 KB Output is correct
28 Correct 75 ms 7160 KB Output is correct
29 Correct 181 ms 14712 KB Output is correct
30 Incorrect 94 ms 12800 KB Output isn't correct
31 Incorrect 100 ms 16368 KB Output isn't correct
32 Incorrect 86 ms 11128 KB Output isn't correct
33 Incorrect 62 ms 9212 KB Output isn't correct
34 Correct 53 ms 6648 KB Output is correct
35 Correct 58 ms 6776 KB Output is correct
36 Correct 55 ms 7032 KB Output is correct
37 Correct 226 ms 17272 KB Output is correct
38 Incorrect 95 ms 13064 KB Output isn't correct
39 Incorrect 86 ms 10488 KB Output isn't correct
40 Incorrect 92 ms 13428 KB Output isn't correct
41 Incorrect 57 ms 9204 KB Output isn't correct
42 Correct 58 ms 6520 KB Output is correct
43 Correct 52 ms 6648 KB Output is correct
44 Correct 53 ms 6904 KB Output is correct
45 Correct 173 ms 15480 KB Output is correct
46 Incorrect 92 ms 12508 KB Output isn't correct
47 Incorrect 91 ms 13556 KB Output isn't correct
48 Incorrect 96 ms 16132 KB Output isn't correct
49 Incorrect 62 ms 8952 KB Output isn't correct
50 Correct 54 ms 6776 KB Output is correct
51 Correct 53 ms 6904 KB Output is correct
52 Correct 53 ms 6692 KB Output is correct
53 Correct 185 ms 15224 KB Output is correct
54 Incorrect 88 ms 12312 KB Output isn't correct