牛客 - 树上博弈(思维)
生活随笔
收集整理的這篇文章主要介紹了
牛客 - 树上博弈(思维)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目鏈接:點擊查看
題目大意:給出一棵樹,現在有兩個人在玩一個游戲,兩人依次在樹上行走,只能走沒有人的結點,無法行走的人失敗,題目需要我們求出有多少個初始點對,對于先手而言是必勝的情況
題目分析:其實稍微畫圖就能看出來,如果初始時兩個人之間的距離為偶數,那么先手必勝,否則先手必敗,因為如果初始距離為偶數的話,那么先手總是可以有路可走的,而后手總會被逼到葉子結點上去,而距離為偶數,意味著深度奇偶性相同的結點,讀入時記錄一下奇偶性不同的結點有多少個,最后排列組合C( n , 2 )就是答案了,因為當兩個點確定時,兩人的位置還可以互換,所以最后結果需要乘以 2?
代碼:
#include<iostream> #include<cstdio> #include<string> #include<ctime> #include<cmath> #include<cstring> #include<algorithm> #include<stack> #include<climits> #include<queue> #include<map> #include<set> #include<sstream> #include<unordered_map> using namespace std;typedef long long LL;typedef unsigned long long ull;const int inf=0x3f3f3f3f;const int N=1e6+100;int deep[N];LL cnt[2];int main() { //#ifndef ONLINE_JUDGE // freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); //#endif // ios::sync_with_stdio(false);int n;scanf("%d",&n);deep[1]=0;cnt[0]++;for(int i=2;i<=n;i++){int fa;scanf("%d",&fa);deep[i]=deep[fa]^1;cnt[deep[i]]++;}printf("%lld\n",cnt[0]*(cnt[0]-1)+cnt[1]*(cnt[1]-1));return 0; }?
總結
以上是生活随笔為你收集整理的牛客 - 树上博弈(思维)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CodeForces - 1300E W
- 下一篇: ZOJ - 3228 Searching