破环为链…
然后求出每个边最优情况下右边的边。
然后倍增贪心即可…
c++代码如下:
#include<bits/stdc++.h>
#define lowbit(x) (x & -x)
#define rep(i,x,y) for(register int i = x ; i <= y ; ++ i)
#define repd(i,x,y) for(register int i = x ; i >= y ; -- i)
using namespace std;
typedef long long ll;
template<typename T>inline void read(T&x)
{
x = 0;char c;int sign = 1;
do { c = getchar(); if(c == '-') sign = -1; }while(!isdigit(c));
do { x = x * 10 + c - '0'; c = getchar(); }while(isdigit(c));
x *= sign;
}
const int N = 4e5 + 500;
int n,m,ans[N];
int p[N][26];
struct Str { int u,v,id; }edge[N];
inline bool cmp(Str a,Str b) { return a.u < b.u; }
int main()
{
read(m); read(n);
rep(i,1,m)
{
read(edge[i].u); read(edge[i].v);
if(edge[i].v < edge[i].u) edge[i].v += n;
edge[i].id = i;
}
sort(edge + 1 ,edge + 1 + m,cmp);
rep(i,1,m)
{
edge[i + m].u = edge[i].u + n;
edge[i + m].v = edge[i].v + n;
}p[m*2][0] = m*2;
int j = 1;
rep(i,1,2*m - 1)
{
while(j < 2*m && edge[j + 1].u <= edge[i].v) ++ j;
p[i][0] = j;
}
rep(j,1,25)
rep(i,1,2*m)
p[i][j] = p[p[i][j - 1]][j - 1];
rep(i,1,m)
{
int num = 0,x = i;
repd(j,25,0)
if(p[x][j] < i + m)
num += (1 << j),x = p[x][j];
ans[edge[i].id] = num + 1;
}
rep(i,1,m) printf("%d ",ans[i]);
return 0;
}