제출 #1126185

#제출 시각아이디문제언어결과실행 시간메모리
1126185MuhammetPipes (BOI13_pipes)C++20
74.07 / 100
126 ms15264 KiB
#include <bits/stdc++.h> using namespace std; #define SZ(s) (int)s.size() int n, m, nd; map <pair<int,int>,int> ans; vector <int> a, u1, u2, deg, vis, v1; vector <vector <int>> v; void dfs(int x){ vis[x] = true; v1.push_back(x); for(auto i : v[x]){ if(deg[i] == 2 and !vis[i]){ dfs(i); return; } } } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> m; a.resize(n+1); for(int i = 1; i <= n; i++){ cin >> a[i]; } v.resize(n+1), deg.resize(n+1,0); u1.resize(m+1), u2.resize(m+1); for(int i = 1; i <= m; i++){ cin >> u1[i] >> u2[i]; v[u1[i]].push_back(u2[i]); v[u2[i]].push_back(u1[i]); } if(m > n){ cout << 0; return 0; } queue <int> q; for(int i = 1; i <= n; i++){ deg[i] = SZ(v[i]); if(deg[i] == 1){ q.push(i); } } while(!q.empty()){ int ind = q.front(); q.pop(); deg[ind]--; for(auto i : v[ind]){ if(deg[i] > 0){ deg[i]--; ans[{min(i,ind), max(i,ind)}] = a[ind]; a[i] -= a[ind]; if(deg[i] == 1) q.push(i); } } } if(n == m+1){ for(int i = 1; i <= m; i++){ cout << 2*ans[{min(u1[i], u2[i]), max(u1[i], u2[i])}] << '\n'; } return 0; } vis.assign(n+1, 0); for(int i = 1; i <= n; i++){ if(deg[i] == 2){ nd = i; break; } } dfs(nd); if(SZ(v1) % 2 == 0){ cout << 0; return 0; } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...