题目传送门

题目大意

给出 \(t\) 个 \(n\) 个点 \(m\) 条边的无向图,每次可以从任意一棵树选择一条边删掉,然后该树不与根(为 \(1\) )联通的部分被删掉。不能操作的人输。问谁有必胜策略。

每棵树都满足:每个环都只会挂在叶子节点上。

\(n\le 100,m\le 500\)

思路

怎么说呢?很厉害的题目吧。

首先考虑一个树的情况,我们设 \(sg(u)\) 表示 \(u\) 子树内的 \(sg\) 函数值,我们可以得到转移式:

\[sg(u)=\text{mex}_{v\in son_u}\{sg(v)\}
\]
\[\Rightarrow sg(u)=\otimes_{v\in son_u} (sg(v)+1)
\]

这个可以通过打表发现,不过有一种比较巧妙的方法,就是说我们把主链拉出来,那么相当于每一个节点连了一条链,那么,删边就相当于取石子了。

然后考虑拓展到任意图上。这里给出一个结论:

一个环如果大小为偶数,它的顶点产生的贡献为 \(0\),反之为 \(1\)。

相当于环大小为偶数时,把环缩为一个点,否则再连向一个点。

考虑证明,我们发现我们可以通过枚举破掉环上哪条边来求,你发现环大小为偶数时,你破掉之后两条链长度一定一奇一偶,也就是对该环的根(挂的叶子)产生的贡献一定为偶数,所以第一个未出现的正整数一定为 \(1\)。同理,我们可以推出环大小为奇数的情况,这里就不再赘述了。

这里提醒一些细节:

  • 需要考虑重边

  • 需要考虑多个环串在一个顶点的情况

具体见代码就好了。

\(\texttt{Code1}\)

#include <cstdio>
#include <vector>
#include <cstring>
#include <bits/stdc++.h>
using namespace std; #define Int register int
#define MAXN 105 template <typename T> inline void read (T &t){t = 0;char c = getchar();int f = 1;while (c < '0' || c > '9'){if (c == '-') f = -f;c = getchar();}while (c >= '0' && c <= '9'){t = (t << 3) + (t << 1) + c - '0';c = getchar();} t *= f;}
template <typename T> inline void write (T x){if (x < 0){x = -x;putchar ('-');}if (x > 9) write (x / 10);putchar (x % 10 + '0');} int T,n,m,sg[MAXN],dep[MAXN],vis[MAXN]; int toop,head[MAXN],to[MAXN * 10],nxt[MAXN * 10]; void Add_Edge (int u,int v){
to[++ toop] = v,nxt[toop] = head[u],head[u] = toop;
to[++ toop] = u,nxt[toop] = head[v],head[v] = toop;
} int dfs (int u,int fa){
bool flag = 0;
dep[u] = dep[fa] + 1,vis[u] = 1;
for (Int i = head[u];i;i = nxt[i]){
int v = to[i];
if (!v) continue;
if (v == fa && !flag){
flag = 1;
continue;
}
if (vis[v]){
sg[v] ^= (dep[u] - dep[v] + 1 & 1);
to[i ^ 1] = 0;
return v;
}
else{
int cur = dfs (v,u);
if (!cur) sg[u] ^= sg[v] + 1;
else if (cur ^ u) return cur;
}
}
return 0;
} void clear (){
toop = 1,memset (head,0,sizeof (head));
for (Int i = 1;i <= n;++ i) sg[i] = dep[i] = vis[i] = 0;
} signed main(){
while (~scanf ("%d",&T)){
int ans = 0;
while (T --> 0){
read (n),read (m),clear ();
for (Int i = 1,u,v;i <= m;++ i) read (u),read (v),Add_Edge (u,v);
dfs (1,0),ans ^= sg[1];
}
puts (ans ? "Sally" : "Harry");
}
return 0;
}

\(\texttt{Code2}\)

#include <cstdio>
#include <vector>
using namespace std; #define Int register int
#define MAXN 105 template <typename T> inline void read (T &t){t = 0;char c = getchar();int f = 1;while (c < '0' || c > '9'){if (c == '-') f = -f;c = getchar();}while (c >= '0' && c <= '9'){t = (t << 3) + (t << 1) + c - '0';c = getchar();} t *= f;}
template <typename T> inline void write (T x){if (x < 0){x = -x;putchar ('-');}if (x > 9) write (x / 10);putchar (x % 10 + '0');} vector <int> G[MAXN];
int T,n,m,top,sg[MAXN],dep[MAXN],vis[MAXN],sta[MAXN]; void Add_Edge (int u,int v){
G[u].push_back (v),
G[v].push_back (u);
} void dfs (int u,int fa){
bool flag = 0;
sta[++ top] = u,vis[u] = 1;
for (Int i = 0;i < G[u].size();++ i){
int v = G[u][i];
if (v == fa && !flag){
flag = 1;
continue;
}
if (vis[v] == 1){
int cnt = 1;
while (sta[top] != v){
cnt ++;
vis[sta[top --]] = 0;
}
sg[v] ^= (cnt & 1);
}
else if (vis[v] == -1){
dfs (v,u);
if (vis[v]) sg[u] ^= sg[v] + 1;
}
}
if (vis[u]) -- top;
return ;
} void clear (){
top = 0;
for (Int i = 1;i <= n;++ i) sg[i] = dep[i] = 0,vis[i] = -1,G[i].clear ();
} signed main(){
while (~scanf ("%d",&T)){
int ans = 0;
while (T --> 0){
read (n),read (m),clear ();
for (Int i = 1,u,v;i <= m;++ i){
read (u),read (v);
if (u ^ v) Add_Edge (u,v);
}
dfs (1,0),ans ^= sg[1];
}
puts (ans ? "Sally" : "Harry");
}
return 0;
}

题解 Christmas Game的更多相关文章

  1. 题解 AT4278 【[ABC115A] Christmas Eve Eve Eve】

    题目传送门. 分析 根据题目,我们可以发现要求如下: \(d\)的值 输出 \(d=25\) Christmas \(d=24\) Christmas Eve \(d=23\) Christmas E ...

  2. POJ3160 Father Christmas flymouse[强连通分量 缩点 DP]

    Father Christmas flymouse Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 3241   Accep ...

  3. poj 3013 Big Christmas Tree (最短路径Dijsktra) -- 第一次用优先队列写Dijsktra

    http://poj.org/problem?id=3013 Big Christmas Tree Time Limit: 3000MS   Memory Limit: 131072K Total S ...

  4. 【POJ3710】Christmas Game (博弈-树上的删边问题)

    [题目] Description Harry and Sally were playing games at Christmas Eve. They drew some Christmas trees ...

  5. Codeforces:Good Bye 2018(题解)

    Good Bye 2018! 题目链接:https://codeforces.com/contest/1091 A. New Year and the Christmas Ornament 题意: 给 ...

  6. AtCoder Beginner Contest 115 题解

    题目链接:https://abc115.contest.atcoder.jp/ A Christmas Eve Eve Eve 题目: Time limit : 2sec / Memory limit ...

  7. Good Bye 2018题解

    Good Bye 2018题解 题解 CF1091A [New Year and the Christmas Ornament] 打完cf都忘记写题解了qwq 题意就是:给你一些黄,蓝,红的球,满足蓝 ...

  8. Codeforces Round #611 (Div. 3) A-F简要题解

    contest链接:https://codeforces.com/contest/1283 A. Minutes Before the New Year 题意:给一个当前时间,输出离第二天差多少分钟 ...

  9. 2016 华南师大ACM校赛 SCNUCPC 非官方题解

    我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...

随机推荐

  1. NVidia Jetson Ubuntu 18.04 安装ROS过程中运行sudo rosdep init指令出错

    参考:https://www.cnblogs.com/xuhaoforwards/p/9399744.html 安装ROS过程中运行sudo rosdep init后,出现如下错误LOG: ERROR ...

  2. linux-解决/usr/bin/which: no ssh-copy-id in 和ssh: Could not resolve hostname问题

    使用yum install openssh-clients  安装命令 有的系统没有此命令 有的系统缺省 就包含这一条命令! 但是我的测试机没有发现此命令 只能这样安装! 这时有报错了 1 2 [ro ...

  3. k8s笔记0528-基于KUBERNETES构建企业容器云手动部署集群记录-6

    1.创建一个测试用的deployment [root@linux-node1 ~]# kubectl run net-test --image=alpine --replicas=2 sleep 36 ...

  4. 基于Linux系统的网络服务——高速缓存DNS及企业级域名解析服务

    1.DNS域名系统 DNS(Domain Name System,域名系统),因特网上作为域名和IP地址相互映射的一个分布式数据库,能够使用户更方便的访问互联网,而不用去记住能够被机器直接读取的IP数 ...

  5. NOIP模拟50

    过分的神圣,往往比恶魔更加恶质. 前言 最大的一个收获就是不要动不动就码线段树,一定要审清楚题目之后再码!! T1 一开始理解错题了,以为答案是就是 \(\dfrac{\operatorname{le ...

  6. [编译] 10、kconfig 入门指导教程

    目录 前言 1. 安装 kconfig 2. 克隆一个 demo 3. 运行 kconfig 4. 源码解析 4.1 选择题目设计模板 4.2 填空题目设计模板 4.3 判断题目设计模板 5. 产物解 ...

  7. 从 1 开始学 JVM 系列 | JVM 类加载器(一)

    从 1 开始学 JVM 系列 类加载器,对于很多人来说并不陌生.我自己第一次听到这个概念时觉得有点"高大上",觉得只有深入 JDK 源码才会触碰到 ClassLoader,平时都是 ...

  8. 【JDK】分析 String str=““ 与 new String()

    一.基础概念 为了讲清楚他们的差异,这里先介绍几个概念. 1.1 常量池 所谓常量池:顾名思义就是用来存放一些常量的.该常量是在编译期被确定,并被保存在已编译的.class文件中,其中包括了类,方法, ...

  9. HDU2063 过山车(二分匹配)

    过山车 HDU - 2063 RPG girls今天和大家一起去游乐场玩,终于可以坐上梦寐以求的过山车了.可是,过山车的每一排只有两个座位,而且还有条不成文的规矩,就是每个女生必须找个个男生做part ...

  10. vue 熟悉项目结构 创建第一个自己的组件

    * vue开发环境搭建 * 项目入口文件 ./src/main.js // The Vue build version to load with the `import` command // (ru ...