답안 #631545

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
631545 2022-08-18T08:21:40 Z bachhoangxuan Pipes (BOI13_pipes) C++17
100 / 100
89 ms 13128 KB
#include<bits/stdc++.h>
using namespace std;
#define maxn 100005
#define pii pair<int,int>
vector<pii> edge[maxn];
long long n,m,c[maxn],st,en,num=0,ans[maxn],dep[maxn];
bool check[maxn],visited[maxn];
bool dfs(int u,int par){
    bool ok=false;
    visited[u]=true;
    for(pii x:edge[u]){
        int v=x.first;
        if(v==par) continue;
        if(visited[v]){st=u;en=v;}
        else if(dfs(v,u)) ok=true;
    }
}
void dfs2(int u,int par){
    for(pii v:edge[u]){
        if(v.first==par || check[v.first]) continue;
        dfs2(v.first,u);
        ans[v.second]-=c[v.first];
        c[u]-=c[v.first];
        c[v.first]=0;
    }
}
void bfs(){
    queue<int> q;
    for(int i=1;i<=n;i++){
        if(dep[i]==1) q.push(i);
    }
    while(!q.empty()){
        int u=q.front();q.pop();
        for(pii v:edge[u]){
            dep[v.first]--;
            if(dep[v.first]==1) q.push(v.first);
        }
    }
    for(int i=1;i<=n;i++){
        if(dep[i]>1){check[i]=true;num++;}
    }
}
int main(){
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    cin >> n >> m;
    if(m>n){
        cout << 0 << '\n';
        return 0;
    }
    for(int i=1;i<=n;i++) cin >> c[i];
    for(int i=1;i<=m;i++){
        int u,v;cin >> u >> v;
        edge[u].push_back({v,i});dep[u]++;
        edge[v].push_back({u,i});dep[v]++;
    }
    bfs();
    if(num%2==0 && m==n){
        cout << 0 << '\n';
        return 0;
    }
    for(int i=1;i<=n;i++){
        if(check[i]) dfs2(i,0);
    }
    if(m!=n) dfs2(1,0);
    int cur=0,u=0,pre=0;
    for(int i=1;i<=n;i++){
        if(check[i]){cur=i;u=i;break;}
    }
    for(int i=0;i<=1;i++){
        u=cur;
        if(i==1) c[u]/=2;
        while(true){
            for(pii v:edge[u]){
                if(check[v.first] && v.first!=pre){
                    //cout << u << ' ' << v.first << '\n';
                    ans[v.second]-=c[u];
                    c[v.first]-=c[u];
                    c[u]=0;pre=u;u=v.first;
                    break;
                }
            }
            if(u==cur) break;
        }
    }
    for(int i=1;i<=m;i++) cout << -ans[i]*2 << '\n';
}

Compilation message

pipes.cpp: In function 'bool dfs(int, int)':
pipes.cpp:9:10: warning: variable 'ok' set but not used [-Wunused-but-set-variable]
    9 |     bool ok=false;
      |          ^~
pipes.cpp:17:1: warning: no return statement in function returning non-void [-Wreturn-type]
   17 | }
      | ^
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2688 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 60 ms 9356 KB Output is correct
5 Correct 1 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 2 ms 2644 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 3 ms 2644 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 2 ms 2720 KB Output is correct
13 Correct 53 ms 7964 KB Output is correct
14 Correct 63 ms 9004 KB Output is correct
15 Correct 55 ms 9392 KB Output is correct
16 Correct 74 ms 8392 KB Output is correct
17 Correct 59 ms 9308 KB Output is correct
18 Correct 55 ms 9292 KB Output is correct
19 Correct 73 ms 12524 KB Output is correct
20 Correct 1 ms 2644 KB Output is correct
21 Correct 2 ms 2644 KB Output is correct
22 Correct 74 ms 9376 KB Output is correct
23 Correct 52 ms 8004 KB Output is correct
24 Correct 73 ms 9312 KB Output is correct
25 Correct 47 ms 8232 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 43 ms 7640 KB Output is correct
4 Correct 1 ms 2644 KB Output is correct
5 Correct 1 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 2 ms 2644 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 1 ms 2644 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 1 ms 2668 KB Output is correct
13 Correct 1 ms 2644 KB Output is correct
14 Correct 1 ms 2644 KB Output is correct
15 Correct 2 ms 2644 KB Output is correct
16 Correct 2 ms 2644 KB Output is correct
17 Correct 2 ms 2644 KB Output is correct
18 Correct 1 ms 2644 KB Output is correct
19 Correct 2 ms 2644 KB Output is correct
20 Correct 1 ms 2644 KB Output is correct
21 Correct 1 ms 2568 KB Output is correct
22 Correct 2 ms 2772 KB Output is correct
23 Correct 44 ms 7756 KB Output is correct
24 Correct 74 ms 9000 KB Output is correct
25 Correct 44 ms 7616 KB Output is correct
26 Correct 1 ms 2644 KB Output is correct
27 Correct 1 ms 2644 KB Output is correct
28 Correct 1 ms 2644 KB Output is correct
29 Correct 2 ms 2644 KB Output is correct
30 Correct 68 ms 12236 KB Output is correct
31 Correct 60 ms 8684 KB Output is correct
32 Correct 64 ms 9304 KB Output is correct
33 Correct 39 ms 7728 KB Output is correct
34 Correct 1 ms 2644 KB Output is correct
35 Correct 1 ms 2644 KB Output is correct
36 Correct 2 ms 2644 KB Output is correct
37 Correct 2 ms 2644 KB Output is correct
38 Correct 61 ms 11216 KB Output is correct
39 Correct 60 ms 9316 KB Output is correct
40 Correct 70 ms 9104 KB Output is correct
41 Correct 37 ms 7500 KB Output is correct
42 Correct 2 ms 2644 KB Output is correct
43 Correct 1 ms 2644 KB Output is correct
44 Correct 1 ms 2644 KB Output is correct
45 Correct 1 ms 2644 KB Output is correct
46 Correct 59 ms 13128 KB Output is correct
47 Correct 71 ms 9132 KB Output is correct
48 Correct 79 ms 8772 KB Output is correct
49 Correct 43 ms 7852 KB Output is correct
50 Correct 2 ms 2644 KB Output is correct
51 Correct 1 ms 2644 KB Output is correct
52 Correct 2 ms 2644 KB Output is correct
53 Correct 1 ms 2644 KB Output is correct
54 Correct 89 ms 12040 KB Output is correct