This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <vector>
#include <utility>
#include <algorithm>
#include <cstring>
using namespace std;
typedef pair <int, int> pii;
#define x first
#define y second
#define mp make_pair
int l, r, m, sad, boja[1000005], vr[1000005];
vector <pii> edg;
vector <int> ms[200005];
bool bio[1000005];
void dfs(int x){
while(!ms[x].empty()){
int idx = ms[x].back();
pii o = edg[idx];
ms[x].pop_back();
int y = (o.x == x ? o.y : o.x);
if(bio[idx])
continue;
bio[idx] = 1;
vr[idx] = sad;
sad ^= 1;
dfs(y);
}
}
vector <int> solve(vector <pii> e){
if(e.empty())
return {};
vector <int> lt, rt;
//cout << e.size() << endl;
for(int i = 0 ; i < e.size() ; ++i){
//cout << e[i].x << ' ' << e[i].y << endl;
int x = e[i].x, y = e[i].y;
lt.push_back(x);
rt.push_back(y);
}
//cout << endl;
sort(lt.begin(), lt.end());
sort(rt.begin(), rt.end());
lt.resize(unique(lt.begin(), lt.end()) - lt.begin());
rt.resize(unique(rt.begin(), rt.end()) - rt.begin());
edg.clear();
for(int i = 0 ; i <= l + r + 1 ; ++i){
ms[i].clear();
}
for(int i = 0 ; i < e.size() ; ++i){
int x = e[i].x, y = e[i].y;
x = lower_bound(lt.begin(), lt.end(), x) - lt.begin();
y = lower_bound(rt.begin(), rt.end(), y) - rt.begin();
y += l + 1;
edg.push_back(mp(x, y));
ms[x].push_back(i);
ms[y].push_back(i);
}
vector <int> tc;
int maxi = 0;
for(int i = 0 ; i <= l + r + 1 ; ++i){
maxi = max(maxi, (int)ms[i].size());
if(ms[i].size() & 1){
tc.push_back(i);
}
}
bool p = 1;
for(int i = 0 ; i <= l + r + 1 ; ++i){
p &= (ms[i].size() <= 1);
}
if(p){
vector <int> ret;
for(int i = 0 ; i < e.size() ; ++i){
ret.push_back(1);
}
return ret;
}
int idx = e.size();
for(int i = 0 ; i < tc.size() ; ++i){
int x = tc[i];
//cout << x << " : ";
if(x <= l){
edg.push_back(mp(x, l + r + 1));
ms[x].push_back(idx);
ms[l + r + 1].push_back(idx);
idx++;
}
else{
edg.push_back(mp(l, x));
ms[l].push_back(idx);
ms[x].push_back(idx);
idx++;
}
}
//cout << endl;
if(ms[0].size() & 1){
edg.push_back(mp(0, l + r + 1));
ms[0].push_back(idx);
ms[l + r + 1].push_back(idx);
idx++;
}
memset(vr, -1, sizeof(vr));
memset(bio, 0, sizeof(bio));
for(int i = 0 ; i <= l + r + 1 ; ++i){
if(!ms[i].empty()){
dfs(i);
}
}
vector <pii> lc, rc;
vector <int> tvr;
for(int i = 0 ; i < (int)e.size() ; ++i){
tvr.push_back(vr[i]);
if(!vr[i]){
lc.push_back(e[i]);
}
else{
rc.push_back(e[i]);
}
}
vector <int> sl = solve(lc), sr = solve(rc);
vector <int> ret;
int br1 = 0, br2 = 0, val = *max_element(sl.begin(), sl.end());
for(int i = 0 ; i < (int)e.size() ; ++i){
if(!tvr[i]){
ret.push_back(sl[br1++]);
}
else{
ret.push_back(sr[br2++] + val);
}
}
return ret;
}
int main ()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> l >> r >> m;
int br = 0;
for(int i = 0 ; i < m ; ++i){
int x, y;
cin >> x >> y;
x--;
y--;
edg.push_back(mp(x, y));
}
vector <int> sol = solve(edg);
cout << *max_element(sol.begin(), sol.end()) << endl;
for(int i = 0 ; i < sol.size() ; ++i){
cout << sol[i] << endl;
}
return 0;
}
Compilation message (stderr)
teoreticar.cpp: In function 'std::vector<int> solve(std::vector<std::pair<int, int> >)':
teoreticar.cpp:39:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0 ; i < e.size() ; ++i){
~~^~~~~~~~~~
teoreticar.cpp:54:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0 ; i < e.size() ; ++i){
~~^~~~~~~~~~
teoreticar.cpp:77:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0 ; i < e.size() ; ++i){
~~^~~~~~~~~~
teoreticar.cpp:83:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0 ; i < tc.size() ; ++i){
~~^~~~~~~~~~~
teoreticar.cpp: In function 'int main()':
teoreticar.cpp:154:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0 ; i < sol.size() ; ++i){
~~^~~~~~~~~~~~
teoreticar.cpp:144:5: warning: unused variable 'br' [-Wunused-variable]
int br = 0;
^~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |