当前位置:网站首页>[acwing] 909. Chess game
[acwing] 909. Chess game
2022-07-24 15:34:00 【Record algorithm solution】
Title address :
https://www.acwing.com/problem/content/911/
Given a directed acyclic graph as a chessboard , There are... On the chessboard M M M A chess piece , Multiple pieces can be placed on the same point in the chessboard . Two players play games on the chessboard , Each player needs to move any piece along a directed edge to any point adjacent to it every round . When it's a player's turn , If the player cannot move any pieces , It is regarded as a game failure . Will you be the first to win .
Input format :
The first line contains integers N N N, There are... In the diagram N N N A little bit .
Next N N N That's ok , Each line contains several integers , The first integer X i X_i Xi Indicates from number i i i The number of directed edges starting from the point of , Next X i X_i Xi A number represents the end of each directed edge .
The number of the point is 0 0 0 To N − 1 N−1 N−1, Above N N N Row data is introduced in sequence by 0 ∼ N − 1 0∼N−1 0∼N−1 The number of directed edges starting from point No. and the end point of each edge .
The next few lines are asking , Each line first contains an integer M M M, Indicates the number of pieces in a group of queries , Next it contains M M M It's an integer , Express M M M The number of the initial point where the pieces are located .
The last line is an integer 0 0 0, End of input .
Output format :
Each group of inquiry outputs a result , Each result takes up one line , If you win first, you will output WIN, Otherwise output LOSE.
Data range :
1 ≤ N ≤ 10000 1≤N≤10000 1≤N≤10000
1 ≤ M ≤ 10 1≤M≤10 1≤M≤10
The data guarantees that the number of edges in the graph will not exceed 1 0 5 10^5 105 strip .
The number of inquiries will not exceed 10000 10000 10000 Time .
It can be used SG Functions do . Reference resources https://blog.csdn.net/qq_46105170/article/details/114006814. For the digraph of this problem , You can find out the of each point first SG Function value , Because the pieces are independent of each other , It can be seen as M M M Two independent games are playing at the same time , So by the SG Theorem , Just put the initial position of all the pieces SG Function to do XOR , If it's not zero , First hand wins , Otherwise, we will lose first . The code is as follows :
#include <iostream>
#include <cstring>
#include <unordered_set>
using namespace std;
const int N = 10010, M = N * 10;
int n, m;
int h[N], e[M], ne[M], idx;
int f[N];
void add(int a, int b) {
e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}
int sg(int x) {
if (~f[x]) return f[x];
unordered_set<int> st;
for (int i = h[x]; ~i; i = ne[i])
st.insert(sg(e[i]));
for (int i = 0;; i++)
if (!st.count(i))
return f[x] = i;
}
int main() {
memset(h, -1, sizeof h);
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &m);
while (m--) {
int x;
scanf("%d", &x);
add(i, x);
}
}
memset(f, -1, sizeof f);
while (cin >> m, m) {
int res = 0;
while (m--) {
int x;
scanf("%d", &x);
res ^= sg(x);
}
res ? puts("WIN") : puts("LOSE");
}
}
Preprocessing time complexity O ( N M ) O(NM) O(NM)( Can calculate SG The time of the function is counted as preprocessing ), The time complexity of each inquiry O ( M ) O(M) O(M), Space O ( N M ) O(NM) O(NM).
边栏推荐
- Android SQLite database practice
- 图的存储和遍历
- 2022 robocom world robot developer competition - undergraduate group (provincial competition) -- question 2: intelligent medication assistant (finished)
- Experience summary of slow SQL problems
- [machine learning basics] - another perspective to explain SVM
- 华为无线设备配置WAPI-证书安全策略
- 2022 robocom world robot developer competition - undergraduate group (provincial competition) -- question 1: don't waste gold (finished)
- 2022 RoboCom 世界机器人开发者大赛-本科组(省赛)-- 第五题 树与二分图 (已完结)
- 力扣 31.下一个排列--双指针法
- [shaders realize pixelate mosaic effect _shader effect Chapter 7]
猜你喜欢

2022 robocom world robot developer competition - undergraduate group (provincial competition) -- question 1: don't waste gold (finished)

JMeter - call the interface for uploading files or pictures

Mlx90640 infrared thermal imager temperature measurement module development notes (III)

Here comes the problem! Unplug the network cable for a few seconds and plug it back in. Does the original TCP connection still exist?

Existence form and legitimacy of real data in C language (floating point number)

Huawei wireless device configuration wpa2-802.1x-aes security policy

【TA-霜狼_may-《百人计划》】图形3.4 延迟渲染管线介绍

Research on stability of time-delay systems based on Lambert function

2022 RoboCom 世界机器人开发者大赛-本科组(省赛)-- 第五题 树与二分图 (已完结)

Nine key measures to maintain server security in Hong Kong
随机推荐
C# 无操作则退出登陆
【ACWing】909. 下棋游戏
【机器学习基础】——另一个视角解释SVM
Android SQLite database practice
[USENIX atc'22] an efficient distributed training framework whale that supports the super large-scale model of heterogeneous GPU clusters
4279. 笛卡尔树
Huffman tree (optimal binary tree)
Reentrantlock reentrant lock
从哪些维度评判代码质量的好坏?如何具备写出高质量代码的能力?
Pattern water flow lamp 1: check the table and display the LED lamp
[fluent -- layout] flow layout (flow and wrap)
Simple encapsulation of wechat applet wx.request
循环结构practice
Exomiser对外显子组变体进行注释和优先排序
Memorythrashing: Tiktok live broadcast to solve memory dithering practice
IP protocol - network segment division
【Flutter -- 布局】流式布局(Flow和Wrap)
[machine learning basics] - another perspective to explain SVM
ReentrantLock 可重入锁
Getting started with mongodb