跳到正文

算法·理论

该值不应为空。

Fikuzza约 37 分钟
原文
文章信息与版本

文章信息

发布
2026年10月27日
收录
2026年10月27日
更新
2026年11月13日
篇幅
约 37 分钟

版本

  1. v1当前
Python
# Let's explore the graph / planar division by chords.
# A set of chords divides the circle into several faces (regions).
# Each face is bounded by:
# - some chords (straight line segments)
# - some circular arcs (along the boundary of the unit circle)

# Crucial question:
# Can a face contain MORE THAN ONE circular arc?
# Earlier I thought:
# "Consider the region between two parallel chords:
# Chord 1: y = 0.5
# Chord 2: y = -0.5
# The region between them has TWO circular arcs (left and right)!"
# WAIT! Does it?
# In that region, can you connect a point from the left arc to a point on the right arc?
# YES! The segment connecting them stays inside the strip -0.5 < y < 0.5!
# It doesn't intersect Chord 1 or Chord 2!
# BUT wait! What if chords intersect each other?

Python
# Let's think carefully:
# If two chords C1 and C2 intersect inside the circle:
# They form an "X".
# The 4 endpoints of C1 and C2 on the circle: A, B, C, D in cyclic order.
# C1 connects A and C. C2 connects B and D.
# They intersect at a point inside the circle.
# How many faces do they divide the circle into?
# 4 faces!
# Face 1: arc AB, segment AX, segment XB.
# Face 2: arc BC, segment BX, segment XC.
# Face 3: arc CD, segment CX, segment XD.
# Face 4: arc DA, segment DX, segment XA.
# Notice: Each of the 4 faces touches the boundary of the circle in EXACTLY ONE connected arc!
# Wait! Is it ALWAYS true that if chords intersect, faces touch at most one arc?
# What if we have 3 chords?
# Let's test with various chord arrangements.

Python
# Let's write code to construct the faces formed by chords in a circle.
# Let vertices on circle be angles.
# A chord connects angle a and angle b.
# Let's test if a face can have more than one arc of the circle.
# In general, the chords form a planar straight line graph (PSLG) inside the circle.
# The boundary of the circle has 2m endpoints of chords, dividing the circle into 2m arcs.
# Each arc of the circle belongs to EXACTLY ONE face!
# Can two arcs belong to the same face?
# YES, as we saw with two parallel (non-intersecting) chords!
# But what if there are intersecting chords?
# Can two arcs belong to the same face even when there are intersecting chords?
# Let's construct an example:
# Suppose we have Chord 1 and Chord 2 that intersect each other: (A, C) and (B, D).
# Now add Chord 3 that does not intersect them?
# Let's test whether two arcs can belong to the same face.

Python
# Let's verify:
# Suppose F is a 2D convex region (intersection of unit disc and halfplanes).
# The boundary of F is a simple closed curve.
# Points on the boundary of the unit disc that belong to F lie on the boundary of F.
# Since the boundary of F is a simple closed curve homeomorphic to a circle S^1,
# any subset of points on \partial F has a natural cyclic order!
# If you connect any two points u, v on \partial F by a line segment:
# Because F is convex, the open line segment (u, v) is contained in the interior of F.
# By Jordan Curve Theorem / convexity, the segment (u, v) divides F into exactly two connected components F1 and F2,
# both of which are convex!
# The boundary of F is divided into two open paths: Path 1 and Path 2.
# Any point w in \partial F \setminus {u, v} belongs to the boundary of F1 if w is on Path 1,
# and to the boundary of F2 if w is on Path 2!
#
# Therefore, IF a face F has k available vertices,
# then ANY pair of vertices (u, v) can be chosen,
# and choosing (u, v) splits the k vertices into two sets of sizes a and b,
# where a + b = k - 2, and a can be ANY value from 0 to k - 2!
#
# WAIT! Can ANY pair (u, v) be chosen?
# YES, because the segment uv is inside F, so it doesn't cross any existing chords!
# AND what about the cyclic order?
# In cyclic order along the boundary of F, say the vertices are 1, 2, ..., k.
# If you choose vertex i and vertex j, then the vertices on one side are {i+1, ..., j-1} (size a),
# and on the other side are {j+1, ..., i-1} (size k - 2 - a).
# Can 'a' be ANY value from 0 to k - 2?
# YES! For any desired a in {0, ..., k - 2}, just choose j = (i + a + 1).
# That always exists!
print("Is that true? Wow! That means ANY face F with k available points is IDENTICAL to a single game of size k in Dawson's chess (Game .07)!")

代码
Is that true? Wow! That means ANY face F with k available points is IDENTICAL to a single game of size k in Dawson's chess (Game .07)!

Python
# Let's think:
# When you have a face with k points on the circle.
# If you draw a chord between point x and point y:
# Does this chord remove ONLY points x and y?
# YES, points x and y are occupied!
# But what happens to the region?
# The region is SPLIT into two regions!
# One region contains the points between x and y along the boundary (say a points).
# The other region contains the remaining points (say b points, where b = k - 2 - a).
# In future turns:
# Any chord drawn between points in Region 1 stays entirely in Region 1!
# Any chord drawn between points in Region 2 stays entirely in Region 2!
# A chord CANNOT connect a point in Region 1 and a point in Region 2, because that chord would INTERSECT the chord xy!
# Because the chord xy connects x and y, and divides the face into Region 1 and Region 2!
# Any path between a point in Region 1 and a point in Region 2 must cross chord xy!
# Therefore, Region 1 and Region 2 become TWO INDEPENDENT GAMES!
# And the sizes of these games are a and b, where a + b = k - 2!
# Since you can choose ANY two points x and y,
# the number of points between x and y can be ANY integer a from 0 to k - 2!
# Thus, a move from a state of size k consists of choosing ANY a in {0, 1, ..., k - 2},
# and transitioning to the independent sum of a state of size a and a state of size k - 2 - a!
# That means:
# SG(k) = mex { SG(a) ^ SG(k - 2 - a) : 0 <= a <= k - 2 } !
# THAT IS EXACTLY THE DEFINITION OF DAWSON'S CHESS / GAME .07!
print("SG(k) = mex { SG(a) ^ SG(k - 2 - a) : 0 <= a <= k - 2 }")

代码
SG(k) = mex { SG(a) ^ SG(k - 2 - a) : 0 <= a <= k - 2 }

Python
# Let's check the examples in the image:
# Example 1:
# 2
# 2 0 -> n = 2, m = 0. Output: YES
# 5 0 -> n = 5, m = 0. Output: NO

# Let's check:
# n = 2, m = 0: k = 2. SG(2) = f[2] = 1 != 0 -> YES. Correct!
# n = 5, m = 0: k = 5. SG(5) = f[5] = 0 == 0 -> NO. Correct!

# Example 2:
# 2
# 8 1
# 0 4 -> n = 8, m = 1, chord (0, 4). Output: NO
# 11 2
# 0 6
# 8 3 -> n = 11, m = 2, chords (0, 6) and (8, 3). Output: YES

# Let's analyze:
# In Example 2, Case 1: n = 8, m = 1, chord (0, 4).
# Endpoints used: 0, 4.
# Points remaining:
# Between 0 and 4: 1, 2, 3 (3 points).
# Between 4 and 0: 5, 6, 7 (3 points).
# The chord (0, 4) divides the circle into 2 faces:
# Face 1 has 3 points. SG(3) = f[3] = 1.
# Face 2 has 3 points. SG(3) = f[3] = 1.
# Total SG = SG(3) ^ SG(3) = 1 ^ 1 = 0.
# SG = 0 -> NO!
# The output for Case 1 is indeed NO!

# In Example 2, Case 2: n = 11, m = 2, chords (0, 6) and (8, 3).
# Wait! (0, 6) and (8, 3) intersect!
# Endpoints: 0, 3, 6, 8.
# Points between 8 and 0: 9, 10 (2 points).
# Points between 0 and 3: 1, 2 (2 points).
# Points between 3 and 6: 4, 5 (2 points).
# Points between 6 and 8: 7 (1 point).
# The two chords intersect, dividing the circle into 4 faces.
# Each face has one arc:
# Face 1: 2 points -> SG(2) = 1.
# Face 2: 2 points -> SG(2) = 1.
# Face 3: 2 points -> SG(2) = 1.
# Face 4: 1 point -> SG(1) = 0.
# Total SG = 1 ^ 1 ^ 1 ^ 0 = 1 != 0 -> YES!
# The output for Case 2 is indeed YES!
print("Matches both examples perfectly!")

代码
Matches both examples perfectly!

Python
# When do two elementary arcs belong to the same face?
# Let's consider the planar graph formed by the circle boundary and the chords.
# Actually, the chords are inside the circle.
# An elementary arc is on the boundary of the circle.
# Consider the chords as edges of a planar graph.
# The boundary of the circle connects the endpoints of chords in cyclic order.
# BUT wait! The circle boundary is NOT an obstacle for moves! The circle boundary is the boundary of the disc.
# Any face of the chord arrangement that touches the circle boundary contains some elementary arcs.
# Question: Can a face of the chord arrangement contain TWO OR MORE elementary arcs?
# Let's check:
# Two chords (A, B) and (C, D) that do NOT intersect.
# Suppose in cyclic order: A, C, D, B.
# Chord 1 is AB. Chord 2 is CD.
# Chord CD is "inside" the arc AB.
# Region 1: between arc CD and chord CD. (Contains 1 arc: arc CD).
# Region 2: outside chord AB (between arc BA and chord AB). (Contains 1 arc: arc BA).
# Region 3: between chord AB, chord CD, arc AC, and arc DB!
# LOOK AT REGION 3!
# Region 3 touches the circle at arc AC AND arc DB!
# That's TWO arcs!
print("Region 3 touches the circle at arc AC and arc DB!")

代码
Region 3 touches the circle at arc AC and arc DB!

Python
# Let's consider a chord arrangement.
# Let the unit circle be {z in C : |z| <= 1}.
# Chords are straight line segments between points on the unit circle.
# Let's think topologically:
# When can two boundary arcs of the unit circle belong to the same face?
# Suppose arc 1 is (p1, p2) and arc 2 is (p3, p4).
# If arc 1 and arc 2 belong to the same face F,
# then F is a connected (in fact convex) set whose boundary contains both arc 1 and arc 2.
# Between arc 1 and arc 2 along the circle, say between p2 and p3:
# There is another boundary component of the circle.
# Why is that boundary component NOT part of F?
# Because some chord starts at p2 or between p2 and p3!
# In fact, the boundary of F must leave the circle at p2 along some chord,
# and it must re-enter the circle at p3 along some chord!
# Wait, can a single chord go from p2 to p3?
# If a chord goes from p2 to p3, then it cuts off the arc between p2 and p3!
# That chord forms a boundary edge of F!
# What if there is a sequence of chords?
# Can the boundary of F consist of:
# chord from p2 to x, chord from x to y, ..., chord from w to p3?
# BUT WAIT!
# Where can chords intersect? Chords are initial lines.
# If F is a face of the line arrangement inside the disc:
# The edges of F are either:
# 1) circular arcs on the boundary of the disc.
# 2) straight line segments along the chords.
# For each chord line L:
# Does L go all the way from one side of the disc to the other side?
# YES! EVERY CHORD IS A FULL CHORD OF THE CIRCLE!
# That is: each chord has both its endpoints on the boundary of the circle!
# Therefore, each chord line L divides the entire disc into TWO half-discs!
# A face F is the intersection of the disc D with a set of half-planes:
# F = D \cap (\bigcap_{i=1}^m H_i)
# where H_i is one of the two half-planes defined by chord i!
print("EVERY FACE F is D \\cap (\\bigcap_{i=1}^m H_i)!")

代码
EVERY FACE F is D \cap (\bigcap_{i=1}^m H_i)!

Python
# Can a convex polygon P intersect the boundary of the unit circle in more than one arc?
# Wait! We ALREADY found an example earlier!
# The strip between y = -0.5 and y = 0.5 is a convex polygon (intersection of two half-planes)!
# Its intersection with the circle x^2 + y^2 = 1 is TWO ARCS:
# Arc 1: -pi/6 to pi/6 (x > 0)
# Arc 2: 5pi/6 to 7pi/6 (x < 0)
# Both arcs belong to P \cap \partial D!
# In this case, P = { (x, y) : -0.5 <= y <= 0.5 }.
# Here, m = 2 chords:
# Chord 1: y = 0.5 (endpoints at angles pi/6 and 5pi/6)
# Chord 2: y = -0.5 (endpoints at angles -pi/6 and -5pi/6 = 7pi/6)
# Notice: Chord 1 and Chord 2 DO NOT INTERSECT!
# What if Chord 1 and Chord 2 DO intersect?
# Let's test with intersecting chords!

Python
# Suppose we have a set of chords, some of which may intersect.
# How many arcs can P \cap \partial D have?
# Each arc of P \cap \partial D is an interval of angles on the circle.
# An arc ends when a chord boundary of P hits the circle.
# That is, the endpoints of the arcs of P \cap \partial D must be endpoints of the chords that form edges of P!
# Specifically, if a chord is an edge of P, and its endpoints are on the circle,
# does that chord endpoint connect two edges of P?
# Let's see: if chord L is an edge of P, and both endpoints of L are on the circle:
# At each endpoint of L, the circle is intersected by L.
# Since L is a chord of the circle, one side of L is in H, the other side is NOT in H.
# So the arc on the other side of L is EXCLUDED from P!
# Can P contain the arc on the other side of L? No, because L is a bounding halfplane!
# So for each chord L that bounds P:
# L cuts off an arc of the circle. That cut-off arc is NOT in P!
# What remains of the circle is the boundary of the circle MINUS the arcs cut off by all chords bounding P!
# WAIT!
# Let chord i have endpoints A_i and B_i.
# Chord i divides the circle into two arcs: Arc_i^+ and Arc_i^-.
# Halfplane H_i contains one of the arcs, say Arc_i^+, and does NOT contain Arc_i^-.
# Wait, DOES H_i CONTAIN Arc_i^+?
# NO!
# The chord connects A_i and B_i.
# The straight line segment A_i B_i is inside the disc.
# One side of the chord contains the minor arc (or major arc).
# BUT THE CHORD ITSELF IS A STRAIGHT LINE!
# Is the arc on the side of the halfplane?
# YES! The halfplane H_i is { (x,y) : a x + b y + c >= 0 }.
# The circle is convex.
# The line intersects the circle at A_i and B_i.
# On one side of the line, the arc of the circle is on that side!
# On the other side of the line, the arc of the circle is on the other side!
# So the circle boundary \partial D \cap H_i is a SINGLE CONNECTED ARC of the circle!
# WOW!
# For EACH chord i:
# \partial D \cap H_i is an arc of the circle!
# Therefore:
# \partial D \cap P = \partial D \cap (\bigcap_{i=1}^m H_i) = \bigcap_{i=1}^m (\partial D \cap H_i) !
# THE INTERSECTION OF ARCS OF A CIRCLE!
# WAIT A MINUTE!
# IS THAT TRUE?!
# Let's check!
print("Is \partial D \cap (\bigcap H_i) equal to \bigcap (\partial D \cap H_i)?")
# By set theory:
# S \cap (\bigcap A_i) = \bigcap (S \cap A_i) FOR ANY SETS!
# ALWAYS TRUE FOR ANY SETS S and A_i!
# Because x in S \cap (\bigcap A_i) <=> x in S and (for all i, x in A_i) <=> for all i, (x in S and x in A_i) <=> x in \bigcap (S \cap A_i)!
print("YES! THAT IS A BASIC SET IDENTITY!")

代码
Is \partial D \cap (igcap H_i) equal to igcap (\partial D \cap H_i)?
YES! THAT IS A BASIC SET IDENTITY!

Python
# Let's understand how chords divide a circle into faces.
# Specifically, we have m chords.
# Each chord connects two points on the boundary of the unit circle.
# All 2m endpoints are distinct.
#
# Let's consider the planar map inside the circle.
# Vertices of the planar map:
# 1) The 2m endpoints on the circle.
# 2) The intersection points of chords in the interior of the circle.
# Edges:
# - segments of the chords between intersection points / endpoints
# - the 2m circular arcs connecting adjacent endpoints on the circle.
#
# Notice: EACH of the 2m circular arcs belongs to EXACTLY ONE face!
# Wait!
# Does an interior intersection point of chords connect to ANY arc of the circle?
# Let's see: how many faces touch the boundary of the circle?
# Does EVERY face that contains at least one unused point touch the boundary of the circle?
# YES, because ALL unused points lie on the boundary of the circle!
# Any face that does NOT touch the boundary of the circle is an interior polygon,
# which has 0 unused points on its boundary!
# Since SG(0) = 0, any interior face with 0 points has SG value 0, and contributes nothing to the XOR sum!
# What about faces that touch the boundary of the circle?
# Which arcs belong to the same face?

在连线博弈中,初始线段相交绝不能直接判 YES**。面对相交情况,利用平面区域的凸性**与异或哈希(Zobrist Hashing)即可在 O(mlog⁡m)O(m \log m) 内完美解决。


一、相交线段的几何与博弈本质

  1. 每个区域都是独立的凸多边形:
  • 整个圆盘是凸的,每条弦都是一条贯穿圆盘的直线,将圆盘分为两个半平面(凸集)。
  • 无论初始线段如何相交,它们将圆盘内部划分出的每一个面(Face)都是若干半平面的交集——任意一个面在几何上都是严格的凸区域。
  1. 同一个面内的所有点构成一个独立的子游戏:
  • 凸区域内部任意两点之间的连线段必定严格位于该区域内部,绝不会穿透该区域的边界(即不会与任何已有线段相交)。
  • 在同一个面的边界上任意连一条线,依然会将该面的空闲点分为大小为 aa 和 b=K−2−ab = K - 2 - a 的两部分(0≤a≤K−20 \le a \le K - 2)。
  • 因此,无论一个面包含多少段不连续的圆弧,只要属于同一个面的空闲点总数为 KK,它就严格等价于一个大小为 KK 的 Dawson’s Chess(八进制游戏 .07)单游戏,其值为 SG(K)SG(K)。
  • 整场游戏的胜负由所有面的 SG 异或和决定:SGtotal=⨁FSG(KF)SG_{\text{total}} = \bigoplus_{F} SG(K_F)。

二、如何快速求出“哪些圆弧属于同一个面”

这是处理相交的核心难点。但利用半平面交的性质,结论极为简洁:

充要条件:圆周上的两段圆弧属于同一个面,当且仅当它们位于所有 mm 条线段的同一侧。

异或哈希算法

  1. 初始的 mm 条线段共有 2m2m 个端点,将圆周切分成 2m2m 段基本圆弧。
  2. 为每条线段 i∈[1,m]i \in [1, m] 分配一个 64 位随机权值 wiw_i。
  3. 从 00 开始沿圆周逆时针走:
  • 每经过线段 ii 的一个端点,我们在该线段的两侧发生了一次翻转,即让当前的状态哈希值 cur_hash ^= w[i]。
  • 则当前基本圆弧的特征哈希值即为 cur_hash。
  1. 哈希值相同的圆弧,必属于圆内部的同一个连通面。将具有相同哈希值的圆弧长度累加,得到该面包含的空闲点总数 KK,再异或 SG(K)SG(K) 即可。

三、完整 AC 代码

C++
#include <bits/stdc++.h>
#define int long long
using namespace std;

const int N = 2e5 + 10;
int n, m;

// Dawson's Chess (Game .07) SG 表 (前 69 项与周期 34)
int f[] = {0,0,1,1,2,0,3,1,1,0,3,3,2,2,4,0,5,2,2,3,3,0,1,1,3,0,2,1,1,0,4,5,2,7,4,0,1,1,2,0,3,1,1,0,3,3,2,2,4,4,5,5,2,3,3,0,1,1,3,0,2,1,1,0,4,5,3,7,4};
int g[] = {0,8,1,1,2,0,3,1,1,0,3,3,2,2,4,4,5,5,9,3,3,0,1,1,3,0,2,1,1,0,4,5,3,7,4};

int get_sg(int v) {
    if (v <= 0) return 0;
    if (v > 68) return g[(v % 34 ? v % 34 : 34)];
    return f[v];
}

// 64-bit 伪随机生成器
mt19937_64 rng(13331);
unsigned long long rnd() {
    return rng();
}

struct Point {
    int pos, id;
    bool operator<(const Point &other) const {
        return pos < other.pos;
    }
} p[N * 2];

unsigned long long w[N];

void solve() {
    cin >> n >> m;
    if (m == 0) {
        if (get_sg(n)) cout << "YES\n";
        else cout << "NO\n";
        return;
    }

    for (int i = 1; i <= m; i++) {
        int u, v;
        cin >> u >> v;
        p[2 * i - 1] = {u, i};
        p[2 * i] = {v, i};
        w[i] = rnd();
    }

    sort(p + 1, p + 2 * m + 1);

    // 统计各哈希值对应的空闲点总数
    map<unsigned long long, int> face_pts;
    unsigned long long cur_hash = 0;

    for (int i = 1; i <= 2 * m; i++) {
        cur_hash ^= w[p[i].id]; // 经过端点,跨越对应线段
        
        // 计算当前端点与下一个端点之间的空闲点数
        int nxt_pos = (i == 2 * m) ? p[1].pos : p[i + 1].pos;
        int len = (nxt_pos - p[i].pos - 1 + n) % n;
        
        face_pts[cur_hash] += len;
    }

    int sg_sum = 0;
    for (auto &it : face_pts) {
        sg_sum ^= get_sg(it.second);
    }

    if (sg_sum) cout << "YES\n";
    else cout << "NO\n";
}

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int T;
    if (cin >> T) {
        while (T--) solve();
    }
    return 0;
}
原文