Submission #253386

# Submission time Handle Problem Language Result Execution time Memory
253386 2020-07-27T21:00:45 Z m3r8 Pipes (BOI13_pipes) C++14
100 / 100
231 ms 17336 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); 
      };  
    };
    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:95:17: warning: 'idd' may be used uninitialized in this function [-Wmaybe-uninitialized]
       used[idd] = 1;
       ~~~~~~~~~~^~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2688 KB Output is correct
2 Correct 1 ms 2688 KB Output is correct
3 Correct 2 ms 2688 KB Output is correct
4 Correct 84 ms 9816 KB Output is correct
5 Correct 2 ms 2688 KB Output is correct
6 Correct 1 ms 2688 KB Output is correct
7 Correct 2 ms 2688 KB Output is correct
8 Correct 2 ms 2688 KB Output is correct
9 Correct 2 ms 2688 KB Output is correct
10 Correct 2 ms 2688 KB Output is correct
11 Correct 2 ms 2816 KB Output is correct
12 Correct 2 ms 2688 KB Output is correct
13 Correct 62 ms 8316 KB Output is correct
14 Correct 77 ms 9464 KB Output is correct
15 Correct 88 ms 9816 KB Output is correct
16 Correct 67 ms 8696 KB Output is correct
17 Correct 80 ms 9796 KB Output is correct
18 Correct 85 ms 9816 KB Output is correct
19 Correct 81 ms 9080 KB Output is correct
20 Correct 2 ms 2688 KB Output is correct
21 Correct 2 ms 2688 KB Output is correct
22 Correct 80 ms 9776 KB Output is correct
23 Correct 61 ms 8312 KB Output is correct
24 Correct 81 ms 9812 KB Output is correct
25 Correct 65 ms 8568 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2688 KB Output is correct
2 Correct 2 ms 2816 KB Output is correct
3 Correct 61 ms 9016 KB Output is correct
4 Correct 54 ms 6648 KB Output is correct
5 Correct 55 ms 6904 KB Output is correct
6 Correct 227 ms 17272 KB Output is correct
7 Correct 2 ms 2688 KB Output is correct
8 Correct 1 ms 2688 KB Output is correct
9 Correct 2 ms 2688 KB Output is 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 Correct 2 ms 2688 KB Output is correct
15 Correct 3 ms 2816 KB Output is correct
16 Correct 2 ms 2816 KB Output is correct
17 Correct 2 ms 2688 KB Output is correct
18 Correct 2 ms 2688 KB Output is correct
19 Correct 2 ms 2688 KB Output is correct
20 Correct 3 ms 2688 KB Output is correct
21 Correct 4 ms 2816 KB Output is correct
22 Correct 3 ms 2816 KB Output is correct
23 Correct 85 ms 12532 KB Output is correct
24 Correct 112 ms 13812 KB Output is correct
25 Correct 78 ms 9016 KB Output is correct
26 Correct 85 ms 6724 KB Output is correct
27 Correct 60 ms 6648 KB Output is correct
28 Correct 63 ms 7032 KB Output is correct
29 Correct 182 ms 14712 KB Output is correct
30 Correct 93 ms 12800 KB Output is correct
31 Correct 102 ms 16368 KB Output is correct
32 Correct 87 ms 11000 KB Output is correct
33 Correct 67 ms 9208 KB Output is correct
34 Correct 56 ms 6648 KB Output is correct
35 Correct 54 ms 6776 KB Output is correct
36 Correct 57 ms 7032 KB Output is correct
37 Correct 231 ms 17336 KB Output is correct
38 Correct 109 ms 13064 KB Output is correct
39 Correct 84 ms 10492 KB Output is correct
40 Correct 99 ms 13428 KB Output is correct
41 Correct 58 ms 9204 KB Output is correct
42 Correct 55 ms 6524 KB Output is correct
43 Correct 53 ms 6648 KB Output is correct
44 Correct 54 ms 6916 KB Output is correct
45 Correct 185 ms 15224 KB Output is correct
46 Correct 105 ms 12408 KB Output is correct
47 Correct 94 ms 13556 KB Output is correct
48 Correct 98 ms 16132 KB Output is correct
49 Correct 69 ms 8952 KB Output is correct
50 Correct 56 ms 6776 KB Output is correct
51 Correct 54 ms 6904 KB Output is correct
52 Correct 57 ms 6628 KB Output is correct
53 Correct 182 ms 15224 KB Output is correct
54 Correct 96 ms 12312 KB Output is correct