题解:P8347 「Wdoi-6」另一侧的月
题意转换:在无根树意义下,双方轮流操作,每次砍掉一棵子树,砍成只剩一个点的输。
容易发现,谁整出一个只有两个结点的链,谁就赢了,因为对手必须把它砍成一个点。
之后我们有结论:删除两个互为兄弟的叶子,先后手胜负情况不变。 下面证明。
假设你是删除这两个结点后的必胜方,你可以选择忽略这两个结点,然后:
- 如果对手某一步只砍掉其中一个点,那么要么是只砍了一个点,要么是只剩了一个点。只剩了一个点的话对手就直接输了。只砍了一个点的话,你砍掉另外一个点就行了,这样你就化为一开始删除这两个结点之后了。
- 如果对手某一步同时砍掉了了这两个点,和在原图上去掉这两个点,再砍掉对应子树是一样的。所以你也是必胜的。
又发现,如果一个结点下面挂着偶数个叶子,那么它就相当于也是一个叶子,并且这个是可以往上递归的。
于是我们就随便选一个根 DFS 这棵树,把所有互为兄弟的叶子删掉,如果一个结点没有子结点就令其变成叶子。最后看一下剩下的图,如果它是只有两个结点的链就后手必胜,否则先手必胜。做完了。 :::info[上面那个东西正确性证明] 如果是只有两个结点的链显然后手必胜。
| 否则,考虑最深的叶子,它的父亲显然不可能挂别的叶子(否则就早被删掉了),也不可能挂非叶子(因为它挂的叶子是最深的)。所以它父亲的子树就是只有两个结点的链,砍掉其它就行了。 |
|---|
| :::success[code] |
| ``` |
| #include<bits/stdc++.h> |
| using namespace std; |
| #define int long long |
| #define fi first |
| #define se second |
| #define pii pair<int,int> |
| #define lowbit(x) ((x)&(-(x))) |
| #define popc(x) __builtin_popcountll(x) |
| #define ctz(x) __builtin_ctzll(x) |
| #define clz(x) __builtin_clzll(x) |
| #define double long double |
| #define sqrt(x) sqrtl(x) |
| #define cbrt(x) cbrtl(x) |
| #define pow(x,y) powl(x,y) |
| #define sin(x) sinl(x) |
| #define cos(x) cosl(x) |
| #define tan(x) tanl(x) |
| #define vct basic_string |
| const int N=5e5+10,mod=998244353; |
| vct<int>adj[N]; |
| bool leaf[N]; |
| int n; |
| bool ok1,ok2; |
| void dfs(int u,int f) |
| { |
| bool hvl=0; |
| for(auto v:adj[u]) |
| { |
| if(v==f) continue; |
| dfs(v,u); |
| if(leaf[v]) hvl^=1; |
| } |
| if(hvl) |
| { |
| if(u==1) ok2=1; |
| else ok1=0; |
| } |
| else leaf[u]=1; |
| } |
| void solve() |
| { |
| cin>>n; |
| for(int i=1;i<=n;i++) leaf[i]=0,adj[i].clear(); |
| for(int i=1;i<n;i++) |
| { |
| int u,v; |
| cin>>u>>v; |
| adj[u]+=v,adj[v]+=u; |
| } |
| for(int i=1;i<=n;i++) if(adj[i].size()==1) leaf[i]=1; |
| ok1=1,ok2=0; |
| dfs(1,0); |
| if(ok1&&ok2) cout<<"Luna\n"; |
| else cout<<"Hifuu\n"; |
| } |
| signed main() |
| { |
| ios::sync_with_stdio(0); |
| cin.tie(0),cout.tie(0); |
| int t; |
| cin>>t; |
| while(t--) solve(); |
| return 0; |
| } |
| ``` |
| ::: |