# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
230454 | BamiTorabi | Pipes (BOI13_pipes) | C++14 | 505 ms | 56416 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//Sasayego! Sasayego! Shinzou wo Sasageyo!
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <cstring>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <numeric>
#include <bitset>
#include <ctime>
#define debug(x) cerr << #x << " = " << x << endl
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair <ll, ll> pll;
typedef pair <int, int> pii;
const int maxN = 1e5 + 5;
const int maxM = 5e5 + 5;
const ll INF = 1e18;
const ll MOD = 1e9 + 7;
bool mark[maxN];
int c[maxN];
set <int> G[maxN];
vector <int> vec;
vector <pii> E;
map <pii, int> mp;
queue <int> Q;
pii sorted(int u, int v){
pii pp = {u, v};
if (pp.first > pp.second)
swap(pp.first, pp.second);
return pp;
}
int main(){
time_t START = clock();
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n, m; scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++){
scanf("%d", c + i);
c[i] *= 2;
}
for (int i = 0; i < m; i++){
int u, v; scanf("%d%d", &u, &v);
tie(u, v) = sorted(u, v);
G[u].insert(v);
G[v].insert(u);
E.push_back({u, v});
}
if (m > n)
return printf("0\n"), 0;
for (int i = 1; i <= n; i++)
if ((int)G[i].size() == 1)
Q.push(i);
while (!Q.empty()){
int v = Q.front();
Q.pop();
if ((int)G[v].size() != 1)
continue;
int u = *G[v].begin();
G[v].clear();
mp[sorted(u, v)] = c[v];
c[u] -= c[v];
G[u].erase(G[u].lower_bound(v));
if ((int)G[u].size() == 1)
Q.push(u);
}
int cnt = 0;
for (int i = 1; i <= n; i++)
cnt += ((int)G[i].size() == 2);
if (cnt > 0 && cnt % 2 == 0)
return printf("0\n"), 0;
if (cnt)
for (int i = 1; i <= n; i++)
if ((int)G[i].size() == 2){
vec.push_back(i);
int u = *(G[i].begin());
G[i].erase(G[i].begin());
int x = *(G[i].begin());
G[x].erase(G[x].lower_bound(i));
ll ans = c[i];
while (x != i){
vec.push_back(x);
ans = c[x] - ans;
int y = *(G[x].begin());
G[x].erase(G[x].begin());
if (y != i)
G[y].erase(G[y].lower_bound(x));
x = y;
}
c[u] -= ans / 2;
c[i] -= ans / 2;
mp[sorted(i, u)] = ans / 2;
break;
}
for (int i = 1; i < (int)vec.size(); i++){
mp[sorted(vec[i - 1], vec[i])] = c[vec[i - 1]];
c[vec[i]] -= c[vec[i - 1]];
}
for (pii e : E)
printf("%d\n", mp[e]);
time_t FINISH = clock();
cerr << "Execution time: " << (ld)(FINISH - START) / CLOCKS_PER_SEC * 1000.0 << " milliseconds.\n";
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |