답안 #253384

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
253384 2020-07-27T20:46:42 Z m3r8 Pipes (BOI13_pipes) C++14
74.0741 / 100
262 ms 23544 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){
      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;
       ~~~~~~~~~~^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2688 KB Output is correct
2 Correct 2 ms 2688 KB Output is correct
3 Correct 2 ms 2816 KB Output is correct
4 Correct 80 ms 11352 KB Output is correct
5 Correct 3 ms 2688 KB Output is correct
6 Correct 2 ms 2688 KB Output is correct
7 Correct 2 ms 2688 KB Output is correct
8 Correct 3 ms 2688 KB Output is correct
9 Correct 2 ms 2688 KB Output is correct
10 Correct 2 ms 2816 KB Output is correct
11 Correct 2 ms 2816 KB Output is correct
12 Correct 2 ms 2816 KB Output is correct
13 Correct 62 ms 9592 KB Output is correct
14 Correct 75 ms 10896 KB Output is correct
15 Correct 89 ms 11352 KB Output is correct
16 Correct 67 ms 10104 KB Output is correct
17 Correct 90 ms 11436 KB Output is correct
18 Correct 86 ms 11352 KB Output is correct
19 Correct 96 ms 10652 KB Output is correct
20 Correct 2 ms 2688 KB Output is correct
21 Correct 2 ms 2816 KB Output is correct
22 Correct 89 ms 11352 KB Output is correct
23 Correct 79 ms 9720 KB Output is correct
24 Correct 96 ms 11348 KB Output is correct
25 Correct 75 ms 9976 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 2688 KB Output isn't correct
2 Incorrect 3 ms 2816 KB Output isn't correct
3 Correct 69 ms 10488 KB Output is correct
4 Correct 63 ms 8312 KB Output is correct
5 Correct 53 ms 8440 KB Output is correct
6 Correct 262 ms 23544 KB Output is correct
7 Incorrect 3 ms 2688 KB Output isn't correct
8 Incorrect 2 ms 2688 KB Output isn't correct
9 Correct 2 ms 2688 KB Output is correct
10 Correct 2 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 4 ms 2816 KB Output isn't correct
16 Incorrect 4 ms 2816 KB Output isn't correct
17 Correct 2 ms 2688 KB Output is correct
18 Correct 3 ms 2688 KB Output is correct
19 Correct 3 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 3 ms 2816 KB Output isn't correct
23 Incorrect 76 ms 13812 KB Output isn't correct
24 Incorrect 103 ms 15476 KB Output isn't correct
25 Correct 62 ms 10488 KB Output is correct
26 Correct 58 ms 8368 KB Output is correct
27 Correct 53 ms 8184 KB Output is correct
28 Correct 60 ms 8696 KB Output is correct
29 Correct 189 ms 19832 KB Output is correct
30 Incorrect 102 ms 17536 KB Output isn't correct
31 Incorrect 100 ms 18160 KB Output isn't correct
32 Incorrect 90 ms 12752 KB Output isn't correct
33 Correct 64 ms 10872 KB Output is correct
34 Correct 55 ms 8312 KB Output is correct
35 Correct 54 ms 8312 KB Output is correct
36 Correct 57 ms 8572 KB Output is correct
37 Correct 224 ms 23544 KB Output is correct
38 Incorrect 98 ms 14728 KB Output isn't correct
39 Incorrect 88 ms 12024 KB Output isn't correct
40 Incorrect 103 ms 15092 KB Output isn't correct
41 Correct 64 ms 10868 KB Output is correct
42 Correct 53 ms 8184 KB Output is correct
43 Correct 73 ms 8312 KB Output is correct
44 Correct 59 ms 8444 KB Output is correct
45 Correct 197 ms 20728 KB Output is correct
46 Incorrect 112 ms 15740 KB Output isn't correct
47 Incorrect 102 ms 15220 KB Output isn't correct
48 Incorrect 95 ms 17924 KB Output isn't correct
49 Correct 61 ms 10360 KB Output is correct
50 Correct 55 ms 8440 KB Output is correct
51 Correct 54 ms 8440 KB Output is correct
52 Correct 56 ms 8184 KB Output is correct
53 Correct 184 ms 20728 KB Output is correct
54 Incorrect 99 ms 16280 KB Output isn't correct