题解:P6900 [ICPC 2014 WF] Sensor Network
题解
乍一看是计算几何,但是我们考虑去枚举最大团的最远点对之后,合法的点集是分别以两点为圆心做圆后的公共部分(那是一个眼睛一样的形状)。我们现在连接两个圆心,就把“眼睛”分成了两个部分,每个部分中的点互相都可以同时选,但是两个部分中的某些点不能同时选。如果我们将不能同时选择的点连边,这个就是一个非常显然的二分图,并且我们需要去求其最大独立集。于是直接网络流启动即可。时间复杂度就是网络流的复杂度加上枚举点对的
代码
int n, D, cnt, hd[N], cur[N], d[N], s, t;
struct edge{int nxt, to, w;}e[N * N << 4];
inline void clr(){memset(hd, 0, sizeof hd); cnt = 1;}
inline void add(int u, int v, int w){e[++cnt] = {hd[u], v, w}; hd[u] = cnt;}
inline void addedge(int u, int v, int w){add(u, v, w); add(v, u, 0);}
inline bool bfs(){
for(int i = 1; i <= n; ++i)d[i] = inf;
queue < int > q; q.push(s); d[s] = 0;
while(!q.empty()){
int u = q.front(); q.pop(); cur[u] = hd[u];
for(int i = hd[u], v; i; i = e[i].nxt)
if(d[v = e[i].to] == inf and e[i].w > 0)d[v] = d[u] + 1, q.push(v);
}
return d[t] != inf;
}
inline int dfs(int u, int flow){
if(u == t)return flow; int res = 0;
for(int &i = cur[u], v, w, k; i; i = e[i].nxt)
if((w = e[i].w) > 0 and d[v = e[i].to] == d[u] + 1 and (k = dfs(v, min(flow, w)))){
res += k; flow -= k; e[i].w -= k; e[i ^ 1].w += k;
if(! flow)return res;
}
if(! res)d[u] = inf; return res;
}
inline int dinic(){
int res = 0, k;
while(bfs())while(k = dfs(s, inf))res += k;
return res;
}
bool vs[N];
inline void dfs(int u){
vs[u] = true;
for(int i = hd[u], v; i; i = e[i].nxt)
if(e[i].w > 0 and ! vs[v = e[i].to])dfs(v);
}
struct node{
int x, y; inline node(int _x = 0, int _y = 0) : x(_x), y(_y){}
friend inline node operator - (const node &a, const node &b){
return node(a.x - b.x, a.y - b.y);
}
friend inline int operator ^ (const node &a, const node &b){
return a.x * b.y - a.y * b.x;
}
}a[N];
inline int sq(int x){return x * x;}
inline int dis(const node &a, const node &b){return sq(a.x - b.x) + sq(a.y - b.y);}
inline bool sid(const node &a, const node &b){return (a ^ b) < 0;}
inline bool px(const node &a, const node &b){return (a ^ b) == 0;}
vector < int > ans;
int tg[N];
inline void calc(int x, int y){
vector < int > res; int lim = dis(a[x], a[y]);
res.push_back(x); res.push_back(y); clr();
for(int i = 1; i <= n; ++i)tg[i] = - 1; s = x; t = y;
for(int i = 1; i <= n; ++i)if(i != x and i != y and dis(a[x], a[i]) <= lim and dis(a[y], a[i]) <= lim)
if(px(a[i] - a[x], a[y] - a[x]))res.push_back(i);
else tg[i] = sid(a[i] - a[x], a[y] - a[x]);
for(int i = 1; i <= n; ++i)if(~ tg[i])tg[i] ? addedge(s, i, 1) : addedge(i, t, 1);
for(int i = 1; i <= n; ++i)if(~ tg[i])for(int j = i + 1; j <= n; ++j)if(~ tg[j] and tg[i] != tg[j] and dis(a[i], a[j]) > lim)
tg[i] ? addedge(i, j, 1) : addedge(j, i, 1);
dinic(); for(int i = 1; i <= n; ++i)vs[i] = false; dfs(s);
for(int i = 1; i <= n; ++i)if(~ tg[i] and tg[i] == vs[i])res.push_back(i);
if(res.size() > ans.size())ans = res;
}
signed main(){
n = rd(), D = rd(); ans.push_back(1);
for(int i = 1; i <= n; ++i)a[i].x = rd(), a[i].y = rd();
for(int i = 1; i < n; ++i)for(int j = i + 1; j <= n; ++j)
if(dis(a[i], a[j]) <= D * D)calc(i, j);
wt(ans.size()), pc('\n'); for(int x : ans)wt(x), pc(' ');
return 0;
}
// 于是纵横万相穷通 也守心底灵通
// 合眼识得星沉地动 也岿然不动
// 敢令岁月乌有 逍遥长驻 敢归云间宿
// 遥祝远行人 有道不孤