-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path1697.cpp
37 lines (36 loc) · 827 Bytes
/
1697.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int n; cin >> n;
vector<int> deg(n), tmp;
vector<pair<int, int>> edges;
priority_queue<pair<int, int>> q;
for (int i = 0; i < n; ++i) {
cin >> deg[i];
if (deg[i]) q.emplace(deg[i], i);
}
while (!q.empty()) {
auto [degu, u] = q.top(); q.pop();
tmp.clear();
while (degu--) {
if (q.empty()) {
cout << "IMPOSSIBLE\n";
return 0;
}
int v = q.top().second; q.pop();
edges.emplace_back(u + 1, v + 1);
if (--deg[v] > 0) {
tmp.emplace_back(v);
}
}
for (int v: tmp) {
q.emplace(deg[v], v);
}
}
cout << edges.size() << "\n";
for (auto [u, v]: edges) {
cout << u << " " << v << "\n";
}
return 0;
}