#include <bits/stdc++.h>
#define PB push_back
#define ST first
#define ND second
#define _ ios_base::sync_with_stdio(0); cin.tie(0);
//mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
using namespace std;
using ll = long long;
using pi = pair<int,int>;
using vi = vector<int>;
const int nax = 1000 * 1000 + 10;
int l, r, m;
int deg[nax][2];
pi edge[nax];
vector<pi> V[nax][2];
int I[nax][2];
bool vis[nax];
int col[nax];
int nr;
vi cycle;
void eulerCycle(int x, bool turn) {
while(I[x][turn] < (int)V[x][turn].size()) {
auto nbh = V[x][turn][I[x][turn]++];
if(!vis[nbh.ND]) {
vis[nbh.ND] = true;
eulerCycle(nbh.ST, turn^1);
cycle.PB(nbh.ND);
}
}
}
vector<pi>V2[nax][2];
void rec(int a, int b, int mx_deg) {
if(a == b) {
for(int i = 1; i <= l; ++i) {
if((int)V[i][0].size() > 0) col[V[i][0][0].ND] = a;
}
return;
}
int ptr = 1;
for(int i = 1; i <= l; ++i) {
if((int)V[i][0].size() % 2 == 1) {
while(V[ptr][1].size() == mx_deg) ptr++;
nr++;
V[i][0].emplace_back(ptr, nr);
V[ptr][1].emplace_back(i, nr);
edge[nr] = {i, ptr};
}
}
ptr = 1;
for(int i = 1; i <= r; ++i) {
if((int)V[i][1].size() % 2 == 1) {
while(V[ptr][0].size() == mx_deg) ptr++;
nr++;
V[ptr][0].emplace_back(i, nr);
V[i][1].emplace_back(ptr, nr);
edge[nr] = {ptr, i};
}
}
for(int i = 1; i <= l; ++i) {
I[i][0] = 0;
for(auto nbh : V[i][0]) {
vis[nbh.ND] = false;
}
}
for(int i = 1; i <= r; ++i) {
I[i][1] = 0;
}
cycle.clear();
for(int i = 1; i <= l; ++i) {
eulerCycle(i, 0);
}
vi cyc = cycle;
for(int i = 0; i < (int)cyc.size(); i += 2) {
V2[edge[cyc[i]].ST][0].PB({edge[cyc[i]].ND, cyc[i]});
V2[edge[cyc[i]].ND][1].PB({edge[cyc[i]].ST, cyc[i]});
}
for(int i = 1; i <= l; ++i) {
V[i][0].swap(V2[i][0]);
V2[i][0].clear();
}
for(int i = 1; i <= r; ++i) {
V[i][1].swap(V2[i][1]);
V2[i][1].clear();
}
rec(a, (a + b)/2, mx_deg / 2);
for(int i = 1; i < (int)cyc.size(); i += 2) {
V2[edge[cyc[i]].ST][0].PB({edge[cyc[i]].ND, cyc[i]});
V2[edge[cyc[i]].ND][1].PB({edge[cyc[i]].ST, cyc[i]});
}
for(int i = 1; i <= l; ++i) {
V[i][0].swap(V2[i][0]);
V2[i][0].clear();
}
for(int i = 1; i <= r; ++i) {
V[i][1].swap(V2[i][1]);
V2[i][1].clear();
}
rec((a+b)/2 + 1, b, mx_deg / 2);
}
int main() {
cin >> l >> r >> m;
for(int a, b, i = 1; i <= m; ++i) {
cin >> a >> b;
edge[i] = {a, b};
deg[a][0]++;
deg[b][1]++;
V[a][0].PB({b, i});
V[b][1].PB({a, i});
}
int mx = 0;
for(int i = 1; i <= l; ++i) {
mx = max(mx, deg[i][0]);
}
for(int i = 1; i <= r; ++i) {
mx = max(mx, deg[i][1]);
}
int pt = 1;
while(pt < mx) pt *= 2;
nr = m;
rec(1, pt, pt);
cout << pt << "\n";
for(int i = 1; i <= m; ++i) {
cout << col[i] << "\n";
}
}
Compilation message
teoreticar.cpp: In function 'void rec(int, int, int)':
teoreticar.cpp:49:27: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
49 | while(V[ptr][1].size() == mx_deg) ptr++;
| ~~~~~~~~~~~~~~~~~^~~~~~~~~
teoreticar.cpp:59:27: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
59 | while(V[ptr][0].size() == mx_deg) ptr++;
| ~~~~~~~~~~~~~~~~~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
60 ms |
94256 KB |
Output is correct |
2 |
Correct |
60 ms |
94168 KB |
Output is correct |
3 |
Correct |
51 ms |
94224 KB |
Output is correct |
4 |
Correct |
51 ms |
94184 KB |
Output is correct |
5 |
Correct |
63 ms |
94232 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
52 ms |
94372 KB |
Output is correct |
2 |
Correct |
53 ms |
94216 KB |
Output is correct |
3 |
Correct |
50 ms |
94248 KB |
Output is correct |
4 |
Correct |
57 ms |
94148 KB |
Output is correct |
5 |
Correct |
60 ms |
94172 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
63 ms |
95416 KB |
Output is correct |
2 |
Correct |
63 ms |
95272 KB |
Output is correct |
3 |
Correct |
74 ms |
96708 KB |
Output is correct |
4 |
Correct |
57 ms |
94776 KB |
Output is correct |
5 |
Correct |
58 ms |
94808 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
116 ms |
100492 KB |
Integer 0 violates the range [1, 256] |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1028 ms |
151096 KB |
Output is correct |
2 |
Runtime error |
2156 ms |
262144 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1047 ms |
151076 KB |
Output is correct |
2 |
Correct |
1938 ms |
181956 KB |
Output is correct |
3 |
Runtime error |
1934 ms |
262144 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
2001 ms |
262144 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1276 ms |
164260 KB |
Output is correct |
2 |
Runtime error |
1992 ms |
262144 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
922 ms |
262144 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
824 ms |
262144 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |