#include<bits/stdc++.h>
using namespace std;
typedef long double ld;
#define int long long
#define rep(a, b) for(int a = 0; a < (b); ++a)
#define st first
#define nd second
#define pb push_back
#define all(a) a.begin(), a.end()
const int LIM=5e5+7;
vector<pair<int,int>>V[LIM];
vector<int>cykl;
pair<int,int>T[LIM];
int ans[LIM], li[LIM], odw[LIM], ile[LIM], l, r, m;
bool ok(vector<int>kraw) {
if(kraw.size()==0) return true;
vector<int>wierz;
for(auto i : kraw) {
wierz.pb(T[i].st);
wierz.pb(T[i].nd);
}
sort(all(wierz));
rep(i, wierz.size()-1) if(wierz[i]==wierz[i+1]) return false;
return true;
}
void DFS(int x) {
while(li[x]<V[x].size()) {
if(odw[V[x][li[x]].nd]) {
++li[x];
continue;
}
odw[V[x][li[x]].nd]=1;
++li[x];
cykl.pb(V[x][li[x]-1].nd);
DFS(V[x][li[x]-1].st);
}
}
void dc(vector<int>kraw, int x) {
if(ok(kraw)) return;
vector<int>P, wierz, lewo, prawo;
for(auto i : kraw) {
V[T[i].st].clear();
V[T[i].nd].clear();
li[T[i].st]=ile[T[i].st]=0;
li[T[i].nd]=ile[T[i].nd]=0;
odw[i]=0;
P.pb(T[i].st);
P.pb(T[i].nd);
}
for(auto i : kraw) {
V[T[i].st].pb({T[i].nd, i});
V[T[i].nd].pb({T[i].st, i});
++ile[T[i].st];
++ile[T[i].nd];
}
sort(all(P));
wierz.pb(P[0]);
rep(i, P.size()-1) if(P[i]!=P[i+1]) wierz.pb(P[i+1]);
for(auto i : wierz) if(ile[i]%2==1) {
if(i<l) lewo.pb(i);
else prawo.pb(i);
}
rep(i, lewo.size()) {
V[lewo[i]].pb({prawo[i], m});
V[prawo[i]].pb({lewo[i], m});
}
vector<int>V1, V2;
for(auto i : wierz) {
cykl.clear();
DFS(i);
rep(j, cykl.size()) if(cykl[j]!=m) {
if(j%2==0) V1.pb(cykl[j]);
else V2.pb(cykl[j]);
}
}
for(auto i : V1) ans[i]+=x;
dc(V1, 2*x);
dc(V2, 2*x);
}
int32_t main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> l >> r >> m;
vector<int>kraw;
rep(i, m) {
cin >> T[i].st >> T[i].nd;
--T[i].st; --T[i].nd;
T[i].nd+=l;
kraw.pb(i);
}
dc(kraw, 1);
int ma=0;
rep(i, m) ma=max(ma, ans[i]);
cout << ma+1 << '\n';
rep(i, m) cout << ans[i]+1 << '\n';
}
Compilation message
teoreticar.cpp: In function 'bool ok(std::vector<long long int>)':
teoreticar.cpp:5:36: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
5 | #define rep(a, b) for(int a = 0; a < (b); ++a)
| ^
teoreticar.cpp:23:2: note: in expansion of macro 'rep'
23 | rep(i, wierz.size()-1) if(wierz[i]==wierz[i+1]) return false;
| ^~~
teoreticar.cpp: In function 'void DFS(long long int)':
teoreticar.cpp:27:13: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
27 | while(li[x]<V[x].size()) {
| ~~~~~^~~~~~~~~~~~
teoreticar.cpp: In function 'void dc(std::vector<long long int>, long long int)':
teoreticar.cpp:5:36: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
5 | #define rep(a, b) for(int a = 0; a < (b); ++a)
| ^
teoreticar.cpp:58:2: note: in expansion of macro 'rep'
58 | rep(i, P.size()-1) if(P[i]!=P[i+1]) wierz.pb(P[i+1]);
| ^~~
teoreticar.cpp:5:36: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
5 | #define rep(a, b) for(int a = 0; a < (b); ++a)
| ^
teoreticar.cpp:63:2: note: in expansion of macro 'rep'
63 | rep(i, lewo.size()) {
| ^~~
teoreticar.cpp:5:36: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
5 | #define rep(a, b) for(int a = 0; a < (b); ++a)
| ^
teoreticar.cpp:71:3: note: in expansion of macro 'rep'
71 | rep(j, cykl.size()) if(cykl[j]!=m) {
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
17 ms |
24308 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
18 ms |
24268 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
17 ms |
13536 KB |
too many colors |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
13380 KB |
Output is correct |
2 |
Runtime error |
26 ms |
26532 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1602 ms |
101088 KB |
too many colors |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1662 ms |
101048 KB |
too many colors |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3653 ms |
119252 KB |
Output is correct |
2 |
Incorrect |
4670 ms |
158372 KB |
too many colors |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2685 ms |
114728 KB |
too many colors |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
244 ms |
100604 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2048 ms |
94320 KB |
too many colors |
2 |
Halted |
0 ms |
0 KB |
- |